-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
87 lines (74 loc) · 2.25 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
'use strict'
const path = require('path')
const Bot = require('telegraf')
const url = require('url')
const inGroupsOnlyMentionsAndReplies = require('./lib/in-groups-only-mentions-and-replies')
const textWithoutMention = require('./lib/text-without-mention')
const logging = require('./lib/logging')
const session = require('./lib/session')
const storage = require('./lib/storage')
const command = require('./lib/command')
const help = require('./commands/help')
const departures = require('./commands/departures')
const journeys = require('./commands/journeys')
const nearby = require('./commands/nearby')
const commands = {
help, h: help,
abfahrt: departures, a: departures,
route: journeys, r: journeys,
nearby, n: nearby
}
const TOKEN = process.env.TOKEN
if (!TOKEN) {
console.error('Missing TOKEN env var.')
process.exit(1)
}
const pathToDb = path.join(__dirname, 'vbb-telegram.ldb')
const bot = new Bot(TOKEN)
bot.telegram.getMe().then(({username}) => {
bot.options.username = username
})
bot.use(inGroupsOnlyMentionsAndReplies)
bot.use(textWithoutMention)
bot.use(logging)
bot.use(session(pathToDb))
bot.use(command)
bot.use(storage)
bot.use((ctx, next) => {
const cmd = ctx.command || ctx.prevCommand || 'help'
ctx.storage.getData = ctx.storage.createGetData(cmd)
ctx.storage.putData = ctx.storage.createPutData(cmd)
const handler = commands[cmd] || commands.help
return handler(ctx, next)
})
if (process.env.NODE_ENV === 'dev') {
console.info('using polling')
bot.telegram.deleteWebhook()
.then(() => bot.startPolling())
.catch(console.error)
} else {
console.info('using web hook')
const WEB_HOOK_HOST = process.env.WEB_HOOK_HOST
if (!WEB_HOOK_HOST) {
console.error('Missing WEB_HOOK_HOST env var.')
process.exit(1)
}
const WEB_HOOK_PATH = process.env.WEB_HOOK_PATH
if (!WEB_HOOK_PATH) {
console.error('Missing WEB_HOOK_PATH env var.')
process.exit(1)
}
const WEB_HOOK_PORT = process.env.WEB_HOOK_PORT && parseInt(process.env.WEB_HOOK_PORT)
if (!WEB_HOOK_PORT) {
console.error('Missing WEB_HOOK_PORT env var.')
process.exit(1)
}
bot.webhookReply = false
bot.telegram.setWebhook(url.format({
protocol: 'https',
host: WEB_HOOK_HOST,
pathname: WEB_HOOK_PATH
}))
bot.startWebhook(WEB_HOOK_PATH, null, WEB_HOOK_PORT)
}
bot.catch(console.error)