-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq.php
490 lines (353 loc) · 18.1 KB
/
req.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
date_default_timezone_set('Europe/Istanbul');
require_once dirname( __FILE__ ) . '/utils.php';
$req = file_get_contents("php://input");
if (strpos($req, 'payload=') === 0) {
$req = urldecode(substr($req, 8));
}
$req = json_decode($req);
if($req->type === 'url_verification' and $req->token === VERIF_TOKEN) {
die($req->challenge);
}
if(/*$req->api_app_id !== APP_ID or*/ $req->token !== VERIF_TOKEN) {
die('auth!');
}
//logx($req);
$inputRaw = str_replace(['“', '”'], '"', $req->event->text);
$input = strtolower($inputRaw);
if(!isset($req->event->bot_id) and $req->type !== 'block_actions' and $req->type !== 'view_submission' and $req->type !== 'message_action') {
# list reminders
if($input === 'l' or $input === 'list' or $input === 'la' or $input === 'list all' or $input === 'listall') {
$all = ($input === 'la' or $input === 'list all' or $input === 'listall');
$db = get_db();
$select = 'select * from reminders where uid=? ';
if(!$all) {
$select .= 'and done=0 ';
}
$select .= 'order by alarm_time asc;';
$res = $db->prepare($select);
$res->execute(array($req->event->user));
$res->setFetchMode(PDO::FETCH_OBJ);
$resList = $res->fetchAll();
$responseText = '{ "blocks": [ { "type": "context", "elements": [ { "type": "mrkdwn", "text": "*your reminders:*" } ] }, { "type": "divider" },';
$ix = 0;
foreach ($resList as $r) {
if($ix>0 and $ix % 5 === 0) {
$responseText = '{ "blocks": [';
}
$prefix = numberToSlackEmoji(1+$ix);
$reminderDate = date('H:i D, j M Y ', strtotime($r->alarm_time));
if(strtotime('now')>strtotime($r->alarm_time)) {
$reminderDate .= ' *(past)*';
}
$responseText .= '{ "type": "section", "text": { "type": "mrkdwn", "text": "'.$prefix.' *' . $r->content . '* _(#' . $r->id_user . ')_\n'.$reminderDate.'" }}, {"type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "emoji": true, "text": "Complete" }, "style": "primary", "value": "complete_'.$r->id_user.'" }, { "type": "button", "confirm": { "title": { "type": "plain_text", "text": "Wait!" }, "text": { "type": "mrkdwn", "text": "Are you sure to permanently delete reminder #'.$r->id_user.'?" }, "confirm": { "type": "plain_text", "text": "Yes, delete" }, "deny": { "type": "plain_text", "text": "Back" } }, "text": { "type": "plain_text", "emoji": true, "text": "Delete" }, "style": "danger", "value": "del_'.$r->id_user.'" } ] },';
if( ($ix>0 and ($ix+1) % 5 === 0) or sizeof($resList)===$ix+1 ) {
$responseText = substr($responseText, 0, -1);
$responseText .= '] }';
$remindersContent = json_decode($responseText);
$data = json_encode([
"channel" => $req->event->channel,
"blocks" => $remindersContent->blocks
]);
curlReq('chat.postMessage', $data);
}
$ix++;
}
if(empty($resList)) {
$responseText = '{ "blocks": [ { "type": "context", "elements": [ { "type": "mrkdwn", "text": "*no reminders!*" } ] } ] }';
$remindersContent = json_decode($responseText);
$data = json_encode([
"channel" => $req->event->channel,
"blocks" => $remindersContent->blocks
]);
curlReq('chat.postMessage', $data);
}
# add new reminder
} elseif(preg_match('/^".*" .*$/i', $inputRaw)) {
addNewReminder($inputRaw);
#help menu
} else if($input === 'help') {
$helpContent = json_decode(getHelpMenuContent());
$data = json_encode([
"channel" => $req->event->channel,
"blocks" => $helpContent->blocks
]);
curlReq('chat.postMessage', $data);
# prefs help
} else if($input === 'pref' or $input === 'prefs' or $input === 'pref snooze') {
$responseText = "type `pref list` for your preferences.\ntype `pref hour [time]` to set your default hour.\ntype `pref snooze [time]` to set your snooze times.";
$data = json_encode([
"channel" => $req->event->channel,
"text" => $responseText
]);
curlReq('chat.postMessage', $data);
# prefs
} else if(strpos($input, 'pref ') === 0) {
$p = preg_split('/\s+/', $input, 3);
$prefOption = trim($p[1]);
#default hour
if($prefOption === 'hour') {
if(preg_match('/^\d{1,2}:\d{2}$/', trim($p[2]))) {
$hhmm = str_pad(trim($p[2]), 5, "0", STR_PAD_LEFT);
$db = get_db();
$upsertQuery = $db->prepare('INSERT INTO prefs (uid, cid, default_hour) VALUES(?,?,?) ON DUPLICATE KEY UPDATE default_hour=?');
if ($upsertQuery->execute(array($req->event->user, $req->event->channel, $hhmm, $hhmm))) {
$responseText = ':white_check_mark: ' . $hhmm . ' is set as your default hour.';
} else {
$responseText = ':x: an error occured! please try again some time...';
}
} else {
$responseText = ':x: default hour must be in `hh:mm` format';
}
#default snooze times
} else if($prefOption === 'snooze') {
$times = explode(',', $p[2]);
$responseText = '';
$validTimes = array();
foreach ($times as $t) {
$tt = convTime(trim($t));
if($tt > 0) {
array_push($validTimes, trim($t));
} else {
if(strlen($responseText) === 0) {
$responseText = ':x: ';
}
$responseText .= '`' . trim($t) . '`, ';
}
}
if(strlen($responseText) > 0) {
$responseText = substr($responseText, 0, -2);
$responseText .= ' invalid. type `help` for valid times. ';
}
if(sizeof($validTimes) > 0) {
$validTimesStr = implode(', ', $validTimes);
$db = get_db();
$upsertQuery = $db->prepare('INSERT INTO prefs (uid, cid, snooze_times) VALUES(?,?,?) ON DUPLICATE KEY UPDATE snooze_times=?');
if ($upsertQuery->execute(array($req->event->user, $req->event->channel, $validTimesStr, $validTimesStr))) {
$responseText .= "\n" . ':white_check_mark: `' . $validTimesStr . '` is set as your default snooze times.';
} else {
$responseText = "\n" . 'x: an error occured! please try again some time...';
}
}
#pref list
} else if($prefOption === 'list') {
$db = get_db();
$res = $db->prepare('select snooze_times, default_hour from prefs where uid=? limit 1;');
$res->execute(array($req->event->user));
$res->setFetchMode(PDO::FETCH_OBJ);
$s = $res->fetch();
$responseText = ':information_source: ';
if($s->default_hour) {
$responseText .= 'default hour: `' . $s->default_hour . '`, ';
} else {
$responseText .= 'no default hour, ';
}
if($s->snooze_times) {
$responseText .= 'snooze times: `' . $s->snooze_times . '`.';
} else {
$responseText .= 'no snooze times.';
}
} else {
$responseText = ':x: invalid pref! type `help` for more';
}
$data = json_encode([
"channel" => $req->event->channel,
"text" => $responseText
]);
curlReq('chat.postMessage', $data);
} else if(isset($req->event->message->bot_id)) {
//ignore
} else {
$data = json_encode([
"channel" => $req->event->channel,
"text" => ':x: invalid command! type `help` for more.'
]);
curlReq('chat.postMessage', $data);
}
} else if($req->type === 'message_action' and $req->callback_id === 'msg_reminder') {
$msgData = array($req->user->id, $req->message_ts, $req->channel->id);
#message reminder modal
$modalData = '{ "trigger_id": "'.$req->trigger_id.'", "view": { "type": "modal", "title": { "type": "plain_text", "text": "Message reminder", "emoji": true }, "submit": { "type": "plain_text", "text": "Submit", "emoji": true }, "close": { "type": "plain_text", "text": "Cancel", "emoji": true }, "callback_id": "remindmsg_'.implode('_', $msgData).'", "blocks": [ { "type": "section", "text": { "type": "plain_text", "text": "Add a reminder about this message.", "emoji": true } }, { "type": "divider" }, { "type": "input", "label": { "type": "plain_text", "text": "Reminder time: (default: tomorrow)", "emoji": false }, "element": { "type": "plain_text_input", "multiline": false }, "optional": true }, { "type": "input", "label": { "type": "plain_text", "text": "Reminder message", "emoji": true }, "element": { "type": "plain_text_input", "multiline": false }, "optional": true } ] } }';
curlReq('views.open', $modalData);
} else {
# delete reminder
if (strpos($req->actions[0]->value, 'del_') === 0) {
$delId = intval(substr($req->actions[0]->value, 4));
$db = get_db();
$delQuery = $db->prepare('delete from reminders where uid=? and id_user=?;');
if ($delQuery->execute(array($req->user->id, $delId))) {
if($delQuery->rowCount() > 0) {
$responseText = ':white_check_mark: reminder #' . $delId . ' has been deleted...';
$updateData = json_encode([
"channel" => $req->channel->id,
"text" => '_#' . $delId . ' is deleted._',
"blocks" => null,
"ts" => $req->message->ts
]);
curlReq('chat.update', $updateData);
} else {
$responseText = ':information_source: no reminder found with id #' . $delId;
}
} else {
$responseText = ':x: an error occured! please try again some time...';
}
$data = json_encode([
"channel" => $req->channel->id,
"text" => $responseText
]);
curlReq('chat.postMessage', $data);
#show popup for reminder date edit
} else if (strpos($req->actions[0]->value, 'edit_popup_') === 0) {
$editId = intval(substr($req->actions[0]->value, 11));
$modalData = '{ "trigger_id": "'.$req->trigger_id.'", "view": { "type": "modal", "title": { "type": "plain_text", "text": "'.APP_NAME.'", "emoji": true }, "submit": { "type": "plain_text", "text": "Submit", "emoji": true }, "close": { "type": "plain_text", "text": "Cancel", "emoji": true }, "callback_id": "edit_'.$editId.'_'.$req->channel->id.'_'.$req->container->message_ts.'", "blocks": [ { "type": "input", "element": { "type": "plain_text_input" }, "label": { "type": "plain_text", "text": "Enter a valid date format to snooze the event #'.$editId.'", "emoji": true } } ] } }';
curlReq('views.open', $modalData);
#mark as complete
} else if(strpos($req->actions[0]->value, 'complete_') === 0) {
$updateId = intval(substr($req->actions[0]->value, 9));
$uid = $req->user->id;
$cid = $req->channel->id;
$db = get_db();
$updateQuery = $db->prepare('update reminders set done=1 where uid=? and id_user=?;');
if ($updateQuery->execute(array($uid, $updateId))) {
if($updateQuery->rowCount() > 0) {
$responseText = ':white_check_mark: reminder #' . $updateId . ' has been marked as complete...';
$updateData = json_encode([
"channel" => $cid,
"text" => '_#' . $updateId . ' is completed._',
"blocks" => null,
"ts" => $req->message->ts
]);
curlReq('chat.update', $updateData);
} else {
$responseText = ':information_source: reminder #' . $updateId . ' is already completed.';
}
} else {
$responseText = ':x: an error occured! please try again some time...';
}
$data = json_encode([
"channel" => $cid,
"text" => $responseText
]);
curlReq('chat.postMessage', $data);
#snooze
} else if(strpos($req->actions[0]->selected_option->value, 'snooze_') === 0) {
$input = explode('_', $req->actions[0]->selected_option->value);
$updateId = intval($input[1]);
$updateTime = $input[2];
$uid = $req->user->id;
$cid = $req->channel->id;
$updateData = json_encode([
"channel" => $cid,
"text" => '_#' . $updateId . ' is snoozed._',
"blocks" => null,
"ts" => $req->message->ts
]);
curlReq('chat.update', $updateData);
snoozeReminder($updateId, $uid, $cid, $updateTime);
#snooze with edit custom time menu
} else if(strpos($req->view->callback_id, 'edit_') === 0) {
$input = explode('_', $req->view->callback_id);
$updateId = intval($input[1]);
$cid = $input[2];
$d = (array) $req->view->state->values;
$d = (array) $d[array_keys($d)[0]];
$d = $d[array_keys($d)[0]];
$updateData = json_encode([
"channel" => $cid,
"text" => '_#' . $updateId . ' is snoozed._',
"blocks" => null,
"ts" => $input[3]
]);
curlReq('chat.update', $updateData);
snoozeReminder($updateId, $req->user->id, $cid, $d->value);
#remind message
} else if(strpos($req->view->callback_id, 'remindmsg_') === 0) {
$input = explode('_', $req->view->callback_id);
$user = $input[1];
$ts = $input[2];
$channel = $input[3];
$data = http_build_query([
"channel" => $channel,
"message_ts" => $ts
]);
$r = curlReq('chat.getPermalink', $data, 'application/x-www-form-urlencoded');
$url = json_decode($r);
$url = stripslashes($url->permalink);
$db = get_db();
$res = $db->prepare('select cid from reminders where uid=? order by id desc limit 1');
$res->execute(array($user));
$res->setFetchMode(PDO::FETCH_OBJ);
$s = $res->fetch();
$userChannel = $s->cid;
/*
$data = http_build_query([
"types" => 'im',
"limit" => "1",
"user" => $user
]);
$resp = curlReq('users.conversations', $data, 'application/x-www-form-urlencoded');
$resp = json_decode($resp);
$userChannel = $resp->channels[0]->id;
*/
$d = (array) $req->view->state->values;
$customTime = (array) $d[array_keys($d)[0]];
$customTime = $customTime[array_keys($customTime)[0]]->value;
$label = (array) $d[array_keys($d)[1]];
$label = $label[array_keys($label)[0]]->value;
if(!$label) $label = 'this message';
if(!$customTime) $customTime = 'tomorrow';
addNewReminder('"<' . $url . '|'. $label . '>" ' . $customTime, $userChannel, $user);
}
}
function snoozeReminder($updateId, $uid, $cid, $updateTime) {
$reminderTime = convTime(trim($updateTime), getDefaultHour($uid));
if(($reminderTime === FALSE) or strtotime($reminderTime) < strtotime('now')) {
$responseText = ':x: invalid snooze time!';
} else {
$db = get_db();
$updateQuery = $db->prepare('update reminders set alarm_sent=0, done=0, alarm_time=? where uid=? and id_user=?;');
if ($updateQuery->execute(array($reminderTime, $uid, $updateId))) {
if($updateQuery->rowCount() > 0) {
$responseText = ':white_check_mark: reminder #' . $updateId . ' has been snoozed until ' . date('H:i D, j M Y ', strtotime($reminderTime));
} else {
$responseText = ':information_source: reminder #' . $updateId . ' not found!';
}
} else {
$responseText = ':x: an error occured! please try again some time...';
}
}
$data = json_encode([
"channel" => $cid,
"text" => $responseText
]);
curlReq('chat.postMessage', $data);
}
function addNewReminder($inputRaw, $channel=null, $user=null) {
global $req;
$parts = explode('"', $inputRaw);
$last = array_pop($parts);
$parts = array(implode('"', $parts), $last);
$content = ltrim($parts[0], '"');
if(!$channel) $channel = $req->event->channel;
if(!$user) $user = $req->event->user;
$reminderTime = convTime(trim($parts[1]), getDefaultHour($user));
if($reminderTime !== FALSE) {
$db = get_db();
$insertQuery = $db->prepare('SET @v1 := (select 1+ifnull(max(id_user),0) from reminders where uid=?); insert into reminders(id_user, uid, cid, content, alarm_time) values(@v1, ?, ?, ?, ?);');
if ($insertQuery->execute(array($user, $user, $channel, $content, $reminderTime))) {
$responseText = ':white_check_mark: done! added the reminder at ' . date('H:i D, j M Y ', strtotime($reminderTime)) . '. type `list` or `l` for your upcoming reminders.';
} else {
$responseText = ':x: an error occured! please try again some time...';
}
} else {
$responseText = ':x: invalid date! type `help` for date formats.';
}
$data = json_encode([
"channel" => $channel,
"text" => $responseText
]);
curlReq('chat.postMessage', $data);
}
function getHelpMenuContent() {
return '{ "blocks": [ { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": ":calendar: *'.APP_NAME.'*" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`list` or `l` for listing incomplete reminders\n\n`list all` or `la` for listing all reminders\n\n" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "`prefs` for setting preferences\n\n" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "Add your reminder with the following syntax:\n\n`\"My new reminder\" dec 19 17:15`\n\n" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "\n\n\n" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "\n\n\n" } }, { "type": "section", "text": { "type": "mrkdwn", "text": ":information_source: *Date formats*" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "\n\n\n" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`dec 19` - _reminder at default hour_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`dec 19 12:15` - _reminder at specified hour_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`dec 19 21` - _reminder at the top of the specified hour (21:00)_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`16:30` - _reminder at the specified hour for today_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`16` - _reminder at the top of the specified hour for today_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`tomorrow` or `tm` - _reminder at default hour for tomorrow_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`tomorrow 14` or `tm 14` - _reminder at the top of the specified hour for tomorrow (14:00)_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`tomorrow 14:15` or `tm 14:15` - _reminder at the specified hour for tomorrow_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`mon` to `sun` - _reminder at the default hour in the next occurrence of the specified day_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`mon 14` - _reminder at the top of the specified hour in the next occurrence of the specified day (14:00)_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`mon 14:15` - _reminder at the specified hour in the next occurrence of the specified day_" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "`in 3h` or `in 30m` or `in 1h 30m` - _relative times to now_" } } ] }';
}