Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Telegraf bug
const Telegraf = require('telegraf')
const session = require('telegraf/session')
const Stage = require('telegraf/stage')
const Scene = require('telegraf/scenes/base')
const mongoose = require('mongoose')
require('dotenv').config()
const scheme = new mongoose.Schema({
key: String,
session: {},
}, {
versionKey: false,
timestamps: { createdAt: true, updatedAt: true }
})
const SessionModel = mongoose.model('Session', scheme, 'sessions')
const connect = () => mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
}).then(() => console.log('Connected'))
const sessionMongooseConfig = {
property: 'sceneSession',
store: {
get: (key) => SessionModel.findOne({ key }),
set: (key, session) => {
console.log('SET', session.session);
return SessionModel.updateOne({ key }, { $set: session }, { upsert: true })
}
},
}
// Переход на сцену second должен перекинуть на сцену first
const run = async () => {
await connect()
// First scene
const firstScene = new Scene('first')
.enter((ctx) => ctx.reply('first scene'))
.command('back', (ctx) => ctx.scene.leave())
.on('message', (ctx) => ctx.reply('first scene'))
// Second scene
const secondScene = new Scene('second')
.enter(async (ctx) => {
await ctx.reply('second scene')
ctx.scene.enter('first')
})
// Если без async/await то работает
// .enter((ctx) => {
// ctx.reply('second scene')
// ctx.scene.enter('first')
// })
.command('back', (ctx) => ctx.scene.leave())
.on('message', (ctx) => ctx.reply('second scene'))
const bot = new Telegraf(process.env.BOT_TOKEN)
const stage = new Stage([firstScene, secondScene], {
sessionName: 'sceneSession',
})
bot.use(session(sessionMongooseConfig))
bot.use(session())
bot.use(stage.middleware())
bot.start((ctx) => ctx.reply('/first or /second'))
bot.command('first', (ctx) => ctx.scene.enter('first'))
bot.command('second', (ctx) => ctx.scene.enter('second'))
bot.launch()
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.