Skip to content

Commit 0e1ef5d

Browse files
flobauermpociot
authored andcommitted
Workplace integrations (#54)
* Workplace (Facebook for Companies) Integration to Facebook Driver * profile picture and format adaptions * code formatting * Final typo
1 parent 3c924a2 commit 0e1ef5d

3 files changed

Lines changed: 130 additions & 6 deletions

File tree

src/Extensions/User.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ public function __construct(
3131
*/
3232
public function getProfilePic()
3333
{
34-
return $this->user_info['profile_pic'] ?? null;
34+
if (isset($this->user_info['profile_pic'])) {
35+
return $this->user_info['profile_pic'];
36+
}
37+
38+
// Workplace (Facebook for companies) uses picture parameter
39+
if (isset($this->user_info['picture'])) {
40+
return $this->user_info['picture']['data']['url'];
41+
}
3542
}
3643

3744
/**
@@ -47,30 +54,30 @@ public function getLocale()
4754
*/
4855
public function getTimezone()
4956
{
50-
return $this->user_info['timezone'] ?? null;
57+
return isset($this->user_info['timezone']) ? $this->user_info['timezone'] : null;
5158
}
5259

5360
/**
5461
* @return string
5562
*/
5663
public function getGender()
5764
{
58-
return $this->user_info['gender'] ?? null;
65+
return isset($this->user_info['gender']) ? $this->user_info['gender'] : null;
5966
}
6067

6168
/**
6269
* @return bool
6370
*/
6471
public function getIsPaymentEnabled()
6572
{
66-
return $this->user_info['is_payment_enabled'] ?? null;
73+
return isset($this->user_info['is_payment_enabled']) ? $this->user_info['is_payment_enabled'] : null;
6774
}
6875

6976
/**
7077
* @return array
7178
*/
7279
public function getLastAdReferral()
7380
{
74-
return $this->user_info['last_ad_referral'] ?? null;
81+
return isset($this->user_info['last_ad_referral']) ? $this->user_info['last_ad_referral'] : null;
7582
}
7683
}

src/FacebookDriver.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,18 @@ public function isConfigured()
383383
*/
384384
public function getUser(IncomingMessage $matchingMessage)
385385
{
386-
$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'));
386+
$messagingDetails = $this->event->get('messaging')[0];
387+
388+
// field string available at Facebook
389+
$fields = 'first_name,last_name,profile_pic,locale,timezone,gender,is_payment_enabled,last_ad_referral';
390+
391+
// WORKPLACE (Facebook for companies)
392+
// if community isset in sender Object, it is a request done by workplace
393+
if (isset($messagingDetails['sender']['community'])) {
394+
$fields = 'first_name,last_name,email,title,department,employee_number,primary_phone,primary_address,picture,link,locale,name,name_format,updated_time';
395+
}
396+
397+
$userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields='.$fields.'&access_token='.$this->config->get('token'));
387398

388399
$this->throwExceptionIfResponseNotOk($userInfoData);
389400
$userInfo = json_decode($userInfoData->getContent(), true);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Tests\Drivers;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use BotMan\Drivers\Facebook\Extensions\User;
7+
8+
class FacebookWorkplaceUserTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function createTestUser()
11+
{
12+
$userInfo = [
13+
'id' => '1234',
14+
'first_name' => 'Christine',
15+
'last_name' => 'Manning',
16+
'email' => 'christine.manning@example.com',
17+
'title' => 'Experimenter',
18+
'picture' => [
19+
'data' => [
20+
'height' => 50,
21+
'is_silhouette' => true,
22+
'url' => 'http://profilepic.com',
23+
'width' => 50,
24+
],
25+
],
26+
'link' => 'http://workplace-link.facebook.com/app_scoped_user_id/100014245873942/',
27+
'locale' => 'en_US',
28+
'name' => 'Christine Manning',
29+
'name_format' => '{first} {last}',
30+
'updated_time' => '2016-11-24T06:37:15+0000',
31+
];
32+
33+
$user = new User(
34+
'1234',
35+
'Christine',
36+
'Manning',
37+
null,
38+
$userInfo
39+
);
40+
41+
return $user;
42+
}
43+
44+
public function testFirstName()
45+
{
46+
$user = $this->createTestUser();
47+
48+
$this->assertEquals('Christine', $user->getFirstName());
49+
}
50+
51+
public function testLastName()
52+
{
53+
$user = $this->createTestUser();
54+
55+
$this->assertEquals('Manning', $user->getLastName());
56+
}
57+
58+
public function testUsername()
59+
{
60+
$user = $this->createTestUser();
61+
62+
$this->assertNull($user->getUsername());
63+
}
64+
65+
public function testProfilePic()
66+
{
67+
$user = $this->createTestUser();
68+
69+
$this->assertEquals('http://profilepic.com', $user->getProfilePic());
70+
}
71+
72+
public function testLocale()
73+
{
74+
$user = $this->createTestUser();
75+
76+
$this->assertEquals('en_US', $user->getLocale());
77+
}
78+
79+
public function testTimezone()
80+
{
81+
$user = $this->createTestUser();
82+
83+
$this->assertNull($user->getTimezone());
84+
}
85+
86+
public function testGender()
87+
{
88+
$user = $this->createTestUser();
89+
90+
$this->assertNull($user->getGender());
91+
}
92+
93+
public function testIsPaymentEnabled()
94+
{
95+
$user = $this->createTestUser();
96+
97+
$this->assertNull($user->getIsPaymentEnabled());
98+
}
99+
100+
public function testLastAdReferral()
101+
{
102+
$user = $this->createTestUser();
103+
104+
$this->assertNull($user->getLastAdReferral());
105+
}
106+
}

0 commit comments

Comments
 (0)