1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
class Message |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Reference to the Slack client responsible for sending |
10
|
|
|
* the message. |
11
|
|
|
* |
12
|
|
|
* @var \Maknz\Slack\Client |
13
|
|
|
*/ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The text to send with the message. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $text; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The channel the message should be sent to. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $channel; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The username the message should be sent as. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $username; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The URL to the icon to use. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $icon; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The type of icon we are using. |
46
|
|
|
* |
47
|
|
|
* @var string (one of self::ICON_* constants) |
48
|
|
|
*/ |
49
|
|
|
protected $iconType; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Whether the message text should be interpreted in Slack's |
53
|
|
|
* Markdown-like language. |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $allow_markdown = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The attachment fields which should be formatted with |
61
|
|
|
* Slack's Markdown-like language. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $markdown_in_attachments = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* An array of attachments to send. |
69
|
|
|
* |
70
|
|
|
* @var Attachment[] |
71
|
|
|
*/ |
72
|
|
|
protected $attachments = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* An array of blocks to send. |
76
|
|
|
* |
77
|
|
|
* @var Block[] |
78
|
|
|
*/ |
79
|
|
|
protected $blocks = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
const ICON_TYPE_URL = 'icon_url'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
const ICON_TYPE_EMOJI = 'icon_emoji'; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Instantiate a new Message. |
93
|
|
|
* |
94
|
|
|
* @param \Maknz\Slack\Client $client |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
21 |
|
public function __construct(Client $client) |
99
|
|
|
{ |
100
|
21 |
|
$this->client = $client; |
101
|
21 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the message text. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
7 |
|
public function getText() |
109
|
|
|
{ |
110
|
7 |
|
return $this->text; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the message text. |
115
|
|
|
* |
116
|
|
|
* @param string $text |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
7 |
|
public function setText($text) |
121
|
|
|
{ |
122
|
7 |
|
$this->text = $text; |
123
|
|
|
|
124
|
7 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the channel we will post to. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
10 |
|
public function getChannel() |
133
|
|
|
{ |
134
|
10 |
|
return $this->channel; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the channel we will post to. |
139
|
|
|
* |
140
|
|
|
* @param string $channel |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
10 |
|
public function setChannel($channel) |
145
|
|
|
{ |
146
|
10 |
|
$this->channel = $channel; |
147
|
|
|
|
148
|
10 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the username we will post as. |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
9 |
|
public function getUsername() |
157
|
|
|
{ |
158
|
9 |
|
return $this->username; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the username we will post as. |
163
|
|
|
* |
164
|
|
|
* @param string $username |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
10 |
|
public function setUsername($username) |
169
|
|
|
{ |
170
|
10 |
|
$this->username = $username; |
171
|
|
|
|
172
|
10 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the icon (either URL or emoji) we will post as. |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
9 |
|
public function getIcon() |
181
|
|
|
{ |
182
|
9 |
|
return $this->icon; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the icon (either URL or emoji) we will post as. |
187
|
|
|
* |
188
|
|
|
* @param string $icon |
189
|
|
|
* |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
10 |
|
public function setIcon($icon) |
193
|
|
|
{ |
194
|
10 |
|
if ($icon == null) { |
195
|
7 |
|
$this->icon = $this->iconType = null; |
196
|
|
|
|
197
|
7 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
3 |
|
if (mb_substr($icon, 0, 1) == ':' && mb_substr($icon, mb_strlen($icon) - 1, 1) == ':') { |
201
|
2 |
|
$this->iconType = self::ICON_TYPE_EMOJI; |
202
|
|
|
} else { |
203
|
1 |
|
$this->iconType = self::ICON_TYPE_URL; |
204
|
|
|
} |
205
|
|
|
|
206
|
3 |
|
$this->icon = $icon; |
207
|
|
|
|
208
|
3 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the icon type being used, if an icon is set. |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
2 |
|
public function getIconType() |
217
|
|
|
{ |
218
|
2 |
|
return $this->iconType; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get whether message text should be formatted with |
223
|
|
|
* Slack's Markdown-like language. |
224
|
|
|
* |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
6 |
|
public function getAllowMarkdown() |
228
|
|
|
{ |
229
|
6 |
|
return $this->allow_markdown; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set whether message text should be formatted with |
234
|
|
|
* Slack's Markdown-like language. |
235
|
|
|
* |
236
|
|
|
* @param bool $value |
237
|
|
|
* |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
8 |
|
public function setAllowMarkdown($value) |
241
|
|
|
{ |
242
|
8 |
|
$this->allow_markdown = (bool)$value; |
243
|
|
|
|
244
|
8 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Enable Markdown formatting for the message. |
249
|
|
|
* |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
|
|
public function enableMarkdown() |
253
|
|
|
{ |
254
|
|
|
$this->setAllowMarkdown(true); |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Disable Markdown formatting for the message. |
261
|
|
|
* |
262
|
|
|
* @return $this |
263
|
|
|
*/ |
264
|
|
|
public function disableMarkdown() |
265
|
|
|
{ |
266
|
|
|
$this->setAllowMarkdown(false); |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get the attachment fields which should be formatted |
273
|
|
|
* in Slack's Markdown-like language. |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
2 |
|
public function getMarkdownInAttachments() |
278
|
|
|
{ |
279
|
2 |
|
return $this->markdown_in_attachments; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set the attachment fields which should be formatted |
284
|
|
|
* in Slack's Markdown-like language. |
285
|
|
|
* |
286
|
|
|
* @param array $fields |
287
|
|
|
* |
288
|
|
|
* @return $this |
289
|
|
|
*/ |
290
|
8 |
|
public function setMarkdownInAttachments(array $fields) |
291
|
|
|
{ |
292
|
8 |
|
$this->markdown_in_attachments = $fields; |
293
|
|
|
|
294
|
8 |
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Change the name of the user the post will be made as. |
299
|
|
|
* |
300
|
|
|
* @param string $username |
301
|
|
|
* |
302
|
|
|
* @return $this |
303
|
|
|
*/ |
304
|
2 |
|
public function from($username) |
305
|
|
|
{ |
306
|
2 |
|
$this->setUsername($username); |
307
|
|
|
|
308
|
2 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Change the channel the post will be made to. |
313
|
|
|
* |
314
|
|
|
* @param string $channel |
315
|
|
|
* |
316
|
|
|
* @return $this |
317
|
|
|
*/ |
318
|
3 |
|
public function to($channel) |
319
|
|
|
{ |
320
|
3 |
|
$this->setChannel($channel); |
321
|
|
|
|
322
|
3 |
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Chainable method for setting the icon. |
327
|
|
|
* |
328
|
|
|
* @param string $icon |
329
|
|
|
* |
330
|
|
|
* @return $this |
331
|
|
|
*/ |
332
|
|
|
public function withIcon($icon) |
333
|
|
|
{ |
334
|
|
|
$this->setIcon($icon); |
335
|
|
|
|
336
|
|
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Add an attachment to the message. |
341
|
|
|
* |
342
|
|
|
* @param mixed $attachment |
343
|
|
|
* |
344
|
|
|
* @return $this |
345
|
|
|
* |
346
|
|
|
* @throws \InvalidArgumentException |
347
|
|
|
*/ |
348
|
7 |
|
public function attach($attachment) |
349
|
|
|
{ |
350
|
7 |
|
if ($attachment instanceof Attachment) { |
351
|
6 |
|
$this->attachments[] = $attachment; |
352
|
|
|
|
353
|
6 |
|
return $this; |
354
|
2 |
|
} elseif (is_array($attachment)) { |
355
|
2 |
|
$attachmentObject = new Attachment($attachment); |
356
|
|
|
|
357
|
2 |
|
if ( ! isset($attachment['mrkdwn_in'])) { |
358
|
2 |
|
$attachmentObject->setMarkdownFields($this->getMarkdownInAttachments()); |
359
|
|
|
} |
360
|
|
|
|
361
|
2 |
|
$this->attachments[] = $attachmentObject; |
362
|
|
|
|
363
|
2 |
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
throw new InvalidArgumentException('Attachment must be an instance of Maknz\\Slack\\Attachment or a keyed array'); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Get the attachments for the message. |
371
|
|
|
* |
372
|
|
|
* @return Attachment[] |
373
|
|
|
*/ |
374
|
9 |
|
public function getAttachments() |
375
|
|
|
{ |
376
|
9 |
|
return $this->attachments; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Set the attachments for the message. |
381
|
|
|
* |
382
|
|
|
* @param array $attachments |
383
|
|
|
* |
384
|
|
|
* @return $this |
385
|
|
|
* |
386
|
|
|
* @throws \InvalidArgumentException |
387
|
|
|
*/ |
388
|
1 |
|
public function setAttachments(array $attachments) |
389
|
|
|
{ |
390
|
1 |
|
$this->clearAttachments(); |
391
|
|
|
|
392
|
1 |
|
foreach ($attachments as $attachment) { |
393
|
1 |
|
$this->attach($attachment); |
394
|
|
|
} |
395
|
|
|
|
396
|
1 |
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Remove all attachments for the message. |
401
|
|
|
* |
402
|
|
|
* @return $this |
403
|
|
|
*/ |
404
|
1 |
|
public function clearAttachments() |
405
|
|
|
{ |
406
|
1 |
|
$this->attachments = []; |
407
|
|
|
|
408
|
1 |
|
return $this; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Add a block to the message. |
413
|
|
|
* |
414
|
|
|
* @param mixed $block |
415
|
|
|
* |
416
|
|
|
* @return $this |
417
|
|
|
* |
418
|
|
|
* @throws \InvalidArgumentException |
419
|
|
|
*/ |
420
|
2 |
|
public function withBlock($block) |
421
|
|
|
{ |
422
|
2 |
|
if ($block instanceof Block) { |
423
|
1 |
|
$this->blocks[] = $block; |
424
|
|
|
|
425
|
1 |
|
return $this; |
426
|
1 |
|
} elseif (is_array($block)) { |
427
|
1 |
|
$blockObject = Block::factory($block); |
428
|
1 |
|
$this->blocks[] = $blockObject; |
429
|
|
|
|
430
|
1 |
|
return $this; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
throw new InvalidArgumentException('Block must be an instance of Maknz\\Slack\\Block or a keyed array'); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Get the blocks for the message. |
438
|
|
|
* |
439
|
|
|
* @return Block[] |
440
|
|
|
*/ |
441
|
7 |
|
public function getBlocks() |
442
|
|
|
{ |
443
|
7 |
|
return $this->blocks; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Get the blocks for the message in array format. |
448
|
|
|
* |
449
|
|
|
* @return array |
450
|
|
|
*/ |
451
|
1 |
|
public function getBlocksAsArrays() |
452
|
|
|
{ |
453
|
1 |
|
$blocks = []; |
454
|
|
|
|
455
|
1 |
|
foreach ($this->getBlocks() as $block) { |
456
|
1 |
|
$blocks[] = $block->toArray(); |
457
|
|
|
} |
458
|
|
|
|
459
|
1 |
|
return $blocks; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Set the blocks for the message. |
464
|
|
|
* |
465
|
|
|
* @param array $blocks |
466
|
|
|
* |
467
|
|
|
* @return $this |
468
|
|
|
* |
469
|
|
|
* @throws \InvalidArgumentException |
470
|
|
|
*/ |
471
|
|
|
public function setBlocks(array $blocks) |
472
|
|
|
{ |
473
|
|
|
$this->clearBlocks(); |
474
|
|
|
|
475
|
|
|
foreach ($blocks as $block) { |
476
|
|
|
$this->withBlock($block); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
return $this; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Remove all blocks for the message. |
484
|
|
|
* |
485
|
|
|
* @return $this |
486
|
|
|
*/ |
487
|
|
|
public function clearBlocks() |
488
|
|
|
{ |
489
|
|
|
$this->blocks = []; |
490
|
|
|
|
491
|
|
|
return $this; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Send the message. |
496
|
|
|
* |
497
|
|
|
* @deprecated |
498
|
|
|
* |
499
|
|
|
* @param string $text The text to send |
500
|
|
|
* |
501
|
|
|
* @return \Maknz\Slack\Message |
502
|
|
|
* |
503
|
|
|
* @throws \RuntimeException |
504
|
|
|
*/ |
505
|
1 |
|
public function send($text = null) |
506
|
|
|
{ |
507
|
1 |
|
if ($text !== null) { |
508
|
1 |
|
$this->setText($text); |
509
|
|
|
} |
510
|
|
|
|
511
|
1 |
|
$this->client->sendMessage($this); |
512
|
|
|
|
513
|
|
|
return $this; |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|