daily pastebin goal
23%
SHARE
TWEET

Mon

a guest Mar 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Telegraf  = require('telegraf'),
  2.       Stage     = require('telegraf/stage'),
  3.       WizardScene = require('telegraf/scenes/wizard')
  4.  
  5. const mongoose = require("mongoose")
  6. mongoose.connect(`mongodb://127.0.0.1:27017/test`, {
  7.     useNewUrlParser: true,
  8.     useFindAndModify: false
  9. })
  10.  
  11. const Schema = mongoose.Schema
  12. const sessionScheme = new Schema({
  13.     key: String,
  14.     session: Object
  15. })
  16. Session = mongoose.model("Session", sessionScheme, 'sessions')
  17.  
  18. session = function () {
  19.     return async (ctx, next) => {
  20.         const key = ctx.from && ctx.chat && `${ctx.from.id}:${ctx.chat.id}`
  21.         if (!key) {
  22.             return next(ctx)
  23.         }
  24.         let session = await Session.findOne({ key: key })
  25.         session = session ? session.session : {}
  26.         Object.defineProperty(ctx, 'session', {
  27.             get: function () { return session },
  28.             set: function (newValue) { session = Object.assign({}, newValue) }
  29.         })
  30.         return next(ctx).then(async () => {
  31.             await Session.updateOne({key: key}, { $set: { session: session } }, { upsert: true })
  32.         })
  33.     }
  34. }
  35.  
  36. const select = new WizardScene('select',
  37.     async (ctx) => {
  38.         console.log('select', ctx.session)
  39.         await ctx.reply('Переход в сцену select. Введите что-то')
  40.         return ctx.wizard.next()
  41.     },
  42.     (ctx) => {
  43.         ctx.reply('Next')
  44.     }                  
  45. )
  46.  
  47. const stage = new Stage([select])
  48.      
  49. const bot = new Telegraf('812106160:AAF1bHTrQQde4hL_rBToXoRiZwYeK7VkyUo') //@hyperloop_bot
  50.  
  51. bot.use(session())
  52. bot.use(stage.middleware())
  53.  
  54. bot.start(async (ctx) => {
  55.     console.log('start', ctx.session)
  56.     ctx.scene.enter('select')
  57. })
  58. bot.launch()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top