-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlackClient.php
More file actions
211 lines (187 loc) · 6.67 KB
/
SlackClient.php
File metadata and controls
211 lines (187 loc) · 6.67 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
declare(strict_types=1);
namespace App\Http;
use function Cake\Core\env;
use function Sentry\captureMessage;
use function Sentry\withScope;
class SlackClient
{
protected const SLACK_API_URL = 'slack.com/api';
protected Client $client;
/**
* Constructor.
*/
public function __construct()
{
$this->client = new Client([
'host' => self::SLACK_API_URL,
'scheme' => 'https',
'headers' => [
'Authorization' => 'Bearer ' . env('SLACK_BOT_USER_OAUTH_TOKEN'),
],
'timeout' => 5,
]);
}
/**
* @param string $channel The channel to send the message to.
* @param string $text The text of the message to send.
* @return void
* @see https://api.slack.com/methods/chat.postMessage
*/
public function postMessage(string $channel, string $text): void
{
$response = $this->client->post('chat.postMessage', [
'channel' => $channel,
'text' => $text,
]);
if ($response->isSuccess()) {
$json = $response->getJson();
if ($json['ok'] === false) {
withScope(function ($scope) use ($json, $channel, $text): void {
$scope->setExtras([
'channel' => $channel,
'text' => $text,
'slack_response' => $json,
]);
captureMessage('Slack API error: https://api.slack.com/methods/chat.postMessage');
});
}
}
}
/**
* @param string $channel The channel to send the message to.
* @param string $blocks The blocks of the message to send.
* @return void
* @see https://api.slack.com/methods/chat.postMessage
*/
public function postBlocks(string $channel, string $blocks): void
{
$response = $this->client->post('chat.postMessage', [
'channel' => $channel,
'blocks' => $blocks,
]);
if ($response->isSuccess()) {
$json = $response->getJson();
if ($json['ok'] === false) {
withScope(function ($scope) use ($json, $channel, $blocks): void {
$scope->setExtras([
'channel' => $channel,
'blocks' => $blocks,
'slack_response' => $json,
]);
captureMessage('Slack API error: https://api.slack.com/methods/chat.postMessage');
});
}
}
}
/**
* @param string $channel The channel to send the message to.
* @param string $user The user to send the message to.
* @param string $text The text of the message to send.
* @param string|null $threadTimestamp The thread timestamp of the message.
* @return void
* @see https://api.slack.com/methods/chat.postEphemeral
*/
public function postEphemeral(string $channel, string $user, string $text, ?string $threadTimestamp = null): void
{
$response = $this->client->post('chat.postEphemeral', [
'channel' => $channel,
'user' => $user,
'text' => $text,
'thread_ts' => $threadTimestamp,
]);
if ($response->isSuccess()) {
$json = $response->getJson();
if ($json['ok'] === false) {
withScope(function ($scope) use ($json, $channel, $user, $text, $threadTimestamp): void {
$scope->setExtras([
'channel' => $channel,
'user' => $user,
'text' => $text,
'thread_ts' => $threadTimestamp,
'slack_response' => $json,
]);
captureMessage('Slack API error: https://api.slack.com/methods/chat.postEphemeral');
});
}
}
}
/**
* @param string $user The user to publish the view for.
* @param array $view The view to publish.
* @return void
* @see https://api.slack.com/methods/views.publish
*/
public function publishView(string $user, array $view): void
{
$response = $this->client->post('views.publish', [
'user_id' => $user,
'view' => json_encode($view),
]);
if ($response->isSuccess()) {
$json = $response->getJson();
if ($json['ok'] === false) {
withScope(function ($scope) use ($json, $user, $view): void {
$scope->setExtras([
'user_id' => $user,
'view' => $view,
'slack_response' => $json,
]);
captureMessage('Slack API error: https://api.slack.com/methods/views.publish');
});
}
}
}
/**
* @param string $triggerId The trigger ID to open the view for.
* @param array $view The view to open.
* @return void
* @see https://api.slack.com/methods/views.open
*/
public function openView(string $triggerId, array $view): void
{
$response = $this->client->post('views.open', [
'trigger_id' => $triggerId,
'view' => json_encode($view),
]);
if ($response->isSuccess()) {
$json = $response->getJson();
if ($json['ok'] === false) {
withScope(function ($scope) use ($json, $triggerId, $view): void {
$scope->setExtras([
'trigger_id' => $triggerId,
'view' => $view,
'slack_response' => $json,
]);
captureMessage('Slack API error: https://api.slack.com/methods/views.open');
});
}
}
}
/**
* @param string $user The Slack user ID.
* @return array
* @see https://api.slack.com/methods/users.info
*/
public function getUser(string $user): array
{
$response = $this->client->get('users.info', [
'user' => $user,
]);
if ($response->isSuccess()) {
$json = $response->getJson();
if ($json['ok'] === true) {
return $json['user'];
} else {
withScope(function ($scope) use ($json, $user): void {
$scope->setExtras([
'user' => $user,
'slack_response' => $json,
]);
captureMessage('Slack API error: https://api.slack.com/methods/users.info');
});
}
}
return [];
}
}