-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathSlackWebhookChannel.php
121 lines (110 loc) · 3.79 KB
/
SlackWebhookChannel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Illuminate\Notifications\Channels;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackAttachmentField;
class SlackWebhookChannel
{
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;
/**
* Create a new Slack channel instance.
*
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http)
{
$this->http = $http;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
if (! $url = $notifiable->routeNotificationFor('slack', $notification)) {
return;
}
$this->http->post($url, $this->buildJsonPayload(
$notification->toSlack($notifiable)
));
}
/**
* Build up a JSON payload for the Slack webhook.
*
* @param \Illuminate\Notifications\Messages\SlackMessage $message
* @return array
*/
protected function buildJsonPayload(SlackMessage $message)
{
$optionalFields = array_filter([
'channel' => data_get($message, 'channel'),
'icon_emoji' => data_get($message, 'icon'),
'icon_url' => data_get($message, 'image'),
'link_names' => data_get($message, 'linkNames'),
'unfurl_links' => data_get($message, 'unfurlLinks'),
'unfurl_media' => data_get($message, 'unfurlMedia'),
'username' => data_get($message, 'username'),
]);
return array_merge([
'json' => array_merge([
'text' => $message->content,
'attachments' => $this->attachments($message),
], $optionalFields),
], $message->http);
}
/**
* Format the message's attachments.
*
* @param \Illuminate\Notifications\Messages\SlackMessage $message
* @return array
*/
protected function attachments(SlackMessage $message)
{
return collect($message->attachments)->map(function ($attachment) use ($message) {
return array_filter([
'author_icon' => $attachment->authorIcon,
'author_link' => $attachment->authorLink,
'author_name' => $attachment->authorName,
'color' => $attachment->color ?: $message->color(),
'fallback' => $attachment->fallback,
'fields' => $this->fields($attachment),
'footer' => $attachment->footer,
'footer_icon' => $attachment->footerIcon,
'image_url' => $attachment->imageUrl,
'mrkdwn_in' => $attachment->markdown,
'pretext' => $attachment->pretext,
'text' => $attachment->content,
'thumb_url' => $attachment->thumbUrl,
'title' => $attachment->title,
'title_link' => $attachment->url,
'ts' => $attachment->timestamp,
]);
})->all();
}
/**
* Format the attachment's fields.
*
* @param \Illuminate\Notifications\Messages\SlackAttachment $attachment
* @return array
*/
protected function fields(SlackAttachment $attachment)
{
return collect($attachment->fields)->map(function ($value, $key) {
if ($value instanceof SlackAttachmentField) {
return $value->toArray();
}
return ['title' => $key, 'value' => $value, 'short' => true];
})->values()->all();
}
}