-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (85 loc) · 2.99 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
const TelegramBot = require('node-telegram-bot-api')
const { getRouteInfo, getForecast } = require('./transportService')
const messages = require('./messages')
const renderImage = require('./imageRenderer')
const { inRow } = require('./utils')
const { routeRenderMiddlware, isEmptyPage, extractContent } = require('./parsing')
const { createHtmlSchedule, createHtmlForecast, isRiverTransport, getDirections } = require('./riverTransportService')
const token = process.env.tgtoken
const bot = new TelegramBot(token, { polling: true })
bot.onText(/\/start/, msg => {
bot.sendMessage(msg.chat.id, messages.info, { parse_mode: 'Markdown' })
})
const formatCallback = (arr, chatId) => arr.map(a => ({
text: a[0],
callback_data:
[a[1], chatId].join('_')
}))
const sendRiver = (message, chatId) => {
const { html, callbacks } = createHtmlSchedule(message);
return renderImage(html).then(data => {
const options = {
reply_markup: JSON.stringify({
inline_keyboard: inRow(formatCallback(callbacks, chatId), 2)
})
}
bot.sendPhoto(chatId, data.imagePath, options)
})
}
bot.on('message', (msg) => {
if (msg.text === '/start') return;
const chatId = msg.chat.id;
const formattedMessage = msg.text.toLowerCase().trim()
if (formattedMessage === 'волга') {
const options = {
reply_markup: JSON.stringify({
inline_keyboard: inRow(formatCallback(getDirections().map(d => [d, 'volga:' + d]), chatId),1)
})
}
return bot.sendMessage(chatId, messages.chooseDirection, options)
}
if (isRiverTransport(formattedMessage)) {
return sendRiver(formattedMessage, chatId)
}
getRouteInfo(formattedMessage).then(response => {
const htmlContent = extractContent(response.toString())
if (isEmptyPage(htmlContent)) {
return bot.sendMessage(chatId, messages.notFound)
}
renderImage(htmlContent, routeRenderMiddlware).then(data => {
const options = {
reply_markup: JSON.stringify({
inline_keyboard: inRow(formatCallback(data.middlewareData, chatId), 3)
})
}
bot.sendPhoto(chatId, data.imagePath, options)
})
}).catch(err => {
bot.sendMessage(chatId, err)
})
})
bot.on('callback_query', (msg) => {
const [link, chatId] = msg.data.split('_')
if (link.startsWith('volga')) {
return sendRiver(link.split(':')[1], chatId)
}
if (link.startsWith('river')) {
const parts = link.split(':')
const destination = parts[2]
const direction = parts[1]
const routeNumber = parseInt(parts[3])
return renderImage(
createHtmlForecast(destination, direction, routeNumber)).then(data => {
bot.sendPhoto(chatId, data.imagePath)
})
}
getForecast(link).then(response => {
const htmlContent = extractContent(response.toString())
if (isEmptyPage(htmlContent)) {
return bot.sendMessage(chatId, messages.error)
}
renderImage(htmlContent).then(data => {
bot.sendPhoto(chatId, data.imagePath)
})
})
})