Merge pull request #1077 from damir-minkashev/add_wizard_sample

feat: add wizard sample
This commit is contained in:
Aleksandr 2023-07-24 16:52:13 +03:00 committed by GitHub
commit 70492f96a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 2 deletions

View File

@ -1,3 +1,5 @@
export const HELLO_SCENE_ID = 'HELLO_SCENE_ID';
export const WIZARD_SCENE_ID = 'WIZARD_SCENE_ID';
export const GreeterBotName = 'greeter';

View File

@ -1,8 +1,9 @@
import { Module } from '@nestjs/common';
import { GreeterUpdate } from './greeter.update';
import { RandomNumberScene } from './scenes/random-number.scene';
import { GreeterWizard } from './wizard/greeter.wizard';
@Module({
providers: [GreeterUpdate, RandomNumberScene],
providers: [GreeterUpdate, RandomNumberScene, GreeterWizard],
})
export class GreeterModule {}

View File

@ -1,7 +1,7 @@
import { Command, Ctx, Hears, Start, Update, Sender } from 'nestjs-telegraf';
import { UpdateType as TelegrafUpdateType } from 'telegraf/typings/telegram-types';
import { Context } from '../interfaces/context.interface';
import { HELLO_SCENE_ID } from '../app.constants';
import { HELLO_SCENE_ID, WIZARD_SCENE_ID } from '../app.constants';
import { UpdateType } from '../common/decorators/update-type.decorator';
@Update()
@ -23,4 +23,9 @@ export class GreeterUpdate {
async onSceneCommand(@Ctx() ctx: Context): Promise<void> {
await ctx.scene.enter(HELLO_SCENE_ID);
}
@Command('wizard')
async onWizardCommand(@Ctx() ctx: Context): Promise<void> {
await ctx.scene.enter(WIZARD_SCENE_ID);
}
}

View File

@ -0,0 +1,36 @@
import { Ctx, Message, On, Wizard, WizardStep } from 'nestjs-telegraf';
import { WIZARD_SCENE_ID } from '../../app.constants';
import { WizardContext } from 'telegraf/typings/scenes';
@Wizard(WIZARD_SCENE_ID)
export class GreeterWizard {
@WizardStep(1)
async onSceneEnter(@Ctx() ctx: WizardContext): Promise<string> {
console.log('Enter to scene');
await ctx.wizard.next();
return 'Welcome to wizard scene ✋ Send me your name';
}
@On('text')
@WizardStep(2)
async onName(
@Ctx() ctx: WizardContext,
@Message() msg: { text: string },
): Promise<string> {
console.log('Enter to step 1');
ctx.wizard.state['name'] = msg.text;
await ctx.wizard.next();
return 'Send me where are you from';
}
@On('text')
@WizardStep(3)
async onLocation(
@Ctx() ctx: WizardContext & { wizard: { state: { name: string } } },
@Message() msg: { text: string },
): Promise<string> {
console.log('Enter to step 3');
await ctx.scene.leave();
return `Hello ${ctx.wizard.state.name} from ${msg.text}. I'm Greater bot from 127.0.0.1 👋`;
}
}