|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Botonomous; |
|
4
|
|
|
|
|
5
|
|
|
use /* @noinspection PhpUndefinedClassInspection */ |
|
6
|
|
|
GuzzleHttp\Client; |
|
7
|
|
|
use /* @noinspection PhpUndefinedClassInspection */ |
|
8
|
|
|
GuzzleHttp\Psr7\Request; |
|
9
|
|
|
use Botonomous\client\ApiClient; |
|
10
|
|
|
use Botonomous\utility\LoggerUtility; |
|
11
|
|
|
|
|
12
|
|
|
class Sender |
|
13
|
|
|
{ |
|
14
|
|
|
private $slackbot; |
|
15
|
|
|
private $loggerUtility; |
|
16
|
|
|
private $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Sender constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param AbstractBot $slackbot |
|
22
|
|
|
*/ |
|
23
|
9 |
|
public function __construct(AbstractBot $slackbot) |
|
24
|
|
|
{ |
|
25
|
9 |
|
$this->setSlackbot($slackbot); |
|
26
|
9 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return Slackbot |
|
30
|
|
|
*/ |
|
31
|
8 |
|
public function getSlackbot() |
|
32
|
|
|
{ |
|
33
|
8 |
|
return $this->slackbot; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param AbstractBot $slackbot |
|
38
|
|
|
*/ |
|
39
|
9 |
|
public function setSlackbot(AbstractBot $slackbot) |
|
40
|
|
|
{ |
|
41
|
9 |
|
$this->slackbot = $slackbot; |
|
42
|
9 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Final endpoint for the response. |
|
46
|
|
|
* |
|
47
|
|
|
* @param $channel |
|
48
|
|
|
* @param $response |
|
49
|
|
|
* @param $attachments |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \Exception |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public function send($channel, $response, $attachments = null) |
|
56
|
|
|
{ |
|
57
|
|
|
// @codeCoverageIgnoreStart |
|
58
|
|
|
if ($this->getSlackbot()->getListener()->isThisBot() !== false) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
// @codeCoverageIgnoreEnd |
|
62
|
|
|
|
|
63
|
8 |
|
$responseType = $this->getConfig()->get('response'); |
|
64
|
8 |
|
$debug = (bool) $this->getSlackbot()->getRequest('debug'); |
|
65
|
|
|
|
|
66
|
8 |
|
if (empty($channel)) { |
|
67
|
3 |
|
$channel = $this->getConfig()->get('channel'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$data = [ |
|
71
|
8 |
|
'text' => $response, |
|
72
|
8 |
|
'channel' => $channel, |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
8 |
|
if ($attachments !== null) { |
|
76
|
1 |
|
$data['attachments'] = json_encode($attachments); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
8 |
|
if ($debug === true) { |
|
80
|
3 |
|
echo json_encode($data); |
|
81
|
5 |
|
} elseif ($responseType === 'slack') { |
|
82
|
1 |
|
$this->getLoggerUtility()->logChat(__METHOD__, $response); |
|
83
|
1 |
|
(new ApiClient())->chatPostMessage($data); |
|
84
|
4 |
|
} elseif ($responseType === 'slashCommand') { |
|
85
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
86
|
|
|
$request = new Request( |
|
87
|
|
|
'POST', |
|
88
|
|
|
$this->getSlackbot()->getRequest('response_url'), |
|
89
|
|
|
['Content-Type' => 'application/json'], |
|
90
|
|
|
json_encode([ |
|
91
|
|
|
'text' => $response, |
|
92
|
|
|
'response_type' => 'in_channel', |
|
93
|
|
|
]) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
/* @noinspection PhpUndefinedClassInspection */ |
|
97
|
|
|
(new Client())->send($request); |
|
98
|
4 |
|
} elseif ($responseType === 'json') { |
|
99
|
4 |
|
$this->getLoggerUtility()->logChat(__METHOD__, $response); |
|
100
|
|
|
// headers_sent is used to avoid issue in the test |
|
101
|
4 |
|
if (!headers_sent()) { |
|
102
|
4 |
|
header('Content-type:application/json;charset=utf-8'); |
|
103
|
|
|
} |
|
104
|
4 |
|
echo json_encode($data); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
8 |
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return LoggerUtility |
|
112
|
|
|
*/ |
|
113
|
5 |
|
public function getLoggerUtility() |
|
114
|
|
|
{ |
|
115
|
5 |
|
if (!isset($this->loggerUtility)) { |
|
116
|
5 |
|
$this->setLoggerUtility(new LoggerUtility()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
5 |
|
return $this->loggerUtility; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param LoggerUtility $loggerUtility |
|
124
|
|
|
*/ |
|
125
|
5 |
|
public function setLoggerUtility(LoggerUtility $loggerUtility) |
|
126
|
|
|
{ |
|
127
|
5 |
|
$this->loggerUtility = $loggerUtility; |
|
128
|
5 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return Config |
|
132
|
|
|
*/ |
|
133
|
9 |
|
public function getConfig() |
|
134
|
|
|
{ |
|
135
|
9 |
|
if ($this->config === null) { |
|
136
|
8 |
|
$this->config = (new Config()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
9 |
|
return $this->config; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param Config $config |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function setConfig(Config $config) |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->config = $config; |
|
148
|
1 |
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|