|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Botonomous; |
|
4
|
|
|
|
|
5
|
|
|
use Botonomous\client\ApiClient; |
|
6
|
|
|
use Botonomous\utility\LoggerUtility; |
|
7
|
|
|
use Botonomous\utility\MessageUtility; |
|
8
|
|
|
use /* @noinspection PhpUndefinedClassInspection */ |
|
9
|
|
|
GuzzleHttp\Client; |
|
10
|
|
|
use /* @noinspection PhpUndefinedClassInspection */ |
|
11
|
|
|
GuzzleHttp\Psr7\Request; |
|
12
|
|
|
|
|
13
|
|
|
class Sender |
|
14
|
|
|
{ |
|
15
|
|
|
private $slackbot; |
|
16
|
|
|
private $loggerUtility; |
|
17
|
|
|
private $messageUtility; |
|
18
|
|
|
private $config; |
|
19
|
|
|
private $apiClient; |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Sender constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param AbstractBot $slackbot |
|
26
|
|
|
*/ |
|
27
|
9 |
|
public function __construct(AbstractBot $slackbot) |
|
28
|
|
|
{ |
|
29
|
9 |
|
$this->setSlackbot($slackbot); |
|
30
|
9 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return Slackbot |
|
34
|
|
|
*/ |
|
35
|
7 |
|
public function getSlackbot() |
|
36
|
|
|
{ |
|
37
|
7 |
|
return $this->slackbot; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param AbstractBot $slackbot |
|
42
|
|
|
*/ |
|
43
|
9 |
|
public function setSlackbot(AbstractBot $slackbot) |
|
44
|
|
|
{ |
|
45
|
9 |
|
$this->slackbot = $slackbot; |
|
46
|
9 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Final endpoint for the response. |
|
50
|
|
|
* |
|
51
|
|
|
* @param $channel |
|
52
|
|
|
* @param $text |
|
53
|
|
|
* @param $attachments |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \Exception |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
7 |
|
public function send($text, $channel = null, $attachments = null) |
|
60
|
|
|
{ |
|
61
|
|
|
// @codeCoverageIgnoreStart |
|
62
|
|
|
if ($this->getSlackbot()->getListener()->isThisBot() !== false) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
// @codeCoverageIgnoreEnd |
|
66
|
|
|
|
|
67
|
7 |
|
$responseType = $this->getResponseType(); |
|
68
|
|
|
|
|
69
|
7 |
|
if (empty($channel)) { |
|
70
|
3 |
|
$channel = $this->getSlackbot()->getListener()->getChannelId(); |
|
71
|
|
|
|
|
72
|
3 |
|
if (empty($channel)) { |
|
73
|
3 |
|
$channel = $this->getConfig()->get('channel'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$data = [ |
|
78
|
7 |
|
'text' => $text, |
|
79
|
7 |
|
'channel' => $channel, |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
7 |
|
if ($attachments !== null) { |
|
83
|
1 |
|
$data['attachments'] = json_encode($attachments); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
7 |
|
$this->getLoggerUtility()->logChat(__METHOD__, $text, $channel); |
|
87
|
|
|
|
|
88
|
7 |
|
if ($responseType === 'slack') { |
|
89
|
1 |
|
$this->respondToSlack($data); |
|
90
|
6 |
|
} elseif ($responseType === 'slashCommand') { |
|
91
|
|
|
$this->respondToSlashCommand($text); |
|
92
|
6 |
|
} elseif ($responseType === 'json') { |
|
93
|
6 |
|
$this->respondAsJSON($data); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
7 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $response |
|
101
|
|
|
*/ |
|
102
|
|
|
private function respondToSlashCommand($response) |
|
103
|
|
|
{ |
|
104
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
105
|
|
|
$request = new Request( |
|
106
|
|
|
'POST', |
|
107
|
|
|
$this->getSlackbot()->getRequest('response_url'), |
|
108
|
|
|
['Content-Type' => 'application/json'], |
|
109
|
|
|
json_encode([ |
|
110
|
|
|
'text' => $response, |
|
111
|
|
|
'response_type' => 'in_channel', |
|
112
|
|
|
]) |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
/* @noinspection PhpUndefinedClassInspection */ |
|
116
|
|
|
$this->getClient()->send($request); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $data |
|
121
|
|
|
*/ |
|
122
|
1 |
|
private function respondToSlack($data) |
|
123
|
|
|
{ |
|
124
|
1 |
|
$this->getApiClient()->chatPostMessage($data); |
|
125
|
1 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $data |
|
129
|
|
|
*/ |
|
130
|
6 |
|
private function respondAsJSON($data) |
|
131
|
|
|
{ |
|
132
|
|
|
// headers_sent is used to avoid issue in the test |
|
133
|
6 |
|
if (!headers_sent()) { |
|
134
|
6 |
|
header('Content-type:application/json;charset=utf-8'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
6 |
|
echo json_encode($data); |
|
138
|
6 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return LoggerUtility |
|
142
|
|
|
*/ |
|
143
|
7 |
|
public function getLoggerUtility() |
|
144
|
|
|
{ |
|
145
|
7 |
|
if (!isset($this->loggerUtility)) { |
|
146
|
7 |
|
$this->setLoggerUtility(new LoggerUtility()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
7 |
|
return $this->loggerUtility; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param LoggerUtility $loggerUtility |
|
154
|
|
|
*/ |
|
155
|
7 |
|
public function setLoggerUtility(LoggerUtility $loggerUtility) |
|
156
|
|
|
{ |
|
157
|
7 |
|
$this->loggerUtility = $loggerUtility; |
|
158
|
7 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return Config |
|
162
|
|
|
*/ |
|
163
|
6 |
|
public function getConfig() |
|
164
|
|
|
{ |
|
165
|
6 |
|
if (!isset($this->config)) { |
|
166
|
5 |
|
$this->config = (new Config()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
6 |
|
return $this->config; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param Config $config |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function setConfig(Config $config) |
|
176
|
|
|
{ |
|
177
|
1 |
|
$this->config = $config; |
|
178
|
1 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return ApiClient |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function getApiClient() |
|
184
|
|
|
{ |
|
185
|
1 |
|
if (!isset($this->apiClient)) { |
|
186
|
1 |
|
$this->setApiClient(new ApiClient()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
return $this->apiClient; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param ApiClient $apiClient |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function setApiClient(ApiClient $apiClient) |
|
196
|
|
|
{ |
|
197
|
1 |
|
$this->apiClient = $apiClient; |
|
198
|
1 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return Client |
|
202
|
|
|
*/ |
|
203
|
1 |
|
public function getClient() |
|
204
|
|
|
{ |
|
205
|
1 |
|
if (!isset($this->client)) { |
|
206
|
1 |
|
$this->setClient(new Client()); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
1 |
|
return $this->client; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param Client $client |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function setClient(Client $client) |
|
216
|
|
|
{ |
|
217
|
1 |
|
$this->client = $client; |
|
218
|
1 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Specify the response type |
|
222
|
|
|
* If response in config is set to empty, it will be considered based on listener. |
|
223
|
|
|
* |
|
224
|
|
|
* @return mixed|string |
|
225
|
|
|
*/ |
|
226
|
7 |
|
private function getResponseType() |
|
227
|
|
|
{ |
|
228
|
7 |
|
if ($this->getSlackbot()->getRequest('debug') === true |
|
229
|
7 |
|
|| $this->getSlackbot()->getRequest('debug') === 'true') { |
|
230
|
2 |
|
return 'json'; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
5 |
|
$responseType = $this->getConfig()->get('response'); |
|
234
|
5 |
|
if (!empty($responseType)) { |
|
235
|
|
|
// Maybe later add a check for response type validation |
|
236
|
5 |
|
return $responseType; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
// response type in the config is empty, so choose it based on listener type |
|
240
|
|
|
return $this->getResponseByListenerType(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return string|null |
|
245
|
|
|
*/ |
|
246
|
|
|
private function getResponseByListenerType() |
|
247
|
|
|
{ |
|
248
|
|
|
$listener = $this->getConfig()->get('listener'); |
|
249
|
|
|
switch ($listener) { |
|
250
|
|
|
case 'slashCommand': |
|
251
|
|
|
return 'slashCommand'; |
|
252
|
|
|
case 'event': |
|
253
|
|
|
return 'slack'; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Send confirmation. |
|
259
|
|
|
*/ |
|
260
|
1 |
|
public function sendConfirmation() |
|
261
|
|
|
{ |
|
262
|
1 |
|
$userId = $this->getSlackbot()->getRequest('user_id'); |
|
263
|
|
|
|
|
264
|
1 |
|
$user = ''; |
|
265
|
1 |
|
if (!empty($userId)) { |
|
266
|
1 |
|
$user = $this->getMessageUtility()->linkToUser($userId).' '; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
1 |
|
$confirmMessage = $this->getConfig()->get('confirmReceivedMessage', ['user' => $user]); |
|
270
|
|
|
|
|
271
|
1 |
|
if (!empty($confirmMessage)) { |
|
272
|
1 |
|
$this->send($confirmMessage); |
|
273
|
|
|
} |
|
274
|
1 |
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return MessageUtility |
|
278
|
|
|
*/ |
|
279
|
1 |
|
public function getMessageUtility() |
|
280
|
|
|
{ |
|
281
|
1 |
|
if (!isset($this->messageUtility)) { |
|
282
|
1 |
|
$this->setMessageUtility(new MessageUtility()); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
1 |
|
return $this->messageUtility; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param MessageUtility $messageUtility |
|
290
|
|
|
*/ |
|
291
|
1 |
|
public function setMessageUtility(MessageUtility $messageUtility) |
|
292
|
|
|
{ |
|
293
|
1 |
|
$this->messageUtility = $messageUtility; |
|
294
|
1 |
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|