-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSocketServer.php
More file actions
166 lines (147 loc) · 5.83 KB
/
Copy pathSocketServer.php
File metadata and controls
166 lines (147 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace SlackPhp\SocketMode;
use Amp;
use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Websocket;
use Amp\Websocket\Client\Connection;
use SlackPhp\Framework\AppServer;
use SlackPhp\Framework\Context;
use SlackPhp\Framework\Contexts\Payload;
use SlackPhp\Framework\Exception;
use Throwable;
/**
* Slack app server implementation to support Socket Mode.
*
* Socket mode can be used to test a Slack application locally or without the need to expose a public URL to the app.
*/
class SocketServer extends AppServer
{
private HttpClient $httpClient;
private Connection $connection;
private bool $debugReconnects;
/**
* Decreases the expiration time of the websocket connections for test/debugging purposes.
*
* @return $this
*/
public function withDebugReconnects(): self
{
$this->debugReconnects = true;
return $this;
}
public function init(): void
{
$this->httpClient = HttpClientBuilder::buildDefault();
$this->debugReconnects = false;
}
/**
* Starts receiving and processing events from Slack.
*/
public function start(): void
{
$logger = $this->getLogger();
Amp\Loop::run(function () use ($logger) {
try {
$this->connection = yield $this->createConnection();
$logger->debug('Socket Mode connection established');
while ($message = yield $this->connection->receive()) {
/** @var Websocket\Message $message */
$content = yield $message->buffer();
$envelope = new SocketEnvelope($content);
if ($envelope->isConnection()) {
$logger->debug('Socket Mode connection acknowledged by Slack');
} elseif ($envelope->isReconnect()) {
$expiredConn = $this->connection;
$this->connection = yield $this->createConnection();
$logger->debug('Socket Mode connection re-established');
$expiredConn->close();
$logger->debug('Expired Socket Mode connection closed');
} elseif ($envelope->isDisconnect()) {
$this->stop();
} elseif ($envelope->isAppEvent()) {
yield $this->handleSlackAppEvent($envelope);
}
yield Amp\delay(250);
}
} catch (Throwable $exception) {
$logger->error('Error occurred during Socket Mode', compact('exception'));
$this->stop();
}
});
}
public function stop(): void
{
if (isset($this->connection)) {
$this->connection->close();
}
$this->getLogger()->debug('Socket Mode connection closed');
exit(1);
}
/**
* Uses the App to handle the incoming Slack event.
*
* @param SocketEnvelope $envelope
* @return Amp\Promise
*/
private function handleSlackAppEvent(SocketEnvelope $envelope): Amp\Promise
{
return Amp\call(function () use ($envelope) {
// Create the Context.
$payload = new Payload($envelope->getPayload());
$context = new Context($payload, ['envelope' => $envelope]);
// Let the app handle the Context.
$app = $this->getApp();
$app->handle($context);
if (!$context->isAcknowledged()) {
throw new Exception('App did not ack for the context');
}
// Ack back to Slack through the websocket. The envelope_id must be included.
$ack = new SocketAck($envelope->getEnvelopeId(), $context->getAck());
yield $this->connection->send((string) $ack);
// Pass the context back through the app a second time for any deferred (i.e., post-ack) logic.
if ($context->isDeferred()) {
$app->handle($context);
}
});
}
/**
* Creates a websocket connection to Slack for the app.
*
* @return Amp\Promise<Connection>
*/
private function createConnection(): Amp\Promise
{
return Amp\call(function () {
// Make sure an app token is available.
$appToken = $this->getAppCredentials()->getAppToken();
if ($appToken === null) {
throw new Exception('Cannot create a Socket Mode connection without a configured app token');
}
// Prepare and send a request to the Slack API to get the Web Socket URL.
$request = new Request('https://slack.com/api/apps.connections.open', 'POST');
$request->addHeader('Content-Type', 'application/x-www-form-urlencoded');
$request->addHeader('Authorization', "Bearer {$appToken}");
/** @var Response $response */
$response = yield $this->httpClient->request($request);
if ($response->getStatus() !== 200) {
throw new Exception('Request to get WSS URL failed');
}
// Extract the Websocket URL from the response from the Slack API.
$contents = yield $response->getBody()->buffer();
$result = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
if (!isset($result['ok'], $result['url']) || !$result['ok']) {
throw new Exception('Response containing WSS URL was invalid');
}
$wssUrl = $result['url'];
if ($this->debugReconnects) {
$wssUrl .= '&debug_reconnects=true';
}
// Establish and return a Connection to the Websocket.
return Websocket\Client\connect($wssUrl);
});
}
}