Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Allow Slack Notifications to use image urls #18011

Merged
merged 1 commit into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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