Skip to content

Commit

Permalink
Workplace integrations (#54)
Browse files Browse the repository at this point in the history
* Workplace (Facebook for Companies) Integration to Facebook Driver

* profile picture and format adaptions

* code formatting

* Final typo
  • Loading branch information
flobauer authored and mpociot committed Dec 13, 2017
1 parent 3c924a2 commit 0e1ef5d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/Extensions/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ public function __construct(
*/
public function getProfilePic()
{
return $this->user_info['profile_pic'] ?? null;
if (isset($this->user_info['profile_pic'])) {
return $this->user_info['profile_pic'];
}

// Workplace (Facebook for companies) uses picture parameter
if (isset($this->user_info['picture'])) {
return $this->user_info['picture']['data']['url'];
}
}

/**
Expand All @@ -47,30 +54,30 @@ public function getLocale()
*/
public function getTimezone()
{
return $this->user_info['timezone'] ?? null;
return isset($this->user_info['timezone']) ? $this->user_info['timezone'] : null;
}

/**
* @return string
*/
public function getGender()
{
return $this->user_info['gender'] ?? null;
return isset($this->user_info['gender']) ? $this->user_info['gender'] : null;
}

/**
* @return bool
*/
public function getIsPaymentEnabled()
{
return $this->user_info['is_payment_enabled'] ?? null;
return isset($this->user_info['is_payment_enabled']) ? $this->user_info['is_payment_enabled'] : null;
}

/**
* @return array
*/
public function getLastAdReferral()
{
return $this->user_info['last_ad_referral'] ?? null;
return isset($this->user_info['last_ad_referral']) ? $this->user_info['last_ad_referral'] : null;
}
}
13 changes: 12 additions & 1 deletion src/FacebookDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,18 @@ public function isConfigured()
*/
public function getUser(IncomingMessage $matchingMessage)
{
$userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields=first_name,last_name,profile_pic,locale,timezone,gender,is_payment_enabled,last_ad_referral&access_token='.$this->config->get('token'));
$messagingDetails = $this->event->get('messaging')[0];

// field string available at Facebook
$fields = 'first_name,last_name,profile_pic,locale,timezone,gender,is_payment_enabled,last_ad_referral';

// 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);
Expand Down
106 changes: 106 additions & 0 deletions tests/FacebookWorkplaceUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Tests\Drivers;

use PHPUnit_Framework_TestCase;
use BotMan\Drivers\Facebook\Extensions\User;

class FacebookWorkplaceUserTest extends PHPUnit_Framework_TestCase
{
public function createTestUser()
{
$userInfo = [
'id' => '1234',
'first_name' => 'Christine',
'last_name' => 'Manning',
'email' => 'christine.manning@example.com',
'title' => 'Experimenter',
'picture' => [
'data' => [
'height' => 50,
'is_silhouette' => true,
'url' => 'http://profilepic.com',
'width' => 50,
],
],
'link' => 'http://workplace-link.facebook.com/app_scoped_user_id/100014245873942/',
'locale' => 'en_US',
'name' => 'Christine Manning',
'name_format' => '{first} {last}',
'updated_time' => '2016-11-24T06:37:15+0000',
];

$user = new User(
'1234',
'Christine',
'Manning',
null,
$userInfo
);

return $user;
}

public function testFirstName()
{
$user = $this->createTestUser();

$this->assertEquals('Christine', $user->getFirstName());
}

public function testLastName()
{
$user = $this->createTestUser();

$this->assertEquals('Manning', $user->getLastName());
}

public function testUsername()
{
$user = $this->createTestUser();

$this->assertNull($user->getUsername());
}

public function testProfilePic()
{
$user = $this->createTestUser();

$this->assertEquals('http://profilepic.com', $user->getProfilePic());
}

public function testLocale()
{
$user = $this->createTestUser();

$this->assertEquals('en_US', $user->getLocale());
}

public function testTimezone()
{
$user = $this->createTestUser();

$this->assertNull($user->getTimezone());
}

public function testGender()
{
$user = $this->createTestUser();

$this->assertNull($user->getGender());
}

public function testIsPaymentEnabled()
{
$user = $this->createTestUser();

$this->assertNull($user->getIsPaymentEnabled());
}

public function testLastAdReferral()
{
$user = $this->createTestUser();

$this->assertNull($user->getLastAdReferral());
}
}

0 comments on commit 0e1ef5d

Please sign in to comment.