|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Botonomous; |
|
4
|
|
|
|
|
5
|
|
|
use /* @noinspection PhpUndefinedClassInspection */ |
|
6
|
|
|
GuzzleHttp\Psr7\Request; |
|
7
|
|
|
|
|
8
|
|
|
class Sender extends AbstractSender |
|
9
|
|
|
{ |
|
10
|
|
|
private $slackbot; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Sender constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param AbstractBot $slackbot |
|
16
|
|
|
*/ |
|
17
|
13 |
|
public function __construct(AbstractBot $slackbot) |
|
18
|
|
|
{ |
|
19
|
13 |
|
$this->setSlackbot($slackbot); |
|
20
|
13 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return Slackbot |
|
24
|
|
|
*/ |
|
25
|
9 |
|
public function getSlackbot() |
|
26
|
|
|
{ |
|
27
|
9 |
|
return $this->slackbot; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param AbstractBot $slackbot |
|
32
|
|
|
*/ |
|
33
|
13 |
|
public function setSlackbot(AbstractBot $slackbot) |
|
34
|
|
|
{ |
|
35
|
13 |
|
$this->slackbot = $slackbot; |
|
36
|
13 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Final endpoint for the response. |
|
40
|
|
|
* |
|
41
|
|
|
* @param $channel |
|
42
|
|
|
* @param $text |
|
43
|
|
|
* @param $attachments |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
9 |
|
public function send($text, $channel = null, $attachments = null) |
|
50
|
|
|
{ |
|
51
|
|
|
// @codeCoverageIgnoreStart |
|
52
|
|
|
if ($this->getSlackbot()->getListener()->isThisBot() !== false) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
// @codeCoverageIgnoreEnd |
|
56
|
|
|
|
|
57
|
9 |
|
$responseType = $this->getResponseType(); |
|
58
|
|
|
|
|
59
|
9 |
|
if (empty($channel)) { |
|
60
|
3 |
|
$channel = $this->getSlackbot()->getListener()->getChannelId(); |
|
61
|
|
|
|
|
62
|
3 |
|
if (empty($channel)) { |
|
63
|
3 |
|
$channel = $this->getConfig()->get('channel'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$data = [ |
|
68
|
9 |
|
'text' => $text, |
|
69
|
9 |
|
'channel' => $channel, |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
9 |
|
if ($attachments !== null) { |
|
73
|
1 |
|
$data['attachments'] = json_encode($attachments); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
$this->getLoggerUtility()->logChat(__METHOD__, $text, $channel); |
|
77
|
|
|
|
|
78
|
9 |
|
if ($responseType === 'slack') { |
|
79
|
1 |
|
$this->respondToSlack($data); |
|
80
|
8 |
|
} elseif ($responseType === 'slashCommand') { |
|
81
|
1 |
|
$this->respondToSlashCommand($text); |
|
82
|
7 |
|
} elseif ($responseType === 'json') { |
|
83
|
7 |
|
$this->respondAsJSON($data); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param $response |
|
91
|
|
|
*/ |
|
92
|
1 |
|
private function respondToSlashCommand($response) |
|
93
|
|
|
{ |
|
94
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
95
|
1 |
|
$request = new Request( |
|
96
|
1 |
|
'POST', |
|
97
|
1 |
|
$this->getSlackbot()->getRequest('response_url'), |
|
98
|
1 |
|
['Content-Type' => 'application/json'], |
|
99
|
|
|
json_encode([ |
|
100
|
1 |
|
'text' => $response, |
|
101
|
1 |
|
'response_type' => 'in_channel', |
|
102
|
|
|
]) |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
/* @noinspection PhpUndefinedClassInspection */ |
|
106
|
1 |
|
$this->getClient()->send($request); |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $data |
|
111
|
|
|
*/ |
|
112
|
1 |
|
private function respondToSlack($data) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->getApiClient()->chatPostMessage($data); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $data |
|
119
|
|
|
*/ |
|
120
|
7 |
|
private function respondAsJSON($data) |
|
121
|
|
|
{ |
|
122
|
|
|
// headers_sent is used to avoid issue in the test |
|
123
|
7 |
|
if (!headers_sent()) { |
|
124
|
7 |
|
header('Content-type:application/json;charset=utf-8'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
7 |
|
echo json_encode($data); |
|
128
|
7 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Specify the response type |
|
132
|
|
|
* If response in config is set to empty, it will be considered based on listener. |
|
133
|
|
|
* |
|
134
|
|
|
* @return mixed|string |
|
135
|
|
|
*/ |
|
136
|
9 |
|
private function getResponseType() |
|
137
|
|
|
{ |
|
138
|
9 |
|
if ($this->getSlackbot()->getRequest('debug') === true |
|
139
|
9 |
|
|| $this->getSlackbot()->getRequest('debug') === 'true') { |
|
140
|
7 |
|
return 'json'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// response type in the config is empty, so choose it based on listener type |
|
144
|
2 |
|
return $this->getResponseByListenerType(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string|null |
|
149
|
|
|
*/ |
|
150
|
2 |
|
private function getResponseByListenerType() |
|
151
|
|
|
{ |
|
152
|
2 |
|
$listener = $this->getConfig()->get('listener'); |
|
153
|
|
|
switch ($listener) { |
|
154
|
2 |
|
case 'slashCommand': |
|
155
|
1 |
|
return 'slashCommand'; |
|
156
|
1 |
|
case 'event': |
|
157
|
1 |
|
return 'slack'; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Send confirmation. |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public function sendConfirmation() |
|
165
|
|
|
{ |
|
166
|
1 |
|
$userId = $this->getSlackbot()->getRequest('user_id'); |
|
167
|
|
|
|
|
168
|
1 |
|
$user = ''; |
|
169
|
1 |
|
if (!empty($userId)) { |
|
170
|
1 |
|
$user = $this->getMessageUtility()->linkToUser($userId).' '; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
$confirmMessage = $this->getConfig()->get('confirmReceivedMessage', ['user' => $user]); |
|
174
|
|
|
|
|
175
|
1 |
|
if (!empty($confirmMessage)) { |
|
176
|
1 |
|
$this->send($confirmMessage); |
|
177
|
|
|
} |
|
178
|
1 |
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|