-
Notifications
You must be signed in to change notification settings - Fork 61
/
SlackMessage.php
273 lines (231 loc) · 4.84 KB
/
SlackMessage.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
namespace Illuminate\Notifications\Messages;
use Closure;
class SlackMessage
{
/**
* The "level" of the notification (info, success, warning, error).
*
* @var string
*/
public $level = 'info';
/**
* The username to send the message from.
*
* @var string|null
*/
public $username;
/**
* 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.
*
* @var string|null
*/
public $channel;
/**
* The text content of the message.
*
* @var string
*/
public $content;
/**
* Indicates if channel names and usernames should be linked.
*
* @var bool
*/
public $linkNames = 0;
/**
* Indicates if you want a preview of links inlined in the message.
*
* @var bool
*/
public $unfurlLinks;
/**
* Indicates if you want a preview of links to media inlined in the message.
*
* @var bool
*/
public $unfurlMedia;
/**
* The message's attachments.
*
* @var array
*/
public $attachments = [];
/**
* Additional request options for the Guzzle HTTP client.
*
* @var array
*/
public $http = [];
/**
* Indicate that the notification gives information about an operation.
*
* @return $this
*/
public function info()
{
$this->level = 'info';
return $this;
}
/**
* Indicate that the notification gives information about a successful operation.
*
* @return $this
*/
public function success()
{
$this->level = 'success';
return $this;
}
/**
* Indicate that the notification gives information about a warning.
*
* @return $this
*/
public function warning()
{
$this->level = 'warning';
return $this;
}
/**
* Indicate that the notification gives information about an error.
*
* @return $this
*/
public function error()
{
$this->level = 'error';
return $this;
}
/**
* Set a custom username and optional emoji icon for the Slack message.
*
* @param string $username
* @param string|null $icon
* @return $this
*/
public function from($username, $icon = null)
{
$this->username = $username;
if (! is_null($icon)) {
$this->icon = $icon;
}
return $this;
}
/**
* Set a custom image icon the message should use.
*
* @param string $image
* @return $this
*/
public function image($image)
{
$this->image = $image;
return $this;
}
/**
* Set the Slack channel the message should be sent to.
*
* @param string $channel
* @return $this
*/
public function to($channel)
{
$this->channel = $channel;
return $this;
}
/**
* Set the content of the Slack message.
*
* @param string $content
* @return $this
*/
public function content($content)
{
$this->content = $content;
return $this;
}
/**
* Define an attachment for the message.
*
* @param \Closure $callback
* @return $this
*/
public function attachment(Closure $callback)
{
$this->attachments[] = $attachment = new SlackAttachment;
$callback($attachment);
return $this;
}
/**
* Get the color for the message.
*
* @return string|null
*/
public function color()
{
switch ($this->level) {
case 'success':
return 'good';
case 'error':
return 'danger';
case 'warning':
return 'warning';
}
}
/**
* Find and link channel names and usernames.
*
* @return $this
*/
public function linkNames()
{
$this->linkNames = 1;
return $this;
}
/**
* Unfurl links to rich display.
*
* @param string $unfurl
* @return $this
*/
public function unfurlLinks($unfurl)
{
$this->unfurlLinks = $unfurl;
return $this;
}
/**
* Unfurl media to rich display.
*
* @param string $unfurl
* @return $this
*/
public function unfurlMedia($unfurl)
{
$this->unfurlMedia = $unfurl;
return $this;
}
/**
* Set additional request options for the Guzzle HTTP client.
*
* @param array $options
* @return $this
*/
public function http(array $options)
{
$this->http = $options;
return $this;
}
}