From 7a9daabd55c1008242b46de2b6156a7378e895ba Mon Sep 17 00:00:00 2001 From: Patrick Landolt Date: Wed, 13 Nov 2019 00:37:09 +0100 Subject: [PATCH] make attachments work --- src/SlackDriver.php | 7 ++++++- tests/SlackDriverTest.php | 42 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/SlackDriver.php b/src/SlackDriver.php index 0524444..5333fc5 100644 --- a/src/SlackDriver.php +++ b/src/SlackDriver.php @@ -363,7 +363,12 @@ protected function replyWithToken($message, $matchingMessage, $additionalParamet $attachment = $message->getAttachment(); if (! is_null($attachment)) { if ($attachment instanceof Image) { - $parameters['attachments'] = json_encode(['image_url' => $attachment->getUrl()]); + $parameters['attachments'] = json_encode([ + [ + 'title' => $attachment->getTitle() ? $attachment->getTitle() : $attachment->getUrl(), + 'image_url' => $attachment->getUrl(), + ], + ]); } } } else { diff --git a/tests/SlackDriverTest.php b/tests/SlackDriverTest.php index 1fde918..8373838 100644 --- a/tests/SlackDriverTest.php +++ b/tests/SlackDriverTest.php @@ -483,7 +483,7 @@ public function it_can_reply_message_objects() } /** @test */ - public function it_can_reply_message_objects_with_image() + public function it_can_reply_message_objects_with_image_without_title() { $responseData = [ 'event' => [ @@ -504,7 +504,7 @@ public function it_can_reply_message_objects_with_image() 'token' => 'Foo', 'channel' => 'general', 'text' => 'Test', - 'attachments' => json_encode(['image_url' => 'http://image.url/foo.png']), + 'attachments' => json_encode([['title' => 'http://image.url/foo.png', 'image_url' => 'http://image.url/foo.png']]), ]); $request = m::mock(\Symfony\Component\HttpFoundation\Request::class.'[getContent]'); @@ -520,6 +520,44 @@ public function it_can_reply_message_objects_with_image() $driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', Image::url('http://image.url/foo.png')), $message)); } + /** @test */ + public function it_can_reply_message_objects_with_image_with_title() + { + $responseData = [ + 'event' => [ + 'user' => 'U0X12345', + 'channel' => 'general', + 'text' => 'response', + ], + ]; + + $html = m::mock(Curl::class); + $this->mockAuthTestEndpoint($html); + $this->mockUserInfoEndpoint($html); + + $html->shouldReceive('post') + ->once() + ->with('https://slack.com/api/chat.postMessage', [], [ + 'as_user' => true, + 'token' => 'Foo', + 'channel' => 'general', + 'text' => 'Test', + 'attachments' => json_encode([['title' => 'title', 'image_url' => 'http://image.url/foo.png']]), + ]); + + $request = m::mock(\Symfony\Component\HttpFoundation\Request::class.'[getContent]'); + $request->shouldReceive('getContent')->andReturn(json_encode($responseData)); + + $driver = new SlackDriver($request, [ + 'slack' => [ + 'token' => 'Foo', + ], + ], $html); + + $message = new IncomingMessage('', 'U0X12345', 'general'); + $driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', (Image::url('http://image.url/foo.png'))->title('title')), $message)); + } + /** @test */ public function it_can_reply_string_messages_for_outgoing_webhooks() {