🚥 Control flow middleware for Telegraf.
Deprecated: [email protected]
now supports scene-based control flow (aka Stage) from the box.
$ npm install telegraf-flow
const Telegraf = require('telegraf')
const TelegrafFlow = require('telegraf-flow')
const { Scene } = TelegrafFlow
// Greeter scene
const greeterScene = new Scene('greeter')
greeterScene.enter((ctx) => ctx.reply('Hi'))
greeterScene.leave((ctx) => ctx.reply('Buy'))
greeterScene.hears(/hi/gi, leave())
greeterScene.on('message', (ctx) => ctx.reply('Send `hi`'))
// Scene registration
const flow = new TelegrafFlow()
flow.register(greeterScene)
const app = new Telegraf(process.env.BOT_TOKEN)
// Flow requires valid Telegraf session
app.use(Telegraf.memorySession())
app.use(flow.middleware())
app.command('greeter', (ctx) => ctx.flow.enter('greeter'))
app.startPolling()
Telegraf user context props and functions:
app.on('...', (ctx) => {
ctx.flow.state // Current scene state
ctx.flow.enter(sceneId, [defaultState, silent]) // Enter scene
ctx.flow.reenter() // Reenter current scene
ctx.flow.leave() // Leave scene
});