forked from telegraf/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-router-bot.js
61 lines (54 loc) · 1.68 KB
/
custom-router-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
const Telegraf = require('../../')
const Router = require('../../router')
const Extra = require('../../extra')
const session = require('../../session')
const markup = Extra
.HTML()
.markup((m) => m.inlineKeyboard([
m.callbackButton('Add 1', 'add:1'),
m.callbackButton('Add 10', 'add:10'),
m.callbackButton('Add 100', 'add:100'),
m.callbackButton('Subtract 1', 'sub:1'),
m.callbackButton('Subtract 10', 'sub:10'),
m.callbackButton('Subtract 100', 'sub:100'),
m.callbackButton('Clear', 'clear')
], {columns: 3}))
const calculator = new Router((ctx) => {
if (!ctx.callbackQuery.data) {
return Promise.resolve()
}
const parts = ctx.callbackQuery.data.split(':')
return Promise.resolve({
route: parts[0],
state: {
amount: parseInt(parts[1], 10) || 0
}
})
})
calculator.on('add', (ctx) => {
ctx.session.value = (ctx.session.value || 0) + ctx.state.amount
return editText(ctx)
})
calculator.on('sub', (ctx) => {
ctx.session.value = (ctx.session.value || 0) - ctx.state.amount
return editText(ctx)
})
calculator.on('clear', (ctx) => {
ctx.session.value = 0
return editText(ctx)
})
function editText (ctx) {
if (ctx.session.value === 42) {
return ctx.answerCbQuery('Answer to the Ultimate Question of Life, the Universe, and Everything', true)
.then(() => ctx.editMessageText('🎆'))
}
return ctx.editMessageText(`Value: <b>${ctx.session.value}</b>`, markup).catch(() => undefined)
}
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.use(session({ ttl: 10 }))
bot.start((ctx) => {
ctx.session.value = 0
return ctx.reply(`Value: <b>${ctx.session.value}</b>`, markup)
})
bot.on('callback_query', calculator)
bot.startPolling()