-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduledTasks.js
46 lines (37 loc) · 1.52 KB
/
scheduledTasks.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
// scheduledTasks.js
import schedule from 'node-schedule';
import { fetchMeetupsLogic } from './handlers/meetupHandlers/meetupDisplayingHandler.js';
import { deleteMessage, sendAndStoreMessage, editAndStoreMessage } from './utils/helpers.js';
import config from './bot/config.js';
const scheduleWeeklyMeetupPost = (bot) => {
schedule.scheduleJob('0 7 * * 1', async () => {
console.log('Running scheduled weekly meetup post');
await postWeeklyMeetups(bot);
});
};
const postWeeklyMeetups = async (bot) => {
let chatId = config.MEETUP_CHAT_ID;
if (!chatId) {
console.error('MEETUP_CHAT_ID is not set in the environment variables');
return;
}
try {
const chatInfo = await bot.getChat(chatId);
let pinnedMessageId = chatInfo.pinned_message ? chatInfo.pinned_message.message_id : null;
if (pinnedMessageId) {
await bot.unpinChatMessage(chatId);
await deleteMessage(bot, chatId, pinnedMessageId);
}
const meetupMessage = await fetchMeetupsLogic('dieseWoche');
if (meetupMessage) {
let sentMessage = await sendAndStoreMessage(bot, chatId, meetupMessage, {
parse_mode: 'HTML',
disable_web_page_preview: true
}, 'pinnedMeetupMessageId');
await bot.pinChatMessage(chatId, sentMessage.message_id);
}
} catch (error) {
console.error('Error posting weekly meetups:', error);
}
};
export { scheduleWeeklyMeetupPost, postWeeklyMeetups };