Skip to content

Nord001/telegraf-flow

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status NPM Version js-standard-style

Telegraf flow

🚥 Control flow middleware for Telegraf

Installation

$ npm install telegraf-flow

Example

const Telegraf = require('telegraf')
const TelegrafFlow = require('telegraf-flow')
const { Scene } = TelegrafFlow

const flow = new TelegrafFlow()

// Global commands
flow.command('greeter', (ctx) => ctx.flow.enter('greeter'))
flow.command('echo', (ctx) => ctx.flow.enter('echo'))

// Greeter scene
const greeterScene = new Scene('greeter')
greeterScene.enter((ctx) => ctx.reply('Hi'))
greeterScene.on('text', (ctx) => {
  if (ctx.message.text.toLowerCase() === 'hi') {
    ctx.flow.leave()
    return ctx.reply('Buy')
  }
  return ctx.reply('Hi again')
})

// Echo scene
const echoScene = new Scene('echo')
echoScene.enter((ctx) => ctx.reply('echo scene'))
echoScene.command('back', (ctx) => {
  ctx.flow.leave()
  return ctx.reply('Okay')
})
echoScene.on('text', (ctx) => ctx.reply(ctx.message.text))
echoScene.on('message', (ctx) => ctx.reply('Only text messages please'))

// Scene registration
flow.register(greeterScene)
flow.register(echoScene)

const app = new Telegraf(process.env.BOT_TOKEN)
app.use(Telegraf.memorySession())
app.use(flow.middleware())
app.startPolling()

Other 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.leave()                                  // Leave scene 
});

About

🚥 Control flow middleware for Telegraf

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%