From 75e168a79ca0f1ded27f5488bfa61d97a77a391e Mon Sep 17 00:00:00 2001 From: damirminkashev <damirolololy@yandex.ru> Date: Fri, 10 Mar 2023 01:12:59 +0400 Subject: [PATCH] feat: add wizard sample --- sample/01-complete-app/src/app.constants.ts | 2 ++ .../src/greeter/greeter.module.ts | 3 +- .../src/greeter/greeter.update.ts | 7 +++- .../src/greeter/wizard/greeter.wizard.ts | 36 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 sample/01-complete-app/src/greeter/wizard/greeter.wizard.ts diff --git a/sample/01-complete-app/src/app.constants.ts b/sample/01-complete-app/src/app.constants.ts index 771789e..8118443 100644 --- a/sample/01-complete-app/src/app.constants.ts +++ b/sample/01-complete-app/src/app.constants.ts @@ -1,3 +1,5 @@ export const HELLO_SCENE_ID = 'HELLO_SCENE_ID'; +export const WIZARD_SCENE_ID = 'WIZARD_SCENE_ID'; + export const GreeterBotName = 'greeter'; diff --git a/sample/01-complete-app/src/greeter/greeter.module.ts b/sample/01-complete-app/src/greeter/greeter.module.ts index 02e3284..6a38de6 100644 --- a/sample/01-complete-app/src/greeter/greeter.module.ts +++ b/sample/01-complete-app/src/greeter/greeter.module.ts @@ -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 {} diff --git a/sample/01-complete-app/src/greeter/greeter.update.ts b/sample/01-complete-app/src/greeter/greeter.update.ts index bef392f..7f41cf7 100644 --- a/sample/01-complete-app/src/greeter/greeter.update.ts +++ b/sample/01-complete-app/src/greeter/greeter.update.ts @@ -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); + } } diff --git a/sample/01-complete-app/src/greeter/wizard/greeter.wizard.ts b/sample/01-complete-app/src/greeter/wizard/greeter.wizard.ts new file mode 100644 index 0000000..a203edc --- /dev/null +++ b/sample/01-complete-app/src/greeter/wizard/greeter.wizard.ts @@ -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 👋`; + } +}