Skip to content

Instantly share code, notes, and snippets.

@murka
Last active July 29, 2021 11:53
Show Gist options
  • Save murka/416d48334aff61028d3cd260e2645a33 to your computer and use it in GitHub Desktop.
Save murka/416d48334aff61028d3cd260e2645a33 to your computer and use it in GitHub Desktop.
const { Telegraf } = require('telegraf')
const RedisSession = require('telegraf-session-redis')
const WizardScene = require('telegraf/scenes/wizard')
const Stage = require('telegraf/stage')
const bot = new Telegraf(process.env.TOKEN)
const session = new RedisSession({
store: { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT }
})
const getSessionKey = session.options.getSessionKey
const sceneKey = 'greeter'
const greeterScene = new WizardScene(sceneKey,
async (ctx) => {
await ctx.reply('Test1')
return ctx.wizard.next()
},
async (ctx) => {
await ctx.reply('Test2')
return ctx.scene.leave()
}
)
const stages = new Stage([greeterScene])
stages.register()
bot.use(session)
bot.use(stages.middleware())
let store = []
bot.start( async (ctx) => {
store.push({ id: ctx.from.id, lastActiveDate: new Date().getTime() })
await ctx.reply('Yo!')
})
setTimeout(() => {
store
.filter(({ lastActiveDate }) => lastActiveDate + 10 * 1000 <= new Date().getTime())
.map( async ({ id }) => {
await bot.telegram.sendMessage(id ,'Прошло N времени, почему не пользуетесь ботом?')
const sessionKey = getSessionKey({ from: { id }, chat: { id }})
session.clearSession(sessionKey)
session.saveSession(sessionKey, { __scenes: { current: sceneKey, state: {} } })
})
}, 15000)
bot.launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment