nestjs-telegraf/sample/01-complete-app/src/echo/echo.update.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-01-06 22:48:10 +03:00
import { UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
2021-01-06 19:39:41 +03:00
import {
Help,
InjectBot,
On,
2021-01-06 22:48:10 +03:00
Message,
2021-01-06 19:39:41 +03:00
Start,
Update,
2021-01-06 22:48:10 +03:00
Command,
2021-01-06 19:39:41 +03:00
} from 'nestjs-telegraf';
2021-01-06 22:48:10 +03:00
import { Telegraf } from 'telegraf';
2020-12-26 16:48:04 +03:00
import { EchoService } from './echo.service';
import { GreeterBotName } from '../app.constants';
2021-01-03 14:26:32 +03:00
import { Context } from '../interfaces/context.interface';
2021-01-06 19:39:41 +03:00
import { ReverseTextPipe } from '../common/pipes/reverse-text.pipe';
2021-01-06 22:48:10 +03:00
import { ResponseTimeInterceptor } from '../common/interceptors/response-time.interceptor';
import { AdminGuard } from '../common/guards/admin.guard';
import { TelegrafExceptionFilter } from '../common/filters/telegraf-exception.filter';
2020-12-26 16:23:54 +03:00
@Update()
2021-01-06 22:48:10 +03:00
@UseInterceptors(ResponseTimeInterceptor)
@UseFilters(TelegrafExceptionFilter)
2021-01-03 14:26:32 +03:00
export class EchoUpdate {
2020-12-26 16:48:04 +03:00
constructor(
2021-01-05 13:37:00 +03:00
@InjectBot(GreeterBotName)
2021-01-03 14:26:32 +03:00
private readonly bot: Telegraf<Context>,
2020-12-26 16:48:04 +03:00
private readonly echoService: EchoService,
) {}
@Start()
2021-01-06 19:39:41 +03:00
async onStart(): Promise<string> {
2021-01-03 14:26:32 +03:00
const me = await this.bot.telegram.getMe();
2021-01-06 19:39:41 +03:00
return `Hey, I'm ${me.first_name}`;
2020-12-26 16:48:04 +03:00
}
@Help()
2021-01-06 19:39:41 +03:00
async onHelp(): Promise<string> {
return 'Send me any text';
2020-12-26 16:48:04 +03:00
}
2021-01-06 22:48:10 +03:00
@Command('admin')
@UseGuards(AdminGuard)
onAdminCommand(): string {
return 'Welcome judge';
}
2021-01-06 19:39:41 +03:00
@On('text')
2021-01-06 22:48:10 +03:00
onMessage(
@Message('text', new ReverseTextPipe()) reversedText: string,
): string {
return this.echoService.echo(reversedText);
2020-12-26 16:23:54 +03:00
}
}