robot-mafia / nestjs-telegraf

🤖 Powerful Nest module for easy and fast creation Telegram bots
https://nestjs-telegraf.0x467.com
MIT License
520 stars 89 forks source link

Scenes usage #926

Closed ddtch closed 2 months ago

ddtch commented 2 years ago

Hello, my issue is more about the question of how to work with scenes? This part is really not described well in docs.

I was trying to create scene like this

@Update()
@Injectable()
@Scene('mainScene')
export class TbotService {
  constructor(
    @InjectBot() private bot: Telegraf<any>,
  ) {
  }

  @Start()
  async startCommand(ctx: SceneContext) {
    console.log(this.bot)
    await ctx.reply('Welcome ss');
  }
}
// other scene
@Update()
@Injectable()
@Scene('authScene')
export class TbotService {
  constructor(
    @InjectBot() private bot: Telegraf<any>,
  ) {
  }

  @SceneEnter()
  async sayHi(ctx: SceneContext) {
    await ctx.reply('Welcome ss');
  }
}

But after I add SceneEnter decorator, I see an error.

/Users/backend/node_modules/nestjs-telegraf/dist/services/listeners-explorer.service.js:133
            composer[method](...args, async (ctx, next) => {
                            ^
TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function
    at ListenersExplorerService.registerIfListener (/Users/backend/node_modules/nestjs-telegraf/dist/services/listeners-explorer.service.js:133:29)
    at MapIterator.iteratee (/Users/backend/node_modules/nestjs-telegraf/dist/services/listeners-explorer.service.js:92:82)
    at MapIterator.next (/Users/backend/node_modules/iterare/src/map.ts:9:39)
    at FilterIterator.next (/Users/backend/node_modules/iterare/src/filter.ts:11:34)
    at IteratorWithOperators.next (/Users/backend/node_modules/iterare/src/iterate.ts:19:28)
    at Function.from (<anonymous>)
    at IteratorWithOperators.toArray (/Users/backend/node_modules/iterare/src/iterate.ts:227:22)
    at MetadataScanner.scanFromPrototype (/Users/backend/node_modules/@nestjs/core/metadata-scanner.js:12:14)
    at ListenersExplorerService.registerListeners (/Users/backend/node_modules/nestjs-telegraf/dist/services/listeners-explorer.service.js:92:30)
    at /Users/backend/node_modules/nestjs-telegraf/dist/services/listeners-explorer.service.js:53:41

Any Ideas, advices ?

DBTK1990 commented 1 year ago

you need to add the session middleware and update telegraf to the latest version if you won't update you will get depracted notice

import { Module } from '@nestjs/common';
import { TelegrafModule } from 'nestjs-telegraf';
import { TestBotModule } from './test-bot/test-bot.module';
import { session } from 'telegraf';

@Module({
  imports: [
    TelegrafModule.forRoot({
      token: 'TALGRAM_TOKEN',
      middlewares: [session()],
    }),
    TestBotModule
  ],
})
export class AppModule { }