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