Skip to content

Commit

Permalink
Add image url options to Slack Notifications (#18011)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurencei authored and taylorotwell committed Feb 21, 2017
1 parent 01d1460 commit e80a34e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);

Expand Down
24 changes: 22 additions & 2 deletions src/Illuminate/Notifications/Messages/SlackMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/Notifications/NotificationSlackChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e80a34e

Please sign in to comment.