Skip to content

Commit

Permalink
new getUserWithField method (#74)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
caleuanhopkins authored and mpociot committed Aug 1, 2018
1 parent d26cb64 commit 0820433
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/FacebookDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,32 @@ public function isConfigured()
return ! empty($this->config->get('token'));
}

/**
* Retrieve specific User field information.
*
* @param array $fields
* @param IncomingMessage $matchingMessage
* @return User
* @throws FacebookException
*/
public function getUserWithFields(array $fields, IncomingMessage $matchingMessage)
{
$messagingDetails = $this->event->get('messaging')[0];
// implode field array to create concatinated comma string
$fields = implode (",", $fields);
// WORKPLACE (Facebook for companies)
// if community isset in sender Object, it is a request done by workplace
if (isset($messagingDetails['sender']['community'])) {
$fields = 'first_name,last_name,email,title,department,employee_number,primary_phone,primary_address,picture,link,locale,name,name_format,updated_time';
}
$userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields='.$fields.'&access_token='.$this->config->get('token'));
$this->throwExceptionIfResponseNotOk($userInfoData);
$userInfo = json_decode($userInfoData->getContent(), true);
$firstName = $userInfo['first_name'] ?? null;
$lastName = $userInfo['last_name'] ?? null;
return new User($matchingMessage->getSender(), $firstName, $lastName, null, $userInfo);
}

/**
* Retrieve User information.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/FacebookDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ public function it_returns_the_user_object()
$this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
}

/** @test */
public function it_returns_the_user_first_name()
{
$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"}}]}]}';
$facebookResponse = '{"first_name":"John"}';
$htmlInterface = m::mock(Curl::class);
$htmlInterface->shouldReceive('get')->once()->with('https://graph.facebook.com/v3.0/1433960459967306?fields=first_name&access_token=Foo')->andReturn(new Response($facebookResponse));
$driver = $this->getDriver($request, null, '', $htmlInterface);
$message = $driver->getMessages()[0];
$user = $driver->getUserWithFields(['first_name'],$message);
$this->assertSame($user->getId(), '1433960459967306');
$this->assertEquals('John', $user->getFirstName());
$this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
}

/** @test */
public function it_throws_exception_in_get_user()
{
Expand Down

0 comments on commit 0820433

Please sign in to comment.