-
-
Save nullcookies/cce050f088b4fdece56b3bb2fdee094f to your computer and use it in GitHub Desktop.
Telegraf bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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