Skip to content

Commit

Permalink
Feature - Add reusable for attachments (#75)
Browse files Browse the repository at this point in the history
* Add way to set attachment reusable for Facebook

* Remove discovery folder

* Fix styleci config

* Fix styles

* Fix styles
  • Loading branch information
Christoph Rumpel committed Aug 7, 2018
1 parent 0820433 commit dd4514d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
4 changes: 1 addition & 3 deletions .styleci.yml
@@ -1,6 +1,4 @@
preset: laravel

enabled:
- unalign_double_arrow

linting: true
- unalign_double_arrow
4 changes: 3 additions & 1 deletion src/FacebookDriver.php
Expand Up @@ -373,6 +373,7 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
$parameters['message']['attachment'] = [
'type' => $attachmentType,
'payload' => [
'is_reusable' => $attachment->getExtras('is_reusable') ?? false,
'url' => $attachment->getUrl(),
],
];
Expand Down Expand Up @@ -419,7 +420,7 @@ public function getUserWithFields(array $fields, IncomingMessage $matchingMessag
{
$messagingDetails = $this->event->get('messaging')[0];
// implode field array to create concatinated comma string
$fields = implode (",", $fields);
$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'])) {
Expand All @@ -430,6 +431,7 @@ public function getUserWithFields(array $fields, IncomingMessage $matchingMessag
$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);
}

Expand Down
63 changes: 62 additions & 1 deletion tests/FacebookDriverTest.php
Expand Up @@ -230,7 +230,7 @@ public function it_returns_the_user_first_name()
$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);
$user = $driver->getUserWithFields(['first_name'], $message);
$this->assertSame($user->getId(), '1433960459967306');
$this->assertEquals('John', $user->getFirstName());
$this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
Expand Down Expand Up @@ -733,6 +733,7 @@ public function it_can_reply_message_objects_with_image()
'attachment' => [
'type' => 'image',
'payload' => [
'is_reusable' => false,
'url' => 'http://image.url//foo.png',
],
],
Expand Down Expand Up @@ -788,6 +789,7 @@ public function it_can_reply_message_objects_with_audio()
'attachment' => [
'type' => 'audio',
'payload' => [
'is_reusable' => false,
'url' => 'http://image.url//foo.mp3',
],
],
Expand Down Expand Up @@ -843,6 +845,7 @@ public function it_can_reply_message_objects_with_file()
'attachment' => [
'type' => 'file',
'payload' => [
'is_reusable' => false,
'url' => 'http://image.url//foo.pdf',
],
],
Expand All @@ -864,6 +867,64 @@ public function it_can_reply_message_objects_with_file()
File::url('http://image.url//foo.pdf')), $message));
}

/** @test */
public function it_can_reply_message_objects_with_reusable_file()
{
$responseData = [
'object' => 'page',
'event' => [
[
'messaging' => [
[
'sender' => [
'id' => '1234567890',
],
'recipient' => [
'id' => '0987654321',
],
'message' => [
'text' => 'test',
],
],
],
],
],
];

$htmlInterface = m::mock(Curl::class);
$htmlInterface->shouldReceive('post')->once()->with('https://graph.facebook.com/v3.0/me/messages', [], [
'messaging_type' => 'RESPONSE',
'recipient' => [
'id' => '1234567890',
],
'message' => [
'attachment' => [
'type' => 'file',
'payload' => [
'is_reusable' => true,
'url' => 'http://image.url//foo.pdf',
],
],
],
'access_token' => 'Foo',
])->andReturn(new Response());

$request = m::mock(\Symfony\Component\HttpFoundation\Request::class.'[getContent]');
$request->shouldReceive('getContent')->andReturn(json_encode($responseData));

$driver = new FacebookDriver($request, [
'facebook' => [
'token' => 'Foo',
],
], $htmlInterface);

$message = new IncomingMessage('', '1234567890', '');
$file = File::url('http://image.url//foo.pdf');
$file->addExtras('is_reusable', true);

$driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', $file), $message));
}

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

0 comments on commit dd4514d

Please sign in to comment.