-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
65 lines (49 loc) · 1.84 KB
/
bot.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
import TelegramBot from 'node-telegram-bot-api';
import {findTimeZone, getZonedTime} from 'timezone-support';
import {getStore} from './store.js';
let bot = null;
let chatId = null;
const getDateTimeString = (dt) => {
return `<i>Stand: ${dt.day}.${dt.month} - ${dt.hours}:${dt.minutes} Uhr</i>`;
};
const getObjectAvailabilityString = (objects) => {
return objects.reduce((acc, {object, free}) => {
let result = acc ? acc + '\n\n' : acc;
result += `<b>${object}:</b>\n${free} freie Plätze`;
return result;
}, '');
};
const getRequestParametersString = (abreise, anreise) => {
return `<i>Anreise: ${anreise}\nAbreise: ${abreise}</i>`;
};
export const initBot = ({token, newChatId}) => {
bot = new TelegramBot(token, {polling: true});
chatId = newChatId;
bot.onText(/\/init/, (msg) => {
console.log(msg.chat.id);
});
bot.onText(/\/status/, sendStatus);
};
export const sendStatus = () => {
if (!bot) {
console.log('Error: Bot not initialized');
return;
}
if (!chatId) {
console.log('Error: No Chat ID');
return;
}
const {timestamp, abreise, anreise, objects} = getStore();
var nativeDate = new Date(timestamp);
const berlin = findTimeZone('Europe/Berlin');
const berlinTime = getZonedTime(nativeDate, berlin);
let message =
`🤖 So sieht es gerade aus. Ich sage Bescheid, wenn es neue Infos gibt. Schreibe /status in den Chat, wenn du sicher gehen willst wie der Stand ist.\n\n` +
`${getDateTimeString(berlinTime)}\n` +
`${getRequestParametersString(abreise, anreise)}\n\n` +
`${getObjectAvailabilityString(objects)}\n\n` +
`<a href="https://buchen.tooc24.de/customer/rerik/index.php">Link zur Buchungsseite</a>`;
bot.sendMessage(chatId, message, {
parse_mode: 'HTML',
});
};