-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
81 lines (66 loc) · 2.01 KB
/
main.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
80
const express = require('express');
const { createEventAdapter } = require('@slack/events-api');
const { WebClient } = require('@slack/web-api');
const moment = require('moment-timezone');
//load env with dotenv
require('dotenv').config();
const app = express();
const web = new WebClient(process.env.SLACK_BOT_TOKEN);
const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET);
//route
app.use('/slack/events', slackEvents.requestListener());
//event listener
slackEvents.on('message', async (event) => {
const messageText = event.text;
const user = event.user;
const date = extractDate(messageText); //time in Istanbul timezone
if (date) {
console.log("date info catched");
const timeInLondon = moment(date).tz('Europe/London').format('LLL'); // receiver timezone
const users = await web.conversations.members({
channel: event.channel
});
//filter out user from users - optional
/*
const index = users.members.indexOf(user);
if (index > -1) {
users.members.splice(index, 1);
}
*/
try {
await Promise.all(users.members.map(async (user) => {
await sendEphemeralMessage(event.channel, user, `Time in London: ${timeInLondon}`);
console.log('Time in London: ', timeInLondon);
console.log('Time in Istanbul: ', date);
console.log("ephmeral message sent to user");
console.dir(user);
}));
} catch (error) {
console.error(error);
}
}
});
async function sendEphemeralMessage(channel, user, message) {
try {
await web.chat.postEphemeral({
text: message,
channel,
user
});
} catch (error) {
console.error(error);
}
}
function extractDate(message) {
const regex = /(\d{4}-\d{2}-\d{2}\ \d{2}:\d{2})/; // YYYY-MM-DD HH:MM
const match = message.match(regex);
if (match) {
return moment(match[0]);
} else {
return null;
}
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});