-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
262 lines (227 loc) · 7.72 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
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
const Telegraf = require('telegraf');
const { Markup } = require('telegraf');
const express = require('express');
const expressApp = express();
const CronJob = require('cron').CronJob;
const moment = require('moment-timezone');
const monk = require('monk');
const dbUrl = process.env.MONGODB_URI;
const db = monk(dbUrl);
const users = db.get('users');
const randray = require('randray');
const msg = require('./messages');
const app = new Telegraf(process.env.BOT_TOKEN);
const PORT = process.env.PORT || 8443;
const notifyOpts = Markup.inlineKeyboard([
[
Markup.callbackButton('5 min', 'onPostpone5'),
Markup.callbackButton('10 min', 'onPostpone10'),
// hack cause dynos will sleep after 30 minutes
Markup.callbackButton('30 min', 'onPostpone29'),
],
[
Markup.callbackButton('Done', 'onDone'),
Markup.callbackButton('Skip', 'onSkip'),
],
])
.extra();
const positiveSmiles = ['👍', '👌', '😎', '😽', '👏', '💪', '💚', '🏆', '🎉'];
const negativeSmiles = ['😢', '😒', '😫', '😩', '😠', '🙁', '😿', '👓', '😞'];
const cronJobHash = new Map();
// set webhooks and up express
app.telegram.setWebhook(`${process.env.URL}bot${process.env.BOT_TOKEN}`);
expressApp.use(app.webhookCallback(`/bot${process.env.BOT_TOKEN}`));
app.telegram.getWebhookInfo().then(info => console.log('wh info: ', info));
expressApp.get('/', (req, res) => {
res.send('Hey babe... You have nice eye color...');
});
expressApp.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// set jobs for all notifications
db.then(() => {
console.log('db connected');
users.find({}).each((user) => {
user.notifications.forEach((time) => {
setCronJob(user.chat_id, time, user.timezone);
});
}).then(() => {
console.log('hope it is set');
});
});
// set up notification
app.hears(/^\d\d/, (ctx) => {
const time = ctx.message.text.match(/^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/);
if (time) {
const timeObj = { h: time[1], m: time[2], full: time[0] };
users.findOne({ chat_id: ctx.chat.id }).then((user) => {
if (user) {
users.update({ chat_id: ctx.chat.id }, { $push: { notifications: timeObj } })
.then(() => {
if (!user.timezone) {
return ctx.reply(msg.askLocation);
}
setCronJob(ctx.chat.id, timeObj, user.timezone);
ctx.reply(`${timeObj.full} set`);
});
} else {
users.insert({
chat_id: ctx.chat.id,
notifications: [timeObj],
waitLocation: true,
})
.then(() => ctx.reply(msg.askLocation));
}
});
} else {
return ctx.reply(msg.cantRecongnizeTime);
}
});
// set up commands
app.command('start', ctx =>
app.telegram.sendMessage(ctx.chat.id, msg.start, { parse_mode: 'HTML' }),
);
app.command('e_tz', (ctx) => {
const tz = ctx.message.text.split(' ').slice(1).join();
if (tz) {
users.findOne({ chat_id: ctx.chat.id }).then((user) => {
updTz(tz, user);
});
} else {
ctx.reply(msg.cantRecongnizeLocation);
}
});
app.command('rm', (ctx) => {
const time = ctx.message.text.match(/([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/);
if (time) {
users.findOne({ chat_id: ctx.chat.id }).then((user) => {
users.update({ chat_id: ctx.chat.id }, { $pull: { notifications: { full: time[0] } } })
.then((result) => {
const resultMsg = result.nModified ? `${time[0]} removed` : 'Ops! Notification not found';
stopJob(user.chat_id, time[0]);
app.telegram.sendMessage(user.chat_id, resultMsg);
})
.catch(() => {
ctx.reply(msg.oops);
});
});
} else {
ctx.reply(msg.cantRecongnizeTime);
}
});
app.command('ls', ctx =>
users.findOne({ chat_id: ctx.chat.id }).then((user) => {
const notif = [];
if (!user) {
ctx.reply(msg.haventNotifications);
}
user.notifications.forEach((time) => {
notif.push(`${time.full}`);
});
if (!notif.length) {
ctx.reply(msg.haventNotifications);
} else {
ctx.reply(`Your notifications: ${notif.join(', ')}. Your timezone: ${user.timezone}`);
}
}),
);
app.command('help', ctx =>
app.telegram.sendMessage(ctx.chat.id, msg.helpMsg, { parse_mode: 'HTML' }),
);
app.action('onDone', ctx => {
ctx.editMessageText(msg.done);
ctx.reply(randray(positiveSmiles));
});
app.action('onSkip', ctx => {
ctx.editMessageText(msg.skipped);
ctx.reply(randray(negativeSmiles));
});
app.action(/^onPostpone/, (ctx) => {
const minutes = parseInt(ctx.callbackQuery.data.match(/\d+/)[0], 10);
users.findOne({ chat_id: ctx.chat.id }).then((user) => {
const time = moment().tz(user.timezone).add(minutes, 'm');
setCronJob(user.chat_id, time, user.timezone);
ctx.editMessageText(`${msg.notifShort}`);
app.telegram.sendMessage(ctx.chat.id, `Postponed for ${minutes} min`);
});
});
// set up other reactions
app.on('sticker', ctx => ctx.reply('👍'));
app.hears(/(hi)|(hey)/i, ctx => ctx.reply('Hey there!'));
app.on('message', (ctx) => {
users.findOne({ chat_id: ctx.chat.id })
.then((user) => {
if (user && user.waitLocation) {
updTz(ctx.message.text, user);
} else {
ctx.reply(msg.notUnderstand);
}
});
});
function getJobId(chat_id, fulltime) {
return `${chat_id}${fulltime}`;
}
function updTz(zone, user) {
const tz = moment.tz.names().find(z => z.includes(zone));
if (tz) {
users.update(
{ chat_id: user.chat_id },
{ $set: {
waitLocation: false,
timezone: tz,
},
},
)
.then((result) => {
const resMsg = result.nModified ? `${tz} set` : msg.oops;
app.telegram.sendMessage(user.chat_id, resMsg);
user.notifications.forEach((time) => {
stopJob(user.chat_id, time.full);
setCronJob(user.chat_id, time, tz);
});
});
} else {
app.telegram.sendMessage(user.chat_id, msg.cantRecongnizeLocation);
}
}
function stopJob(chat_id, fulltime) {
if (cronJobHash.has(getJobId(chat_id, fulltime))) {
cronJobHash.get(getJobId(chat_id, fulltime)).stop();
}
}
function setCronJob(chat_id, time, tz) {
// for weekdays only * * * * 1-5
const cronTime = moment.isMoment(time) ? time.toDate() : `${time.m} ${time.h} * * 1-5`;
const job = new CronJob({
cronTime,
onTick() {
app.telegram.sendMessage(chat_id, msg.notif, notifyOpts);
},
start: true,
timeZone: tz,
});
cronJobHash.set(getJobId(chat_id, time.full), job);
}
// MVP!!!
// /help
// /e_tz <newTz>- edit timezone
// /rm <HH:MM> or Key - rm notification
// /ls - list notifications
// db schema:
// {
// chat_id: int
// notifications: [{h:12, m:30, full: '12:30'}, {h:17, m:0, full: '17:00'}]
// timezone: 'Europe/Minsk'
// waitLocation: false
// }
/* TODO:
- maybe add smth like 'rm all'??
- split somehow and prettify for less spaghettiness and better readability
- write temp notifications to bd??
- tests espec. for e_tz
- send location by btn eg https://github.com/telegraf/telegraf/blob/develop/examples/keyboard-bot.js
- add emojis for eternal beauty
- add some beautiful gif to readme mayb?
- add some default exercises
*/
module.exports = app;