-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (44 loc) · 1.26 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
import TelegramBot from 'node-telegram-bot-api';
import { reminders } from './reminders.js';
import cron from 'node-cron';
import env from 'dotenv';
const config = env.config();
const token = config.parsed?.TELEGRAM_TOKEN;
if (!token)
throw new Error(
'No token specified in config. Expected parameter TELEGRAM_TOKEN to be present.'
);
const bot = new TelegramBot(token, { polling: true });
bot.onText(/^\/start/, async (message) => {
await bot.sendMessage(
message.chat.id,
`This is an internal use only bot. Please ask an admin to configure the bot to use the chat ID \`${message.chat.id}\` for reminders in this group.`,
{
parse_mode: 'Markdown',
reply_to_message_id: message.message_id,
}
);
});
reminders.forEach((reminder, index) => {
console.log(
new Date().toISOString(),
`Scheduling reminder #${index} with CRON "${reminder.cron}".`
);
cron.schedule(reminder.cron, async () => {
console.log(
new Date().toISOString(),
'Sending message to',
JSON.stringify(reminder.chatIds),
`based on CRON #${index} (${reminder.cron}):`,
JSON.stringify(reminder.message)
);
await Promise.all(
reminder.chatIds.map((chatId) =>
bot.sendMessage(chatId, reminder.message, {
parse_mode: 'Markdown',
})
)
);
});
});
export {};