Skip to content

Instantly share code, notes, and snippets.

@RealPeha
Last active February 7, 2020 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RealPeha/b8584817808448ef5028e8e77aef0325 to your computer and use it in GitHub Desktop.
Save RealPeha/b8584817808448ef5028e8e77aef0325 to your computer and use it in GitHub Desktop.
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('select', Telegraf.Markup.inlineKeyboard([[Telegraf.Markup.callbackButton('second', 'second')]]).extra()))
bot.on('callback_query', (ctx) => {
ctx.scene.enter('second')
return ctx.answerCbQuery()
})
bot.launch()
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment