telegraf / telegraf-flow

[Deprecated] Control flow middleware for Telegraf
MIT License
38 stars 8 forks source link

Build Status NPM Version js-standard-style

Telegraf flow

🚥 Control flow middleware for Telegraf.

Deprecated: telegraf@3.15.3 now supports scene-based control flow (aka Stage) from the box.

Installation

$ npm install telegraf-flow

Example

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()

More examples

Telegraf context

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 
});