Block Scene

Scenes are an environment isolated from other updates/handlers

// name.scene.ts
import { BlockScene, Keyboard } from 'degreet-telegram'
import { IContext } from 'degreet-telegram/src/types'

const scene: BlockScene = new BlockScene('enter_name')

scene.onEnter(async (ctx: IContext): Promise<any> => {
  try {
    await ctx.answer.send(
      'Enter your name',
      new Keyboard('under_the_message').btn('cb', 'Cancel', 'cancel').row()
    )
  } catch (e: any) {
    console.error(e)
  }
})

scene.onLeave(async (ctx: IContext): Promise<any> => {
  try {
    console.log('Leaved')
  } catch (e: any) {
    console.error(e)
  }
})

scene.onClick('cancel', async (ctx: IContext): Promise<any> => {
  try {
    return ctx.scene.leave()
  } catch (e: any) {
    console.error(e)
  }
})

scene.on('text', async (ctx: IContext): Promise<any> => {
  try {
    console.log(ctx.scene.params) // params that you can pass on enter
    await ctx.answer.send(ctx.msg.text)
    return ctx.scene.leave()
  } catch (e: any) {
    console.error(e)
  }
})

export default scene

// index.ts
bot.use(scene)

// call scene
ctx.scene.enter('enter_name', { param1: 1 })

Last updated