Skip to content

Commit 0820433

Browse files
caleuanhopkinsmpociot
authored andcommitted
new getUserWithField method (#74)
* Removing extra profile fields Removed Gender, Locale, and Timezone as these need extra verification and submission to Facebook's app team even if it's not needed for just getting a users name. Basic user approval gives access to name. * new method getUserWithFields() * adding back in FB fields for getUser() * Update FacebookDriver.php
1 parent d26cb64 commit 0820433

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/FacebookDriver.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,32 @@ public function isConfigured()
407407
return ! empty($this->config->get('token'));
408408
}
409409

410+
/**
411+
* Retrieve specific User field information.
412+
*
413+
* @param array $fields
414+
* @param IncomingMessage $matchingMessage
415+
* @return User
416+
* @throws FacebookException
417+
*/
418+
public function getUserWithFields(array $fields, IncomingMessage $matchingMessage)
419+
{
420+
$messagingDetails = $this->event->get('messaging')[0];
421+
// implode field array to create concatinated comma string
422+
$fields = implode (",", $fields);
423+
// WORKPLACE (Facebook for companies)
424+
// if community isset in sender Object, it is a request done by workplace
425+
if (isset($messagingDetails['sender']['community'])) {
426+
$fields = 'first_name,last_name,email,title,department,employee_number,primary_phone,primary_address,picture,link,locale,name,name_format,updated_time';
427+
}
428+
$userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields='.$fields.'&access_token='.$this->config->get('token'));
429+
$this->throwExceptionIfResponseNotOk($userInfoData);
430+
$userInfo = json_decode($userInfoData->getContent(), true);
431+
$firstName = $userInfo['first_name'] ?? null;
432+
$lastName = $userInfo['last_name'] ?? null;
433+
return new User($matchingMessage->getSender(), $firstName, $lastName, null, $userInfo);
434+
}
435+
410436
/**
411437
* Retrieve User information.
412438
*

tests/FacebookDriverTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,21 @@ public function it_returns_the_user_object()
221221
$this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
222222
}
223223

224+
/** @test */
225+
public function it_returns_the_user_first_name()
226+
{
227+
$request = '{"object":"page","entry":[{"id":"111899832631525","time":1480279487271,"messaging":[{"sender":{"id":"1433960459967306"},"recipient":{"id":"111899832631525"},"timestamp":1480279487147,"message":{"mid":"mid.1480279487147:4388d3b344","seq":36,"text":"Hi Julia"}}]}]}';
228+
$facebookResponse = '{"first_name":"John"}';
229+
$htmlInterface = m::mock(Curl::class);
230+
$htmlInterface->shouldReceive('get')->once()->with('https://graph.facebook.com/v3.0/1433960459967306?fields=first_name&access_token=Foo')->andReturn(new Response($facebookResponse));
231+
$driver = $this->getDriver($request, null, '', $htmlInterface);
232+
$message = $driver->getMessages()[0];
233+
$user = $driver->getUserWithFields(['first_name'],$message);
234+
$this->assertSame($user->getId(), '1433960459967306');
235+
$this->assertEquals('John', $user->getFirstName());
236+
$this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
237+
}
238+
224239
/** @test */
225240
public function it_throws_exception_in_get_user()
226241
{

0 commit comments

Comments
 (0)