-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbot.js
110 lines (93 loc) · 2.43 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
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
105
106
107
108
109
110
const path = require('path')
const Telegraf = require('telegraf')
const session = require('telegraf/session')
const I18n = require('telegraf-i18n')
const io = require('@pm2/io')
const {
db
} = require('./database')
const {
stats
} = require('./middlewares')
const {
getUser,
getChannel
} = require('./helpers')
const rpsIO = io.meter({
name: 'req/sec',
unit: 'update'
})
const bot = new Telegraf(process.env.BOT_TOKEN, {
telegram: { webhookReply: false },
handlerTimeout: 1
})
;(async () => {
console.log(await bot.telegram.getMe())
})()
bot.use((ctx, next) => {
next().catch((error) => {
console.log('Oops', error)
})
return true
})
bot.use(stats)
bot.use((ctx, next) => {
ctx.db = db
return next()
})
bot.use((ctx, next) => {
rpsIO.mark()
ctx.telegram.oCallApi = ctx.telegram.callApi
ctx.telegram.callApi = (method, data = {}) => {
const startMs = new Date()
return ctx.telegram.oCallApi(method, data).then((result) => {
console.log(`end method ${method}:`, new Date() - startMs)
return result
})
}
return next()
})
bot.command('json', ({ replyWithHTML, message }) => replyWithHTML('<code>' + JSON.stringify(message, null, 2) + '</code>'))
const i18n = new I18n({
directory: path.resolve(__dirname, 'locales'),
defaultLanguage: '-'
})
bot.use(i18n.middleware())
bot.use(session({
getSessionKey: (ctx) => {
if (ctx.from && ctx.chat) {
return `${ctx.from.id}:${ctx.chat.id}`
} else if (ctx.from && ctx.inlineQuery) {
return `${ctx.from.id}:${ctx.from.id}`
} else if (ctx.chat && ctx.chat.type === 'channel') {
return `channel:${ctx.chat.id}`
}
return null
}
}))
bot.use(async (ctx, next) => {
if (ctx.from) await getUser(ctx)
if (ctx.channelPost) {
ctx.session.channelInfo = await getChannel(ctx.chat)
ctx.session.channelInfo.available = true
}
if (ctx.callbackQuery) {
ctx.state.answerCbQuery = []
}
return next(ctx).then(() => {
if (ctx.session && ctx.session.userInfo) ctx.session.userInfo.save()
if (ctx.session && ctx.session.channelInfo) ctx.session.channelInfo.save()
if (ctx.callbackQuery) return ctx.answerCbQuery(...ctx.state.answerCbQuery)
})
})
require('./handlers')(bot)
bot.use((ctx, next) => {
ctx.state.emptyRequest = true
return next()
})
db.connection.once('open', async () => {
console.log('Connected to MongoDB')
await bot.launch().then(() => {
console.log('bot start polling')
})
})