Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Telegraf = require('telegraf'),
- Stage = require('telegraf/stage'),
- WizardScene = require('telegraf/scenes/wizard')
- const mongoose = require("mongoose")
- mongoose.connect(`mongodb://127.0.0.1:27017/test`, {
- useNewUrlParser: true,
- useFindAndModify: false
- })
- const Schema = mongoose.Schema
- const sessionScheme = new Schema({
- key: String,
- session: Object
- })
- Session = mongoose.model("Session", sessionScheme, 'sessions')
- session = function () {
- return async (ctx, next) => {
- const key = ctx.from && ctx.chat && `${ctx.from.id}:${ctx.chat.id}`
- if (!key) {
- return next(ctx)
- }
- let session = await Session.findOne({ key: key })
- session = session ? session.session : {}
- Object.defineProperty(ctx, 'session', {
- get: function () { return session },
- set: function (newValue) { session = Object.assign({}, newValue) }
- })
- return next(ctx).then(async () => {
- await Session.updateOne({key: key}, { $set: { session: session } }, { upsert: true })
- })
- }
- }
- const select = new WizardScene('select',
- async (ctx) => {
- console.log('select', ctx.session)
- await ctx.reply('Переход в сцену select. Введите что-то')
- return ctx.wizard.next()
- },
- (ctx) => {
- ctx.reply('Next')
- }
- )
- const stage = new Stage([select])
- const bot = new Telegraf('812106160:AAF1bHTrQQde4hL_rBToXoRiZwYeK7VkyUo') //@hyperloop_bot
- bot.use(session())
- bot.use(stage.middleware())
- bot.start(async (ctx) => {
- console.log('start', ctx.session)
- ctx.scene.enter('select')
- })
- bot.launch()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement