-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
368 lines (339 loc) · 9.14 KB
/
app.js
File metadata and controls
368 lines (339 loc) · 9.14 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
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
// Require the Bolt package (github.com/slackapi/bolt)
const { App } = require("@slack/bolt");
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
appToken: process.env.SLACK_APP_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
// socketMode: true,
port: process.env.PORT || 3000,
});
(async () => {
// Start your app
await app.start();
console.log("⚡️ Bolt app is running in app.js!");
})();
app.command("/status-create", async ({ ack, payload, context }) => {
ack();
// console.log(payload);
try {
const result = await app.client.views.open({
trigger_id: payload.trigger_id,
view: {
callback_id: "status-create-view",
type: "modal",
title: {
type: "plain_text",
text: "My App",
},
close: {
type: "plain_text",
text: "Close",
},
submit: {
type: "plain_text",
text: "Submit",
},
blocks: [
{
block_id: "instructions",
type: "section",
text: {
type: "plain_text",
text: "Hello, please fill out the information below.",
emoji: true,
},
},
{
block_id: "conversations_select",
optional: false,
type: "input",
element: {
type: "conversations_select",
action_id: "conversations_select-action",
},
label: {
type: "plain_text",
text: "Select a user or channel:",
emoji: true,
},
},
{
block_id: "datepicker",
optional: false,
type: "input",
element: {
type: "datepicker",
action_id: "datepicker-action",
},
label: {
type: "plain_text",
text: "Select a date:",
emoji: true,
},
},
{
block_id: "timepicker",
optional: false,
type: "input",
element: {
type: "timepicker",
action_id: "timepicker-action",
},
label: {
type: "plain_text",
text: "Select a time:",
emoji: true,
},
},
{
block_id: "plain_text_input",
optional: false,
type: "input",
element: {
type: "plain_text_input",
multiline: true,
action_id: "plain_text_input-action",
},
label: {
type: "plain_text",
text: "Enter your task here",
emoji: true,
},
},
],
},
});
} catch (error) {
console.error(error);
}
});
//called on modal submission
app.view("status-create-view", async ({ ack, body, view, client, logger }) => {
await ack();
// console.log(view.state.values);
const recipient =
view.state.values.conversations_select["conversations_select-action"]
.selected_conversation;
const date = view.state.values.datepicker["datepicker-action"].selected_date;
const time = view.state.values.timepicker["timepicker-action"].selected_time;
const message =
view.state.values.plain_text_input["plain_text_input-action"].value;
//need to convert date and time into unix time for chat.scheduleMessage
//also need to convert the user's entered time to UTC due to user time zones
const userInfo = await app.client.users.info({
token: app.token,
user: body.user.id,
});
const userTimeZoneOffsetInSeconds = -userInfo.user.tz_offset;
const epochTime =
Math.floor(Date.parse(date + " " + time) / 1000.0) +
userTimeZoneOffsetInSeconds;
//TODO store status
const reminder = {
ownerId: body.user.id,
recipientId: recipient,
time: epochTime,
message: message,
};
// console.log(reminder);
await remindUserAtTime(reminder);
});
app.command("/testreminder", async ({ ack, payload, context }) => {
await ack();
// console.log(payload);
await remindUserAtTime({
id: 1,
message: "Reminder",
//11 seconds from now
time: Math.floor(new Date().getTime() / 1000.0) + 11,
/*
User/Channel Ids
Logan: U03V1P4617W
Chris: U03V1N69NGL
#bot_testing: C03UUS3U9P0
*/
ownerId: payload.user_id,
recipientId: payload.user_id,
});
});
async function remindUserAtTime(reminder) {
try {
const isChannel = reminder.recipientId[0] === "C";
const ping = isChannel ? "@here" : `<@${reminder.recipientId}>`;
await app.client.chat.scheduleMessage({
token: app.token,
channel: reminder.recipientId,
post_at: reminder.time,
text: `${reminder.message}`,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Hi, ${ping}! Here is your reminder:`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `${reminder.message}`,
},
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "Done",
},
style: "primary",
value: `${JSON.stringify({
reminder: reminder,
status: "Done",
})}`,
action_id: "statusDone",
},
{
type: "button",
text: {
type: "plain_text",
text: "In progress",
},
value: `${JSON.stringify({
reminder: reminder,
status: "In progress",
})}`,
action_id: "statusInProgress",
},
],
},
],
});
} catch (error) {
console.error(error);
}
}
app.action("statusDone", async ({ ack, body, context }) => {
ack();
// console.log(body);
const parsedValue = JSON.parse(body.actions[0].value);
const reminder = parsedValue.reminder;
const status = parsedValue.status;
await notifyUser(reminder.recipientId, reminder, status);
});
app.action("statusInProgress", async ({ ack, body, context }) => {
ack();
// console.log(body);
const parsedValue = JSON.parse(body.actions[0].value);
const reminder = parsedValue.reminder;
const status = parsedValue.status;
await notifyUser(reminder.recipientId, reminder, status);
});
async function notifyUser(userId, reminder, status) {
let reminderContext = `Reminder created by <@${reminder.ownerId}>`;
const forChannel = reminder.recipientId[0] === "C";
if (forChannel) {
reminderContext += ` for <#${reminder.channelId}>`;
}
try {
await app.client.chat.postMessage({
token: app.token,
channel: userId,
text: reminder.message,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Hi, <@${userId}>! This is a notification that the following reminder is now *${status}*:`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `${reminder.message}`,
},
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: reminderContext,
},
],
},
],
});
} catch (error) {
console.error(error);
}
}
// app.command("/statuslist", async ({ ack, payload, context }) => {
// ack();
// await listReminders(payload, context);
// });
// async function listReminders(body, context) {
// try {
// //TODO get reminders
// const reminders = [
// {
// text: "Reminder 1",
// },
// {
// text: "Reminder 2",
// },
// ];
// //TODO nicer formatting
// const reminder_blocks = reminders.map((reminder) => ({
// type: "section",
// text: {
// type: "mrkdwn",
// text: reminder.message,
// },
// }));
// await app.client.chat.postMessage({
// token: app.token,
// channel: body.channel_id,
// blocks: [
// {
// type: "section",
// text: {
// type: "mrkdwn",
// text: `Hi, <@${body.user_id}>! Here are your reminders:`,
// },
// },
// ...reminder_blocks,
// {
// type: "actions",
// //TODO actions for buttons
// elements: [
// {
// type: "button",
// text: {
// type: "plain_text",
// text: "Done",
// },
// style: "primary",
// value: "status_done",
// },
// {
// type: "button",
// text: {
// type: "plain_text",
// text: "In progress",
// },
// value: "status_in_progress",
// },
// ],
// },
// ],
// });
// } catch (error) {
// console.error(error);
// }
// }