1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maknz\Slack; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use GuzzleHttp\Client as Guzzle; |
7
|
|
|
|
8
|
|
|
class Client |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The Slack incoming webhook endpoint. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $endpoint; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The default channel to send messages to. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $channel; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The default username to send messages as. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $username; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The default icon to send messages with. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $icon; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Whether to link names like @regan or leave |
40
|
|
|
* them as plain text. |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $link_names = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Whether Slack should unfurl text-based URLs. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $unfurl_links = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Whether Slack should unfurl media URLs. |
55
|
|
|
* |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
protected $unfurl_media = true; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Whether message text should be formatted with Slack's |
62
|
|
|
* Markdown-like language. |
63
|
8 |
|
* |
64
|
|
|
* @var bool |
65
|
8 |
|
*/ |
66
|
8 |
|
protected $allow_markdown = true; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The attachment fields which should be formatted with |
70
|
|
|
* Slack's Markdown-like language. |
71
|
|
|
* |
72
|
8 |
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected $markdown_in_attachments = []; |
75
|
|
|
|
76
|
8 |
|
/** |
77
|
8 |
|
* The Guzzle HTTP client instance. |
78
|
|
|
* |
79
|
|
|
* @var \GuzzleHttp\Client |
80
|
|
|
*/ |
81
|
|
|
protected $guzzle; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Instantiate a new Client. |
85
|
|
|
* |
86
|
|
|
* @param string $endpoint |
87
|
|
|
* @param array $attributes |
88
|
|
|
* @param \GuzzleHttp\Client|null $guzzle |
89
|
|
|
* |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
* @throws \RuntimeException |
92
|
|
|
*/ |
93
|
9 |
|
public function __construct($endpoint, array $attributes = [], Guzzle $guzzle = null) |
94
|
|
|
{ |
95
|
9 |
|
$this->endpoint = $endpoint; |
96
|
|
|
|
97
|
9 |
|
if (isset($attributes['channel'])) { |
98
|
5 |
|
$this->setDefaultChannel($attributes['channel']); |
99
|
|
|
} |
100
|
|
|
|
101
|
9 |
|
if (isset($attributes['username'])) { |
102
|
5 |
|
$this->setDefaultUsername($attributes['username']); |
103
|
|
|
} |
104
|
|
|
|
105
|
9 |
|
if (isset($attributes['icon'])) { |
106
|
2 |
|
$this->setDefaultIcon($attributes['icon']); |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
if (isset($attributes['link_names'])) { |
110
|
1 |
|
$this->setLinkNames($attributes['link_names']); |
111
|
|
|
} |
112
|
|
|
|
113
|
9 |
|
if (isset($attributes['unfurl_links'])) { |
114
|
1 |
|
$this->setUnfurlLinks($attributes['unfurl_links']); |
115
|
|
|
} |
116
|
|
|
|
117
|
9 |
|
if (isset($attributes['unfurl_media'])) { |
118
|
1 |
|
$this->setUnfurlMedia($attributes['unfurl_media']); |
119
|
|
|
} |
120
|
|
|
|
121
|
9 |
|
if (isset($attributes['allow_markdown'])) { |
122
|
1 |
|
$this->setAllowMarkdown($attributes['allow_markdown']); |
123
|
|
|
} |
124
|
|
|
|
125
|
9 |
|
if (isset($attributes['markdown_in_attachments'])) { |
126
|
1 |
|
$this->setMarkdownInAttachments($attributes['markdown_in_attachments']); |
127
|
|
|
} |
128
|
|
|
|
129
|
9 |
|
$this->guzzle = $guzzle ?: new Guzzle; |
130
|
9 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Pass any unhandled methods through to a new Message |
134
|
|
|
* instance. |
135
|
|
|
* |
136
|
|
|
* @param string $name The name of the method |
137
|
|
|
* @param array $arguments The method arguments |
138
|
|
|
* @return \Maknz\Slack\Message |
139
|
|
|
*/ |
140
|
3 |
|
public function __call($name, $arguments) |
141
|
|
|
{ |
142
|
3 |
|
return call_user_func_array([$this->createMessage(), $name], $arguments); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the Slack endpoint. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
1 |
|
public function getEndpoint() |
151
|
|
|
{ |
152
|
1 |
|
return $this->endpoint; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the Slack endpoint. |
157
|
|
|
* |
158
|
|
|
* @param string $endpoint |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function setEndpoint($endpoint) |
162
|
|
|
{ |
163
|
|
|
$this->endpoint = $endpoint; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the default channel messages will be created for. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
8 |
|
public function getDefaultChannel() |
172
|
|
|
{ |
173
|
8 |
|
return $this->channel; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set the default channel messages will be created for. |
178
|
|
|
* |
179
|
|
|
* @param string $channel |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
5 |
|
public function setDefaultChannel($channel) |
183
|
|
|
{ |
184
|
5 |
|
$this->channel = $channel; |
185
|
5 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the default username messages will be created for. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
8 |
|
public function getDefaultUsername() |
193
|
|
|
{ |
194
|
8 |
|
return $this->username; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set the default username messages will be created for. |
199
|
|
|
* |
200
|
|
|
* @param string $username |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
5 |
|
public function setDefaultUsername($username) |
204
|
|
|
{ |
205
|
5 |
|
$this->username = $username; |
206
|
5 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the default icon messages will be created with. |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
8 |
|
public function getDefaultIcon() |
214
|
|
|
{ |
215
|
8 |
|
return $this->icon; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set the default icon messages will be created with. |
220
|
|
|
* |
221
|
|
|
* @param string $icon |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
2 |
|
public function setDefaultIcon($icon) |
225
|
|
|
{ |
226
|
2 |
|
$this->icon = $icon; |
227
|
2 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get whether messages sent will have names (like @regan) |
231
|
|
|
* will be converted into links. |
232
|
8 |
|
* |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
14 |
|
public function getLinkNames() |
236
|
|
|
{ |
237
|
6 |
|
return $this->link_names; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set whether messages sent will have names (like @regan) |
242
|
|
|
* will be converted into links. |
243
|
8 |
|
* |
244
|
|
|
* @param bool $value |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
9 |
|
public function setLinkNames($value) |
248
|
|
|
{ |
249
|
9 |
|
$this->link_names = (bool) $value; |
250
|
1 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get whether text links should be unfurled. |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
14 |
|
public function getUnfurlLinks() |
258
|
|
|
{ |
259
|
6 |
|
return $this->unfurl_links; |
260
|
|
|
} |
261
|
8 |
|
|
262
|
|
|
/** |
263
|
|
|
* Set whether text links should be unfurled. |
264
|
|
|
* |
265
|
8 |
|
* @param bool $value |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
1 |
|
public function setUnfurlLinks($value) |
269
|
|
|
{ |
270
|
9 |
|
$this->unfurl_links = (bool) $value; |
271
|
1 |
|
} |
272
|
8 |
|
|
273
|
|
|
/** |
274
|
|
|
* Get whether media links should be unfurled. |
275
|
|
|
* |
276
|
|
|
* @return bool |
277
|
8 |
|
*/ |
278
|
14 |
|
public function getUnfurlMedia() |
279
|
|
|
{ |
280
|
6 |
|
return $this->unfurl_media; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Set whether media links should be unfurled. |
285
|
|
|
* |
286
|
|
|
* @param bool $value |
287
|
|
|
* @return void |
288
|
8 |
|
*/ |
289
|
1 |
|
public function setUnfurlMedia($value) |
290
|
|
|
{ |
291
|
1 |
|
$this->unfurl_media = (bool) $value; |
292
|
1 |
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get whether message text should be formatted with |
296
|
|
|
* Slack's Markdown-like language. |
297
|
|
|
* |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
8 |
|
public function getAllowMarkdown() |
301
|
|
|
{ |
302
|
8 |
|
return $this->allow_markdown; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Set whether message text should be formatted with |
307
|
|
|
* Slack's Markdown-like language. |
308
|
|
|
* |
309
|
|
|
* @param bool $value |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
1 |
|
public function setAllowMarkdown($value) |
313
|
|
|
{ |
314
|
1 |
|
$this->allow_markdown = (bool) $value; |
315
|
1 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get the attachment fields which should be formatted |
319
|
|
|
* in Slack's Markdown-like language. |
320
|
|
|
* |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
8 |
|
public function getMarkdownInAttachments() |
324
|
|
|
{ |
325
|
8 |
|
return $this->markdown_in_attachments; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Set the attachment fields which should be formatted |
330
|
|
|
* in Slack's Markdown-like language. |
331
|
|
|
* |
332
|
|
|
* @param array $fields |
333
|
|
|
* @return void |
334
|
|
|
*/ |
335
|
1 |
|
public function setMarkdownInAttachments(array $fields) |
336
|
|
|
{ |
337
|
1 |
|
$this->markdown_in_attachments = $fields; |
338
|
1 |
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Create a new message with defaults. |
342
|
|
|
* |
343
|
|
|
* @return \Maknz\Slack\Message |
344
|
|
|
*/ |
345
|
7 |
|
public function createMessage() |
346
|
|
|
{ |
347
|
7 |
|
$message = new Message($this); |
348
|
|
|
|
349
|
7 |
|
$message->setChannel($this->getDefaultChannel()); |
350
|
|
|
|
351
|
7 |
|
$message->setUsername($this->getDefaultUsername()); |
352
|
|
|
|
353
|
7 |
|
$message->setIcon($this->getDefaultIcon()); |
354
|
|
|
|
355
|
7 |
|
$message->setAllowMarkdown($this->getAllowMarkdown()); |
356
|
|
|
|
357
|
7 |
|
$message->setMarkdownInAttachments($this->getMarkdownInAttachments()); |
358
|
|
|
|
359
|
7 |
|
return $message; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Send a message. |
364
|
|
|
* |
365
|
|
|
* @param \Maknz\Slack\Message $message |
366
|
|
|
* |
367
|
|
|
* @throws \RuntimeException |
368
|
|
|
*/ |
369
|
1 |
|
public function sendMessage(Message $message) |
370
|
|
|
{ |
371
|
1 |
|
$payload = $this->preparePayload($message); |
372
|
|
|
|
373
|
1 |
|
$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE); |
374
|
|
|
|
375
|
1 |
|
if ($encoded === false) { |
376
|
1 |
|
throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg())); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$this->guzzle->post($this->endpoint, ['body' => $encoded]); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Prepares the payload to be sent to the webhook. |
384
|
|
|
* |
385
|
|
|
* @param \Maknz\Slack\Message $message The message to send |
386
|
|
|
* @return array |
387
|
|
|
*/ |
388
|
5 |
|
public function preparePayload(Message $message) |
389
|
|
|
{ |
390
|
|
|
$payload = [ |
391
|
5 |
|
'text' => $message->getText(), |
392
|
5 |
|
'channel' => $message->getChannel(), |
393
|
5 |
|
'username' => $message->getUsername(), |
394
|
5 |
|
'link_names' => $this->getLinkNames() ? 1 : 0, |
395
|
5 |
|
'unfurl_links' => $this->getUnfurlLinks(), |
396
|
5 |
|
'unfurl_media' => $this->getUnfurlMedia(), |
397
|
5 |
|
'mrkdwn' => $message->getAllowMarkdown(), |
398
|
|
|
]; |
399
|
|
|
|
400
|
5 |
|
if ($icon = $message->getIcon()) { |
401
|
|
|
$payload[$message->getIconType()] = $icon; |
402
|
|
|
} |
403
|
|
|
|
404
|
5 |
|
$payload['attachments'] = $this->getAttachmentsAsArrays($message); |
405
|
|
|
|
406
|
5 |
|
return $payload; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Get the attachments in array form. |
411
|
|
|
* |
412
|
|
|
* @param \Maknz\Slack\Message $message |
413
|
|
|
* |
414
|
|
|
* @return array |
415
|
|
|
*/ |
416
|
5 |
|
protected function getAttachmentsAsArrays(Message $message) |
417
|
|
|
{ |
418
|
5 |
|
$attachments = []; |
419
|
|
|
|
420
|
5 |
|
foreach ($message->getAttachments() as $attachment) { |
421
|
3 |
|
$attachments[] = $attachment->toArray(); |
422
|
|
|
} |
423
|
|
|
|
424
|
5 |
|
return $attachments; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|