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