From dd4514d60bc1c70699d1c1c6478cc470c0460aa3 Mon Sep 17 00:00:00 2001 From: Christoph Rumpel Date: Tue, 7 Aug 2018 09:34:49 +0200 Subject: [PATCH] Feature - Add reusable for attachments (#75) * Add way to set attachment reusable for Facebook * Remove discovery folder * Fix styleci config * Fix styles * Fix styles --- .styleci.yml | 4 +-- src/FacebookDriver.php | 4 ++- tests/FacebookDriverTest.php | 63 +++++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index a2f2088..6034ad4 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,6 +1,4 @@ preset: laravel enabled: - - unalign_double_arrow - -linting: true \ No newline at end of file +- unalign_double_arrow \ No newline at end of file diff --git a/src/FacebookDriver.php b/src/FacebookDriver.php index 3c63e14..b830b42 100644 --- a/src/FacebookDriver.php +++ b/src/FacebookDriver.php @@ -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(), ], ]; @@ -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'])) { @@ -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); } diff --git a/tests/FacebookDriverTest.php b/tests/FacebookDriverTest.php index 1965a14..a1f95c1 100644 --- a/tests/FacebookDriverTest.php +++ b/tests/FacebookDriverTest.php @@ -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()); @@ -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', ], ], @@ -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', ], ], @@ -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', ], ], @@ -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() {