nestjs-telegraf/sample/app.update.ts

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-01-03 01:30:57 +03:00
import { Telegraf } from 'telegraf';
import { SceneContextMessageUpdate } from 'telegraf/typings/stage';
import {
Command,
Ctx,
Help,
InjectBot,
MessageText,
On,
Start,
Update,
} from '../lib';
2020-12-26 16:48:04 +03:00
import { EchoService } from './echo.service';
import { HELLO_SCENE_ID } from './app.constants';
import { Context } from './interfaces/context.interface';
2021-01-03 01:30:57 +03:00
import { UseGuards, UseInterceptors } from '@nestjs/common';
import { AdminGuard } from './common/guards/admin.guard';
import { ResponseTimeInterceptor } from './common/interceptors/response-time.interceptor';
import { ReverseTextPipe } from './common/pipes/reverse-text.pipe';
2020-12-26 16:23:54 +03:00
@Update()
export class AppUpdate {
2020-12-26 16:48:04 +03:00
constructor(
@InjectBot()
2021-01-03 01:30:57 +03:00
private readonly bot: Telegraf<SceneContextMessageUpdate>,
2020-12-26 16:48:04 +03:00
private readonly echoService: EchoService,
) {}
@Start()
2021-01-03 01:30:57 +03:00
async onStart(): Promise<string> {
2020-12-26 16:48:04 +03:00
const me = await this.bot.telegram.getMe();
2021-01-03 01:30:57 +03:00
return `Hey, I'm ${me.first_name}`;
2020-12-26 16:48:04 +03:00
}
@Help()
2021-01-03 01:30:57 +03:00
@UseInterceptors(ResponseTimeInterceptor)
async onHelp(@Ctx() ctx: Context): Promise<void> {
2020-12-26 16:48:04 +03:00
await ctx.reply('Send me any text');
}
2021-01-03 01:30:57 +03:00
@UseGuards(AdminGuard)
@Command('admin')
async onAdminCommand(@Ctx() ctx: Context): Promise<void> {
await ctx.reply('Welcome judge');
}
@Command('scene')
2021-01-03 01:30:57 +03:00
async onSceneCommand(@Ctx() ctx: Context): Promise<void> {
await ctx.scene.enter(HELLO_SCENE_ID);
}
2020-12-26 16:23:54 +03:00
@On('message')
2021-01-03 01:30:57 +03:00
async onMessage(
@Ctx() ctx: Context,
@MessageText(new ReverseTextPipe()) reversedMessage: string,
): Promise<void> {
2020-12-26 16:23:54 +03:00
console.log('New message received');
2020-12-26 16:48:04 +03:00
if ('text' in ctx.message) {
2021-01-03 01:30:57 +03:00
const echoText = this.echoService.echo(reversedMessage);
2020-12-26 16:48:04 +03:00
await ctx.reply(echoText);
} else {
await ctx.reply('Only text messages');
}
2020-12-26 16:23:54 +03:00
}
}