1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Slack; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Closure; |
7
|
|
|
use Nopolabs\Yabot\Helpers\ConfigTrait; |
8
|
|
|
use Nopolabs\Yabot\Helpers\LogTrait; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use React\Promise\PromiseInterface; |
11
|
|
|
use Slack\Channel; |
12
|
|
|
use Slack\ChannelInterface; |
13
|
|
|
use Slack\Payload; |
14
|
|
|
use Slack\RealTimeClient; |
15
|
|
|
use Slack\User; |
16
|
|
|
|
17
|
|
|
class Client |
18
|
|
|
{ |
19
|
|
|
use ConfigTrait; |
20
|
|
|
use LogTrait; |
21
|
|
|
|
22
|
|
|
/** @var RealTimeClient */ |
23
|
|
|
private $realTimeClient; |
24
|
|
|
|
25
|
|
|
/** @var Users */ |
26
|
|
|
private $users; |
27
|
|
|
|
28
|
|
|
/** @var Channels */ |
29
|
|
|
private $channels; |
30
|
|
|
|
31
|
|
|
/** @var User */ |
32
|
|
|
protected $authedUser; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
RealTimeClient $realTimeClient, |
36
|
|
|
Users $users, |
37
|
|
|
Channels $channels, |
38
|
|
|
array $config = [], |
39
|
|
|
LoggerInterface $log = null) |
40
|
|
|
{ |
41
|
|
|
$this->realTimeClient = $realTimeClient; |
42
|
|
|
$this->users = $users; |
43
|
|
|
$this->channels = $channels; |
44
|
|
|
$this->setConfig($config); |
45
|
|
|
$this->setLog($log); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function init() : Client |
49
|
|
|
{ |
50
|
|
|
$this->initChannelUpdateHandlers(); |
51
|
|
|
$this->initUserUpdateHandlers(); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function update(Closure $authedUserUpdated) |
57
|
|
|
{ |
58
|
|
|
$this->updateUsers(); |
59
|
|
|
$this->updateChannels(); |
60
|
|
|
$this->updateAuthedUser($authedUserUpdated); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function onEvent($event, array $onEvent) |
64
|
|
|
{ |
65
|
|
|
$this->realTimeClient->on($event, function(Payload $payload) use ($onEvent) { |
66
|
|
|
call_user_func($onEvent, $payload); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getRealTimeClient() |
71
|
|
|
{ |
72
|
|
|
return $this->realTimeClient; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getAuthedUser() |
76
|
|
|
{ |
77
|
|
|
return $this->authedUser; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getAuthedUsername() |
81
|
|
|
{ |
82
|
|
|
return $this->authedUser->getUsername(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function connect() : PromiseInterface |
86
|
|
|
{ |
87
|
|
|
return $this->realTimeClient->connect(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function disconnect() |
91
|
|
|
{ |
92
|
|
|
return $this->realTimeClient->disconnect(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function say($text, $channelOrName, array $additionalParameters = []) |
96
|
|
|
{ |
97
|
|
|
$channel = $channelOrName; |
98
|
|
|
if (!($channel instanceof ChannelInterface)) { |
99
|
|
|
$channel = $this->getChannelByName($channelOrName); |
100
|
|
|
if (!($channel instanceof ChannelInterface)) { |
101
|
|
|
$channel = $this->getChannelById($channelOrName); |
102
|
|
|
if (!($channel instanceof ChannelInterface)) { |
103
|
|
|
$this->warning('No channel, trying to say: '.$text); |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (empty($additionalParameters) && $this->useWebSocket()) { |
110
|
|
|
// WebSocket send does not support message formatting. |
111
|
|
|
$this->send($text, $channel); |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Http post send supports message formatting. |
116
|
|
|
$this->post($text, $channel, $additionalParameters); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function send($text, ChannelInterface $channel) |
120
|
|
|
{ |
121
|
|
|
$this->realTimeClient->send($text, $channel); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function post($text, ChannelInterface $channel, array $additionalParameters = []) |
125
|
|
|
{ |
126
|
|
|
$parameters = array_merge([ |
127
|
|
|
'text' => $text, |
128
|
|
|
'channel' => $channel->getId(), |
129
|
|
|
'as_user' => true, |
130
|
|
|
], $additionalParameters); |
131
|
|
|
|
132
|
|
|
$this->realTimeClient->apiCall('chat.postMessage', $parameters); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $id |
137
|
|
|
* @return null|User |
138
|
|
|
*/ |
139
|
|
|
public function getUserById($id) |
140
|
|
|
{ |
141
|
|
|
return $this->users->byId($id); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $name |
146
|
|
|
* @return null|User |
147
|
|
|
*/ |
148
|
|
|
public function getUserByName($name) |
149
|
|
|
{ |
150
|
|
|
return $this->users->byName($name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $id |
155
|
|
|
* @return null|Channel |
156
|
|
|
*/ |
157
|
|
|
public function getChannelById($id) |
158
|
|
|
{ |
159
|
|
|
return $this->channels->byId($id); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $name |
164
|
|
|
* @return null|Channel |
165
|
|
|
*/ |
166
|
|
|
public function getChannelByName($name) |
167
|
|
|
{ |
168
|
|
|
return $this->channels->byName($name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function useWebSocket() : bool |
172
|
|
|
{ |
173
|
|
|
return (bool) $this->get('use.websocket', false); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function updateUsers() |
177
|
|
|
{ |
178
|
|
|
$this->realTimeClient->getUsers()->then(function(array $users) { |
179
|
|
|
$this->users->update($users); |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function updateChannels() |
184
|
|
|
{ |
185
|
|
|
$this->realTimeClient->getChannels()->then(function(array $channels) { |
186
|
|
|
$this->channels->update($channels); |
187
|
|
|
}); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
protected function updateAuthedUser(Closure $authedUserUpdated) |
191
|
|
|
{ |
192
|
|
|
$this->realTimeClient->getAuthedUser()->then(function(User $user) use ($authedUserUpdated) { |
193
|
|
|
$this->authedUser = $user; |
194
|
|
|
$authedUserUpdated($user); |
195
|
|
|
}); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected function initChannelUpdateHandlers() |
199
|
|
|
{ |
200
|
|
|
$events = ['channel_created', 'channel_deleted', 'channel_rename']; |
201
|
|
|
foreach ($events as $event) { |
202
|
|
|
$this->onEvent($event, [$this, 'updateChannels']); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function initUserUpdateHandlers() |
207
|
|
|
{ |
208
|
|
|
$events = ['user_change']; |
209
|
|
|
foreach ($events as $event) { |
210
|
|
|
$this->onEvent($event, [$this, 'updateUsers']); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |