1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace NotificationChannels\RocketChat; |
6
|
|
|
|
7
|
|
|
class RocketChatMessage |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* RocketChat channel id. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $channel = ''; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A user or app access token. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $from = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The text content of the message. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $content = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The alias name of the message. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $alias = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The avatar emoji of the message. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $emoji = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The avatar image of the message. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $avatar = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Attachments of the message. |
53
|
|
|
* |
54
|
|
|
* @var RocketChatAttachment[] |
55
|
|
|
*/ |
56
|
|
|
public $attachments = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create a new instance of RocketChatMessage. |
60
|
|
|
* |
61
|
|
|
* @param string $content |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
5 |
|
public static function create($content = ''): self |
65
|
|
|
{ |
66
|
5 |
|
return new static($content); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new instance of RocketChatMessage. |
71
|
|
|
* |
72
|
|
|
* @param string $content |
73
|
|
|
*/ |
74
|
16 |
|
public function __construct($content = '') |
75
|
|
|
{ |
76
|
16 |
|
$this->content($content); |
77
|
16 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the sender's access token. |
81
|
|
|
* |
82
|
|
|
* @param string $accessToken |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
4 |
|
public function from($accessToken): self |
86
|
|
|
{ |
87
|
4 |
|
$this->from = $accessToken; |
88
|
|
|
|
89
|
4 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the RocketChat channel the message should be sent to. |
94
|
|
|
* |
95
|
|
|
* @param string $channel |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
4 |
|
public function to($channel): self |
99
|
|
|
{ |
100
|
4 |
|
$this->channel = $channel; |
101
|
|
|
|
102
|
4 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the sender's alias. |
107
|
|
|
* |
108
|
|
|
* @param string $alias |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
1 |
|
public function alias(string $alias): self |
112
|
|
|
{ |
113
|
1 |
|
$this->alias = $alias; |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the sender's emoji. |
120
|
|
|
* |
121
|
|
|
* @param string $emoji |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
1 |
|
public function emoji(string $emoji): self |
125
|
|
|
{ |
126
|
1 |
|
$this->emoji = $emoji; |
127
|
|
|
|
128
|
1 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the sender's avatar. |
133
|
|
|
* |
134
|
|
|
* @param string $avatar |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
1 |
|
public function avatar(string $avatar): self |
138
|
|
|
{ |
139
|
1 |
|
$this->avatar = $avatar; |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the content of the RocketChat message. Supports GitHub flavoured markdown. |
146
|
|
|
* |
147
|
|
|
* @param string $content |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
16 |
|
public function content($content): self |
151
|
|
|
{ |
152
|
16 |
|
$this->content = $content; |
153
|
|
|
|
154
|
16 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Add an attachment to the message. |
159
|
|
|
* |
160
|
|
|
* @param array|RocketChatAttachment $attachment |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
4 |
|
public function attachment($attachment): self |
165
|
|
|
{ |
166
|
4 |
|
if (! ($attachment instanceof RocketChatAttachment)) { |
167
|
1 |
|
$attachment = new RocketChatAttachment($attachment); |
168
|
|
|
} |
169
|
|
|
|
170
|
4 |
|
$this->attachments[] = $attachment; |
171
|
|
|
|
172
|
4 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Add multiple attachments to the message. |
177
|
|
|
* |
178
|
|
|
* @param array $attachments |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
2 |
|
public function attachments(array $attachments): self |
182
|
|
|
{ |
183
|
2 |
|
foreach ($attachments as $attachment) { |
184
|
2 |
|
$this->attachment($attachment); |
185
|
|
|
} |
186
|
|
|
|
187
|
2 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* clear all attachments. |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
1 |
|
public function clearAttachments(): self |
196
|
|
|
{ |
197
|
1 |
|
$this->attachments = []; |
198
|
|
|
|
199
|
1 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get an array representation of the RocketChatMessage. |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
2 |
|
public function toArray(): array |
208
|
|
|
{ |
209
|
2 |
|
$attachments = []; |
210
|
2 |
|
foreach ($this->attachments as $attachment) { |
211
|
|
|
$attachments[] = $attachment->toArray(); |
212
|
|
|
} |
213
|
|
|
|
214
|
2 |
|
$message = array_filter([ |
215
|
2 |
|
'text' => $this->content, |
216
|
2 |
|
'channel' => $this->channel, |
217
|
2 |
|
'alias' => $this->alias, |
218
|
2 |
|
'emoji' => $this->emoji, |
219
|
2 |
|
'avatar' => $this->avatar, |
220
|
2 |
|
'attachments' => $attachments, |
221
|
|
|
]); |
222
|
|
|
|
223
|
2 |
|
return $message; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|