From 7f7f78637311fa49d7599a569d28ce4a921c279b Mon Sep 17 00:00:00 2001 From: Morb0 Date: Wed, 6 Jan 2021 22:48:10 +0300 Subject: [PATCH] feat(sample): update sample --- ....decorator.ts => update-type.decorator.ts} | 4 +-- .../filters/telegraf-exception.filter.ts | 12 +++++---- .../src/common/guards/admin.guard.ts | 2 +- .../01-complete-app/src/echo/echo.module.ts | 4 +-- .../01-complete-app/src/echo/echo.update.ts | 25 ++++++++++++++----- .../src/greeter/greeter.module.ts | 3 ++- .../src/greeter/greeter.update.ts | 15 ++++++----- ...{hello.scene.ts => random-number.scene.ts} | 18 ++++++------- 8 files changed, 51 insertions(+), 32 deletions(-) rename sample/01-complete-app/src/common/decorators/{from.decorator.ts => update-type.decorator.ts} (59%) rename sample/01-complete-app/src/greeter/scenes/{hello.scene.ts => random-number.scene.ts} (56%) diff --git a/sample/01-complete-app/src/common/decorators/from.decorator.ts b/sample/01-complete-app/src/common/decorators/update-type.decorator.ts similarity index 59% rename from sample/01-complete-app/src/common/decorators/from.decorator.ts rename to sample/01-complete-app/src/common/decorators/update-type.decorator.ts index b02273c..2639e6d 100644 --- a/sample/01-complete-app/src/common/decorators/from.decorator.ts +++ b/sample/01-complete-app/src/common/decorators/update-type.decorator.ts @@ -1,7 +1,7 @@ import { createParamDecorator, ExecutionContext } from '@nestjs/common'; import { TelegrafExecutionContext } from 'nestjs-telegraf'; -export const From = createParamDecorator( +export const UpdateType = createParamDecorator( (_, ctx: ExecutionContext) => - TelegrafExecutionContext.create(ctx).getContext().from, + TelegrafExecutionContext.create(ctx).getContext().updateType, ); diff --git a/sample/01-complete-app/src/common/filters/telegraf-exception.filter.ts b/sample/01-complete-app/src/common/filters/telegraf-exception.filter.ts index a1a4099..8221b86 100644 --- a/sample/01-complete-app/src/common/filters/telegraf-exception.filter.ts +++ b/sample/01-complete-app/src/common/filters/telegraf-exception.filter.ts @@ -1,11 +1,13 @@ import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; import { TelegrafArgumentsHost } from 'nestjs-telegraf'; +import { Context } from '../../interfaces/context.interface'; @Catch() -export class TelegrafExceptionFilter implements ExceptionFilter { - catch(exception: T, host: ArgumentsHost) { - const tgHost = TelegrafArgumentsHost.create(host); - console.log(tgHost); - return exception; +export class TelegrafExceptionFilter implements ExceptionFilter { + async catch(exception: Error, host: ArgumentsHost): Promise { + const telegrafHost = TelegrafArgumentsHost.create(host); + const ctx = telegrafHost.getContext(); + + await ctx.replyWithHTML(`Error: ${exception.message}`); } } diff --git a/sample/01-complete-app/src/common/guards/admin.guard.ts b/sample/01-complete-app/src/common/guards/admin.guard.ts index 130dd4d..23030b0 100644 --- a/sample/01-complete-app/src/common/guards/admin.guard.ts +++ b/sample/01-complete-app/src/common/guards/admin.guard.ts @@ -12,7 +12,7 @@ export class AdminGuard implements CanActivate { const isAdmin = this.ADMIN_IDS.includes(from.id); if (!isAdmin) { - throw new TelegrafException('You are not admin >:('); + throw new TelegrafException('You are not admin 😡'); } return true; diff --git a/sample/01-complete-app/src/echo/echo.module.ts b/sample/01-complete-app/src/echo/echo.module.ts index 9c583a3..36c97ce 100644 --- a/sample/01-complete-app/src/echo/echo.module.ts +++ b/sample/01-complete-app/src/echo/echo.module.ts @@ -1,9 +1,9 @@ import { Module } from '@nestjs/common'; import { EchoUpdate } from './echo.update'; import { EchoService } from './echo.service'; -import { HelloScene } from '../greeter/scenes/hello.scene'; +import { RandomNumberScene } from '../greeter/scenes/random-number.scene'; @Module({ - providers: [EchoUpdate, EchoService, HelloScene], + providers: [EchoUpdate, EchoService, RandomNumberScene], }) export class EchoModule {} diff --git a/sample/01-complete-app/src/echo/echo.update.ts b/sample/01-complete-app/src/echo/echo.update.ts index 92135e1..eee15f0 100644 --- a/sample/01-complete-app/src/echo/echo.update.ts +++ b/sample/01-complete-app/src/echo/echo.update.ts @@ -1,20 +1,25 @@ -import { Telegraf } from 'telegraf'; +import { UseFilters, UseGuards, UseInterceptors } from '@nestjs/common'; import { - Ctx, - MessageText, Help, InjectBot, On, + Message, Start, Update, - Hears, + Command, } from 'nestjs-telegraf'; +import { Telegraf } from 'telegraf'; import { EchoService } from './echo.service'; import { GreeterBotName } from '../app.constants'; import { Context } from '../interfaces/context.interface'; import { ReverseTextPipe } from '../common/pipes/reverse-text.pipe'; +import { ResponseTimeInterceptor } from '../common/interceptors/response-time.interceptor'; +import { AdminGuard } from '../common/guards/admin.guard'; +import { TelegrafExceptionFilter } from '../common/filters/telegraf-exception.filter'; @Update() +@UseInterceptors(ResponseTimeInterceptor) +@UseFilters(TelegrafExceptionFilter) export class EchoUpdate { constructor( @InjectBot(GreeterBotName) @@ -33,8 +38,16 @@ export class EchoUpdate { return 'Send me any text'; } + @Command('admin') + @UseGuards(AdminGuard) + onAdminCommand(): string { + return 'Welcome judge'; + } + @On('text') - onMessage(@MessageText(new ReverseTextPipe()) messageText: string): string { - return this.echoService.echo(messageText); + onMessage( + @Message('text', new ReverseTextPipe()) reversedText: string, + ): string { + return this.echoService.echo(reversedText); } } diff --git a/sample/01-complete-app/src/greeter/greeter.module.ts b/sample/01-complete-app/src/greeter/greeter.module.ts index 40160db..02e3284 100644 --- a/sample/01-complete-app/src/greeter/greeter.module.ts +++ b/sample/01-complete-app/src/greeter/greeter.module.ts @@ -1,7 +1,8 @@ import { Module } from '@nestjs/common'; import { GreeterUpdate } from './greeter.update'; +import { RandomNumberScene } from './scenes/random-number.scene'; @Module({ - providers: [GreeterUpdate], + providers: [GreeterUpdate, RandomNumberScene], }) 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 dfc9c47..bef392f 100644 --- a/sample/01-complete-app/src/greeter/greeter.update.ts +++ b/sample/01-complete-app/src/greeter/greeter.update.ts @@ -1,18 +1,21 @@ -import { Command, Context as Ctx, Hears, Start, Update } from 'nestjs-telegraf'; -import { User } from 'telegraf/typings/telegram-types'; +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 { From } from '../common/decorators/from.decorator'; +import { UpdateType } from '../common/decorators/update-type.decorator'; @Update() export class GreeterUpdate { @Start() - async onStart(@Ctx() ctx: Context): Promise { - await ctx.reply('Say hello to me'); + onStart(): string { + return 'Say hello to me'; } @Hears(['hi', 'hello', 'hey', 'qq']) - onGreetings(@From() { first_name: firstName }: User): string { + onGreetings( + @UpdateType() updateType: TelegrafUpdateType, + @Sender('first_name') firstName: string, + ): string { return `Hey ${firstName}`; } diff --git a/sample/01-complete-app/src/greeter/scenes/hello.scene.ts b/sample/01-complete-app/src/greeter/scenes/random-number.scene.ts similarity index 56% rename from sample/01-complete-app/src/greeter/scenes/hello.scene.ts rename to sample/01-complete-app/src/greeter/scenes/random-number.scene.ts index 211b06f..3c04d46 100644 --- a/sample/01-complete-app/src/greeter/scenes/hello.scene.ts +++ b/sample/01-complete-app/src/greeter/scenes/random-number.scene.ts @@ -3,23 +3,23 @@ import { HELLO_SCENE_ID } from '../../app.constants'; import { Context } from '../../interfaces/context.interface'; @Scene(HELLO_SCENE_ID) -export class HelloScene { +export class RandomNumberScene { @SceneEnter() - async onSceneEnter(ctx: Context): Promise { + onSceneEnter(): string { console.log('Enter to scene'); - await ctx.reply('Welcome on scene ✋'); + return 'Welcome on scene ✋'; } @SceneLeave() - async onSceneLeave(ctx: Context): Promise { + onSceneLeave(): string { console.log('Leave from scene'); - await ctx.reply('Bye Bye 👋'); + return 'Bye Bye 👋'; } - @Command('hello') - async onHelloCommand(ctx: Context): Promise { - console.log('Use say hello'); - await ctx.reply('Hi'); + @Command(['rng', 'random']) + onRandomCommand(): number { + console.log('Use "random" command'); + return Math.floor(Math.random() * 11); } @Command('leave')