mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-03-31 13:53:42 +03:00
Merge pull request #1077 from damir-minkashev/add_wizard_sample
feat: add wizard sample
This commit is contained in:
commit
70492f96a9
@ -1,3 +1,5 @@
|
||||
export const HELLO_SCENE_ID = 'HELLO_SCENE_ID';
|
||||
|
||||
export const WIZARD_SCENE_ID = 'WIZARD_SCENE_ID';
|
||||
|
||||
export const GreeterBotName = 'greeter';
|
||||
|
@ -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 {}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
36
sample/01-complete-app/src/greeter/wizard/greeter.wizard.ts
Normal file
36
sample/01-complete-app/src/greeter/wizard/greeter.wizard.ts
Normal 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 👋`;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user