This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
277 lines (212 loc) · 7.69 KB
/
index.php
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
// http://stackoverflow.com/a/17539561/328817
header("content-type: text/html; charset=UTF-8");
use Slack\ClientObject;
use Slack\User;
use Slack\Payload;
require __DIR__ . '/vendor/autoload.php';
// Die if no token
// ================
if (!array_key_exists('token', $_GET)) {
echo '<p>Go and get a token from <a href="https://api.slack.com/docs/oauth-test-tokens">https://api.slack.com/docs/oauth-test-tokens</a>!</p>';
die();
}
// Setup
// ======
$loop = \React\EventLoop\Factory::create();
$client = new \Slack\ApiClient($loop);
$client->setToken($_GET['token']);
/** @var User[] $usersById */
$usersById = [];
$allTheData = [
'channels' => [],
'groups' => [],
'dms' => [],
];
// The callbacks we'll use
// =======================
// General
// -------
$failHandler = function ($data) {
echo "<p>Failed to get something!</p>";
var_dump($data);
};
// Getting messages
// -----------------
/**
* @param string $type
* @param ClientObject $channelOrWhatever
* @param null|\Slack\User $dmUser -- if $type == 'dms', pass in the user who the DM channel is for
*
* @return Closure
*/
$gotMessagesFunctionMaker = function($type, $channelOrWhatever, $dmUser = null) use (&$allTheData, &$usersById) {
return function ($payload) use (&$allTheData, &$usersById, $type, $channelOrWhatever, $dmUser) {
/** @var Payload $payload */
$data = $payload->getData();
$unread = $channelOrWhatever->data['unread_count'];
$messages = $data['messages'];
if ($unread > 0) {
$message = $messages[$unread - 1]; // -1 as arrays are 0-indexed
// Messages from bots have a username key set with their username in.
// Messages from users don't, but do have the user ID (not username) stored under the `user` key.
if (!array_key_exists('username', $message)) {
$message['username'] = $usersById[$message['user']]->getUsername();
}
$allTheData[$type][$channelOrWhatever->getId()]['channel'] = $channelOrWhatever;
$allTheData[$type][$channelOrWhatever->getId()]['message'] = $message;
if ($dmUser) {
$allTheData[$type][$channelOrWhatever->getId()]['dmUser'] = $dmUser;
}
}
};
};
// Getting channels
// -----------------
$gotChannel = function ($channel) use (&$allTheData, &$usersById, $client, $failHandler, $gotMessagesFunctionMaker) {
/** @var \Slack\Channel $channel */
if ($channel->getUnreadCount()) {
$gotMessages = $gotMessagesFunctionMaker('channels', $channel);
$client->apiCall('channels.history', [
'channel' => $channel->getId(),
'unreads' => 1
])->then($gotMessages, $failHandler);
}
};
$gotChannels = function ($channels) use (&$allTheData, $client, $gotChannel, $failHandler) {
/** @var \Slack\Channel $channel */
foreach ($channels as $channel) {
if ($channel->data['is_member'] && !$channel->isArchived()) {
$client->getChannelById($channel->getId())->then($gotChannel, $failHandler);
}
}
};
// Getting groups (private channels and multi-person DMs)
// -------------------------------------------------------
$gotGroup = function ($group) use ($client, $gotMessagesFunctionMaker, $failHandler) {
/** @var \Slack\Group $group */
if ($group->getUnreadCount()) {
$gotMessages = $gotMessagesFunctionMaker('groups', $group);
$client->apiCall('groups.history', [
'channel' => $group->getId(),
'unreads' => 1
])->then($gotMessages, $failHandler);
}
};
$gotGroups = function ($groups) use ($client, $gotGroup, $failHandler) {
/** @var \Slack\Group $group */
foreach ($groups as $group) {
if (!$group->isArchived()) {
$client->getGroupById($group->getId())->then($gotGroup, $failHandler);
}
}
};
// Getting IMs
// -----------
$gotDM = function ($dm) use (&$usersById, $client, $gotMessagesFunctionMaker, $failHandler) {
/** @var \Slack\DirectMessageChannel $dm */
$user = $usersById[$dm->data['user']];
$gotMessages = $gotMessagesFunctionMaker('dms', $dm, $user);
$client->apiCall('im.history', [
'channel' => $dm->getId(),
'unreads' => 1
])->then($gotMessages, $failHandler);
};
$gotDMs = function($dms) use (&$usersById, &$allTheData, $client, $gotDM, $failHandler) {
/** @var \Slack\DirectMessageChannel $dm */
foreach ($dms as $dm) {
$user = $usersById[$dm->data['user']];
if (!$user->isDeleted()) {
$client->getDMById($dm->getId())->then($gotDM, $failHandler);
}
}
};
// Actually call the stuff
// ========================
$client->getUsers()->then(function ($users) use (
$client, $gotChannels, $gotDMs, $gotGroups, $failHandler, &$usersById
) {
// build up an array of users, keyed on ID
/** @var \Slack\User $user */
foreach ($users as $user) {
$usersById[$user->getId()] = $user;
}
// call stuff that relies on this (these will in turn make further calls)
$client->getChannels()->then($gotChannels, $failHandler);
$client->getDMs()->then($gotDMs, $failHandler);
$client->getGroups()->then($gotGroups, $failHandler);
}, $failHandler);
// Run the loop and parse the data to write out the output
// =======================================================
$loop->run();
function formatUserMention($username) {
return "<span class=\"user-mention\">@{$username}</span>";
}
function formatMessage($message, $usersById)
{
// Sort out user mentions
$message = str_replace('<!channel>', formatUserMention('channel'), $message);
$message = preg_replace_callback('/<@([^>]+)>/', function ($string) use (&$usersById) {
/** @var User[] $usersById */
$username = $string[1];
if (array_key_exists($username, $usersById)) {
$user = $usersById[$username];
return formatUserMention($user->getUsername());
}
return $string[0];
}, $message);
return $message;
}
function printMessage(array $message, $usersById)
{
echo (sprintf(
'<blockquote><p class="msg-header"><span class="username">%s</span> <span class="msg-date">%s</span></p><p>%s</p></blockquote>',
$message['username'],
date('j M Y G:i', $message['ts']),
formatMessage($message['text'], $usersById)
));
}
function printChannelName($name, $extraStuff = '')
{
echo sprintf('<p class="channel-name"><span class="channel-hash">#</span> <b>%s</b>%s</p>', $name, $extraStuff);
}
echo <<<EOD
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
<title>Slack Headlines</title>
</head>
<body>
EOD;
echo "<h1>Your public channels</h1>";
foreach ($allTheData['channels'] as $channelArray) {
/** @var \Slack\Channel $channel */
$channel = $channelArray['channel'];
printChannelName($channel->getName(), sprintf(
' (%d)',
$channel->getUnreadCount()
));
printMessage($channelArray['message'], $usersById);
}
echo "<h1 class=\"later-header\">Your private channels and multi-person DMs</h1>";
foreach ($allTheData['groups'] as $groupArray) {
/** @var \Slack\Group $group */
$group = $groupArray['channel'];
printChannelName($group->getName());
printMessage($groupArray['message'], $usersById);
}
echo "<h1 class=\"later-header\">Your DMs</h1>";
foreach ($allTheData['dms'] as $dmArray) {
/** @var \Slack\DirectMessageChannel $dm */
$dm = $dmArray['channel'];
/** @var User $user */
$user = $dmArray['dmUser'];
printChannelName($user->getUsername());
printMessage($dmArray['message'], $usersById);
}
echo <<<EOD
</body>
</html>
EOD;