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