mirror of
				https://github.com/Maks1mS/nestjs-telegraf.git
				synced 2025-11-04 07:51:22 +03:00 
			
		
		
		
	feat(sample): update sample
This commit is contained in:
		@@ -1,8 +1,18 @@
 | 
				
			|||||||
import { Telegraf } from 'telegraf';
 | 
					import { Telegraf } from 'telegraf';
 | 
				
			||||||
import { Help, InjectBot, On, Start, Update } from 'nestjs-telegraf';
 | 
					import {
 | 
				
			||||||
 | 
					  Ctx,
 | 
				
			||||||
 | 
					  MessageText,
 | 
				
			||||||
 | 
					  Help,
 | 
				
			||||||
 | 
					  InjectBot,
 | 
				
			||||||
 | 
					  On,
 | 
				
			||||||
 | 
					  Start,
 | 
				
			||||||
 | 
					  Update,
 | 
				
			||||||
 | 
					  Hears,
 | 
				
			||||||
 | 
					} from 'nestjs-telegraf';
 | 
				
			||||||
import { EchoService } from './echo.service';
 | 
					import { EchoService } from './echo.service';
 | 
				
			||||||
import { GreeterBotName } from '../app.constants';
 | 
					import { GreeterBotName } from '../app.constants';
 | 
				
			||||||
import { Context } from '../interfaces/context.interface';
 | 
					import { Context } from '../interfaces/context.interface';
 | 
				
			||||||
 | 
					import { ReverseTextPipe } from '../common/pipes/reverse-text.pipe';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Update()
 | 
					@Update()
 | 
				
			||||||
export class EchoUpdate {
 | 
					export class EchoUpdate {
 | 
				
			||||||
@@ -13,26 +23,18 @@ export class EchoUpdate {
 | 
				
			|||||||
  ) {}
 | 
					  ) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Start()
 | 
					  @Start()
 | 
				
			||||||
  async onStart(ctx: Context): Promise<void> {
 | 
					  async onStart(): Promise<string> {
 | 
				
			||||||
    const me = await this.bot.telegram.getMe();
 | 
					    const me = await this.bot.telegram.getMe();
 | 
				
			||||||
    await ctx.reply(`Hey, I'm ${me.first_name}`);
 | 
					    return `Hey, I'm ${me.first_name}`;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Help()
 | 
					  @Help()
 | 
				
			||||||
  async onHelp(ctx: Context): Promise<void> {
 | 
					  async onHelp(): Promise<string> {
 | 
				
			||||||
    await ctx.reply('Send me any text');
 | 
					    return 'Send me any text';
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @On('message')
 | 
					  @On('text')
 | 
				
			||||||
  async onMessage(ctx: Context): Promise<void> {
 | 
					  onMessage(@MessageText(new ReverseTextPipe()) messageText: string): string {
 | 
				
			||||||
    console.log('New message received');
 | 
					    return this.echoService.echo(messageText);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if ('text' in ctx.message) {
 | 
					 | 
				
			||||||
      const messageText = ctx.message.text;
 | 
					 | 
				
			||||||
      const echoText = this.echoService.echo(messageText);
 | 
					 | 
				
			||||||
      await ctx.reply(echoText);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      await ctx.reply('Only text messages');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,22 +1,23 @@
 | 
				
			|||||||
import { Command, Hears, Start, Update } from 'nestjs-telegraf';
 | 
					import { Command, Context as Ctx, Hears, Start, Update } from 'nestjs-telegraf';
 | 
				
			||||||
 | 
					import { User } from 'telegraf/typings/telegram-types';
 | 
				
			||||||
import { Context } from '../interfaces/context.interface';
 | 
					import { Context } from '../interfaces/context.interface';
 | 
				
			||||||
import { HELLO_SCENE_ID } from '../app.constants';
 | 
					import { HELLO_SCENE_ID } from '../app.constants';
 | 
				
			||||||
 | 
					import { From } from '../common/decorators/from.decorator';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Update()
 | 
					@Update()
 | 
				
			||||||
export class GreeterUpdate {
 | 
					export class GreeterUpdate {
 | 
				
			||||||
  @Start()
 | 
					  @Start()
 | 
				
			||||||
  async onStart(ctx: Context): Promise<void> {
 | 
					  async onStart(@Ctx() ctx: Context): Promise<void> {
 | 
				
			||||||
    await ctx.reply('Say hello to me');
 | 
					    await ctx.reply('Say hello to me');
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Hears(['hi', 'hello', 'hey', 'qq'])
 | 
					  @Hears(['hi', 'hello', 'hey', 'qq'])
 | 
				
			||||||
  async onGreetings(ctx: Context): Promise<void> {
 | 
					  onGreetings(@From() { first_name: firstName }: User): string {
 | 
				
			||||||
    const { first_name } = ctx.from;
 | 
					    return `Hey ${firstName}`;
 | 
				
			||||||
    await ctx.reply(`Hey ${first_name}`);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Command('scene')
 | 
					  @Command('scene')
 | 
				
			||||||
  async onSceneCommand(ctx: Context): Promise<void> {
 | 
					  async onSceneCommand(@Ctx() ctx: Context): Promise<void> {
 | 
				
			||||||
    await ctx.scene.enter(HELLO_SCENE_ID);
 | 
					    await ctx.scene.enter(HELLO_SCENE_ID);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user