0x467 / nestjs-telegraf

šŸ¤– Powerful Nest module for easy and fast creation Telegram bots
https://nestjs-telegraf.0x467.com
MIT License
481 stars 84 forks source link

scene is undefined in middleware #1167

Closed Aliph0th closed 7 months ago

Aliph0th commented 8 months ago

I get an error Cannot read properties of undefined (reading 'enter'), although the middleware with the session is called earlier than commandMiddleware

How to fix it?

app.update.ts

@Module({
   imports: [
      TelegrafModule.forRootAsync({
         imports: [ConfigModule],
         inject: [ConfigService],
         useFactory(configService: ConfigService): TelegrafModuleOptions {
            const store = Mongo({
               url: configService.getOrThrow<string>('MONGO_URI'),
               database: configService.getOrThrow<string>('MONGO_DB_SESSION_NAME')
            });
            return {
               token: configService.getOrThrow<string>('BOT_TOKEN'),
               middlewares: [session({ store }), commandMiddleware] // <====================
            };
         }
      })
   ],
   providers: [AppUpdate, HelpScene, AdminScene, RequestScene]
})
export class AppModule {}

commandMiddleware.ts

export const commandMiddleware: Middleware<TelegrafSceneContext> = async (
   ctx,
   next
) => {
   const text = (ctx.message as Message.TextMessage)?.text;
   if (text && text.startsWith('/')) {
      switch (text.slice(1)) {
         case 'help':
            ctx.scene.enter('help_scene');
            break;
         case 'admin':
            ctx.scene.enter('admin_scene');
            break;
         case 'request':
            ctx.scene.enter('request_scene');
            break;
      }
   }
   next();
};