Skip to content

Add an example for Stage and Scene with a custom context #1139

@negebauer

Description

@negebauer

Is your feature request related to a problem? Please describe.

I'm new to typescript and I've been trying to write a bot that has uses session with a custom session object and stages.
I haven't found any examples of Stages using a custom context type.

Describe the solution you'd like

A complete example with a bot with a custom type and stages using that custom type.

Additional context

This is the code I have

import { Telegraf, Context, Stage, BaseScene } from 'telegraf'
import LocalSession from 'telegraf-session-local'

interface Session {
  bar: string
}

interface BotContext extends Context {
  session: Session
}

const bot = new Telegraf<BotContext>('foo')
const session = new LocalSession<BotContext>()
bot.use(session.middleware())

const scene = new BaseScene('scene')
scene.command('back', ctx => ctx.scene.leave())

const stage = new Stage([scene], { ttl: 10 })
bot.use(stage.middleware())

bot.command('foo', ctx => ctx.scene.enter('scene'))

bot.launch()

bot.use(stage.middleware()) shows the following

Argument of type 'MiddlewareFn<SceneContextMessageUpdate>' is not assignable to parameter of type 'Middleware<BotContext>'.
  Type 'MiddlewareFn<SceneContextMessageUpdate>' is not assignable to type 'MiddlewareFn<BotContext>'.
    Property 'scene' is missing in type 'BotContext' but required in type 'SceneContextMessageUpdate'.

and bot.command('foo', ctx => ctx.scene.enter('scene')) shows

Property 'scene' does not exist on type 'BotContext'.

I though that using BaseScene<BotContext> and Stage<BotContext> would make it work, but I get the following instead

For BaseScene<BotContext>

Type 'BotContext' does not satisfy the constraint 'SceneContextMessageUpdate'.
  Property 'scene' is missing in type 'BotContext' but required in type 'SceneContextMessageUpdate'.

And for Stage<BotContext>

Type 'BotContext' does not satisfy the constraint 'SceneContextMessageUpdate'.

I did get to code that compiles by doing the following

import { Telegraf, Context, Stage, BaseScene } from 'telegraf'
import LocalSession from 'telegraf-session-local'
import { SceneContextMessageUpdate } from '../node_modules/telegraf/typings/stage.d'

interface Session {
  foo: string
}

type BaseBotContext = Context & SceneContextMessageUpdate

interface BotContext extends BaseBotContext {
  session: Session
}

const bot = new Telegraf<BotContext>('foo')
const session = new LocalSession<BotContext>()
bot.use(session.middleware())

const scene = new BaseScene<BotContext>('scene')
scene.command('back', ctx => ctx.scene.leave())

const stage = new Stage<BotContext>([scene], { ttl: 10 })
bot.use(stage.middleware())

bot.command('foo', ctx => ctx.scene.enter('scene'))

bot.launch()

Is the type BaseBotContext = Context & SceneContextMessageUpdate the way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions