-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (69 loc) · 2.63 KB
/
index.js
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
require("dotenv").config()
const chrono = require('chrono-node');
const { App } = require('@slack/bolt');
const { humanReadableDiff } = require('./utils');
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
(async () => {
app.action('convert_posted_date', async ({ body, ack, respond, }) => {
await ack();
const user = (await app.client.users.info({
user: body.user.id
})).user
const tz = user?.tz
const date = new Date(body.actions[0].value)
await app.client.chat.postEphemeral({
user: body.user.id,
channel: body.channel.id,
thread_ts: body.message_ts,
text: `This will happen on ${date.toLocaleString('en-US', { timeZone: tz, timeStyle: "short", dateStyle: "long" })} (${user.tz_label}) *or* ${humanReadableDiff(date, new Date())}`
})
});
app.command('/timepost', async ({ ack, command, respond, say, body }) => {
await ack()
const user = (await app.client.users.info({
user: body.user_id
})).user
const tz = user?.tz
const date = chrono.parseDate(command.text, {
timezone: tz,
})
if (!date) return await respond("Unable to parse date. Please try again.")
await say({
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `This will happen on ${date.toLocaleString('en-US', { timeZone: 'Etc/Utc', timeStyle: "short", dateStyle: "long" })} (UTC)\nOriginal time (${user.tz_label}): ${date.toLocaleString('en-US', { timeZone: tz, timeStyle: "short", dateStyle: "long" })}`
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Convert this time into your timezone"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Convert",
"emoji": true
},
"value": date.toISOString(),
"action_id": "convert_posted_date"
}
}
]
})
})
await app.start();
})();
process.on("unhandledRejection", (error) => {
console.error(error);
});