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