mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 23:14:39 +03:00
feat(sample): update sample
This commit is contained in:
parent
daf0d8ffdf
commit
7f7f786373
@ -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,
|
||||
);
|
@ -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<T> 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<void> {
|
||||
const telegrafHost = TelegrafArgumentsHost.create(host);
|
||||
const ctx = telegrafHost.getContext<Context>();
|
||||
|
||||
await ctx.replyWithHTML(`<b>Error</b>: ${exception.message}`);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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 {}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {}
|
||||
|
@ -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<void> {
|
||||
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}`;
|
||||
}
|
||||
|
||||
|
@ -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<void> {
|
||||
onSceneEnter(): string {
|
||||
console.log('Enter to scene');
|
||||
await ctx.reply('Welcome on scene ✋');
|
||||
return 'Welcome on scene ✋';
|
||||
}
|
||||
|
||||
@SceneLeave()
|
||||
async onSceneLeave(ctx: Context): Promise<void> {
|
||||
onSceneLeave(): string {
|
||||
console.log('Leave from scene');
|
||||
await ctx.reply('Bye Bye 👋');
|
||||
return 'Bye Bye 👋';
|
||||
}
|
||||
|
||||
@Command('hello')
|
||||
async onHelloCommand(ctx: Context): Promise<void> {
|
||||
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')
|
Loading…
Reference in New Issue
Block a user