From ead0d1b66adb271cdc661eafdad53570b826fe58 Mon Sep 17 00:00:00 2001 From: Laurence Date: Mon, 20 Feb 2017 12:14:27 +0000 Subject: [PATCH] Add image url options to Slack Notifications --- .../Channels/SlackWebhookChannel.php | 1 + .../Notifications/Messages/SlackMessage.php | 24 +++++++- .../NotificationSlackChannelTest.php | 60 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Notifications/Channels/SlackWebhookChannel.php b/src/Illuminate/Notifications/Channels/SlackWebhookChannel.php index 9900dd5896d5..c8186e8b65a9 100644 --- a/src/Illuminate/Notifications/Channels/SlackWebhookChannel.php +++ b/src/Illuminate/Notifications/Channels/SlackWebhookChannel.php @@ -57,6 +57,7 @@ protected function buildJsonPayload(SlackMessage $message) $optionalFields = array_filter([ 'username' => data_get($message, 'username'), 'icon_emoji' => data_get($message, 'icon'), + 'icon_url' => data_get($message, 'image'), 'channel' => data_get($message, 'channel'), ]); diff --git a/src/Illuminate/Notifications/Messages/SlackMessage.php b/src/Illuminate/Notifications/Messages/SlackMessage.php index 7fb97a22999a..d4a61be882b1 100644 --- a/src/Illuminate/Notifications/Messages/SlackMessage.php +++ b/src/Illuminate/Notifications/Messages/SlackMessage.php @@ -21,12 +21,19 @@ class SlackMessage public $username; /** - * The user icon for the message. + * The user emoji icon for the message. * * @var string|null */ public $icon; + /** + * The user image icon for the message. + * + * @var string|null + */ + public $image; + /** * The channel to send the message on. * @@ -92,7 +99,7 @@ public function error() } /** - * Set a custom user icon for the Slack message. + * Set a custom username and optional emoji icon for the Slack message. * * @param string $username * @param string|null $icon @@ -109,6 +116,19 @@ public function from($username, $icon = null) return $this; } + /** + * Set a custom image icon the message should use. + * + * @param string $channel + * @return $this + */ + public function image($image) + { + $this->image = $image; + + return $this; + } + /** * Set the Slack channel the message should be sent to. * diff --git a/tests/Notifications/NotificationSlackChannelTest.php b/tests/Notifications/NotificationSlackChannelTest.php index 43a0d7fa1187..3800c02745b6 100644 --- a/tests/Notifications/NotificationSlackChannelTest.php +++ b/tests/Notifications/NotificationSlackChannelTest.php @@ -65,6 +65,40 @@ public function testCorrectPayloadIsSentToSlack() ); } + public function testCorrectPayloadIsSentToSlackWithImageIcon() + { + $this->validatePayload( + new NotificationSlackChannelTestNotificationWithImageIcon, + [ + 'json' => [ + 'username' => 'Ghostbot', + 'icon_url' => 'http://example.com/image.png', + 'channel' => '#ghost-talk', + 'text' => 'Content', + 'attachments' => [ + [ + 'title' => 'Laravel', + 'title_link' => 'https://laravel.com', + 'text' => 'Attachment Content', + 'fallback' => 'Attachment Fallback', + 'fields' => [ + [ + 'title' => 'Project', + 'value' => 'Laravel', + 'short' => true, + ], + ], + 'mrkdwn_in' => ['text'], + 'footer' => 'Laravel', + 'footer_icon' => 'https://laravel.com/fake.png', + 'ts' => 1234567890, + ], + ], + ], + ] + ); + } + public function testCorrectPayloadWithoutOptionalFieldsIsSentToSlack() { $this->validatePayload( @@ -158,6 +192,32 @@ public function toSlack($notifiable) } } +class NotificationSlackChannelTestNotificationWithImageIcon extends Notification +{ + public function toSlack($notifiable) + { + return (new SlackMessage) + ->from('Ghostbot') + ->image('http://example.com/image.png') + ->to('#ghost-talk') + ->content('Content') + ->attachment(function ($attachment) { + $timestamp = Mockery::mock('Carbon\Carbon'); + $timestamp->shouldReceive('getTimestamp')->andReturn(1234567890); + $attachment->title('Laravel', 'https://laravel.com') + ->content('Attachment Content') + ->fallback('Attachment Fallback') + ->fields([ + 'Project' => 'Laravel', + ]) + ->footer('Laravel') + ->footerIcon('https://laravel.com/fake.png') + ->markdown(['text']) + ->timestamp($timestamp); + }); + } +} + class NotificationSlackChannelWithoutOptionalFieldsTestNotification extends Notification { public function toSlack($notifiable)