forked from telegraf/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself-cleaning-scenes.js
More file actions
52 lines (44 loc) · 1.55 KB
/
self-cleaning-scenes.js
File metadata and controls
52 lines (44 loc) · 1.55 KB
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
const Telegraf = require('telegraf')
const session = require('telegraf/session')
const Markup = require('telegraf/markup')
const Stage = require('telegraf/stage')
const Scene = require('telegraf/scenes/base')
const { enter } = Stage
const sceneCleaner = () => async (ctx) => {
ctx.scene.state.messages.forEach(({ message_id: id }) => {
try {
ctx.deleteMessage(id)
} catch (error) {
console.log(error)
}
})
}
const replyKeyboard = () => Markup.keyboard([
Markup.button('First'),
Markup.button('Second')
]).extra()
const firstScene = new Scene('first')
.enter(async (ctx) => {
const messages = []
messages.push(await ctx.reply('First scene, first message'))
messages.push(await ctx.reply('First scene, second message'))
messages.push(await ctx.reply('First scene, third message'), replyKeyboard())
ctx.scene.state.messages = messages
})
.leave(sceneCleaner())
const secondScene = new Scene('second')
.enter(async (ctx) => {
const messages = []
messages.push(await ctx.reply('Second scene, first message'))
messages.push(await ctx.reply('Second scene, second message'))
messages.push(await ctx.reply('Second scene, third message'), replyKeyboard())
ctx.scene.state.messages = messages
})
.leave(sceneCleaner())
const bot = new Telegraf(process.env.BOT_TOKEN)
const stage = new Stage([firstScene, secondScene], { ttl: 10 })
bot.use(session())
bot.use(stage.middleware())
bot.start(async (ctx) => enter('first'))
bot.hears(/^First|Second$/, async (ctx) => enter(ctx.match[0].toLowerCase()))
bot.launch()