feat(sample): update sample

This commit is contained in:
Morb0 2021-01-06 22:48:10 +03:00
parent daf0d8ffdf
commit 7f7f786373
8 changed files with 51 additions and 32 deletions

View File

@ -1,7 +1,7 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { TelegrafExecutionContext } from 'nestjs-telegraf'; import { TelegrafExecutionContext } from 'nestjs-telegraf';
export const From = createParamDecorator( export const UpdateType = createParamDecorator(
(_, ctx: ExecutionContext) => (_, ctx: ExecutionContext) =>
TelegrafExecutionContext.create(ctx).getContext().from, TelegrafExecutionContext.create(ctx).getContext().updateType,
); );

View File

@ -1,11 +1,13 @@
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { TelegrafArgumentsHost } from 'nestjs-telegraf'; import { TelegrafArgumentsHost } from 'nestjs-telegraf';
import { Context } from '../../interfaces/context.interface';
@Catch() @Catch()
export class TelegrafExceptionFilter<T> implements ExceptionFilter { export class TelegrafExceptionFilter implements ExceptionFilter {
catch(exception: T, host: ArgumentsHost) { async catch(exception: Error, host: ArgumentsHost): Promise<void> {
const tgHost = TelegrafArgumentsHost.create(host); const telegrafHost = TelegrafArgumentsHost.create(host);
console.log(tgHost); const ctx = telegrafHost.getContext<Context>();
return exception;
await ctx.replyWithHTML(`<b>Error</b>: ${exception.message}`);
} }
} }

View File

@ -12,7 +12,7 @@ export class AdminGuard implements CanActivate {
const isAdmin = this.ADMIN_IDS.includes(from.id); const isAdmin = this.ADMIN_IDS.includes(from.id);
if (!isAdmin) { if (!isAdmin) {
throw new TelegrafException('You are not admin >:('); throw new TelegrafException('You are not admin 😡');
} }
return true; return true;

View File

@ -1,9 +1,9 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { EchoUpdate } from './echo.update'; import { EchoUpdate } from './echo.update';
import { EchoService } from './echo.service'; import { EchoService } from './echo.service';
import { HelloScene } from '../greeter/scenes/hello.scene'; import { RandomNumberScene } from '../greeter/scenes/random-number.scene';
@Module({ @Module({
providers: [EchoUpdate, EchoService, HelloScene], providers: [EchoUpdate, EchoService, RandomNumberScene],
}) })
export class EchoModule {} export class EchoModule {}

View File

@ -1,20 +1,25 @@
import { Telegraf } from 'telegraf'; import { UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
import { import {
Ctx,
MessageText,
Help, Help,
InjectBot, InjectBot,
On, On,
Message,
Start, Start,
Update, Update,
Hears, Command,
} from 'nestjs-telegraf'; } from 'nestjs-telegraf';
import { Telegraf } from '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'; 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() @Update()
@UseInterceptors(ResponseTimeInterceptor)
@UseFilters(TelegrafExceptionFilter)
export class EchoUpdate { export class EchoUpdate {
constructor( constructor(
@InjectBot(GreeterBotName) @InjectBot(GreeterBotName)
@ -33,8 +38,16 @@ export class EchoUpdate {
return 'Send me any text'; return 'Send me any text';
} }
@Command('admin')
@UseGuards(AdminGuard)
onAdminCommand(): string {
return 'Welcome judge';
}
@On('text') @On('text')
onMessage(@MessageText(new ReverseTextPipe()) messageText: string): string { onMessage(
return this.echoService.echo(messageText); @Message('text', new ReverseTextPipe()) reversedText: string,
): string {
return this.echoService.echo(reversedText);
} }
} }

View File

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

View File

@ -1,18 +1,21 @@
import { Command, Context as Ctx, Hears, Start, Update } from 'nestjs-telegraf'; import { Command, Ctx, Hears, Start, Update, Sender } from 'nestjs-telegraf';
import { User } from 'telegraf/typings/telegram-types'; import { UpdateType as TelegrafUpdateType } 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'; import { UpdateType } from '../common/decorators/update-type.decorator';
@Update() @Update()
export class GreeterUpdate { export class GreeterUpdate {
@Start() @Start()
async onStart(@Ctx() ctx: Context): Promise<void> { onStart(): string {
await ctx.reply('Say hello to me'); return 'Say hello to me';
} }
@Hears(['hi', 'hello', 'hey', 'qq']) @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}`; return `Hey ${firstName}`;
} }

View File

@ -3,23 +3,23 @@ import { HELLO_SCENE_ID } from '../../app.constants';
import { Context } from '../../interfaces/context.interface'; import { Context } from '../../interfaces/context.interface';
@Scene(HELLO_SCENE_ID) @Scene(HELLO_SCENE_ID)
export class HelloScene { export class RandomNumberScene {
@SceneEnter() @SceneEnter()
async onSceneEnter(ctx: Context): Promise<void> { onSceneEnter(): string {
console.log('Enter to scene'); console.log('Enter to scene');
await ctx.reply('Welcome on scene ✋'); return 'Welcome on scene ✋';
} }
@SceneLeave() @SceneLeave()
async onSceneLeave(ctx: Context): Promise<void> { onSceneLeave(): string {
console.log('Leave from scene'); console.log('Leave from scene');
await ctx.reply('Bye Bye 👋'); return 'Bye Bye 👋';
} }
@Command('hello') @Command(['rng', 'random'])
async onHelloCommand(ctx: Context): Promise<void> { onRandomCommand(): number {
console.log('Use say hello'); console.log('Use "random" command');
await ctx.reply('Hi'); return Math.floor(Math.random() * 11);
} }
@Command('leave') @Command('leave')