mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-12 15:11:05 +03:00
feat(sample): make echo bot
This commit is contained in:
parent
1ecc43f2a9
commit
f10df2e960
@ -1,5 +1,6 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TelegrafModule } from '../lib';
|
import { TelegrafModule } from '../lib';
|
||||||
|
import { EchoService } from './echo.service';
|
||||||
import { AppUpdate } from './app.update';
|
import { AppUpdate } from './app.update';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
@ -8,6 +9,6 @@ import { AppUpdate } from './app.update';
|
|||||||
token: '1467731595:AAHCvH65H9VQYKF9jE-E8c2rXsQBVAYseg8',
|
token: '1467731595:AAHCvH65H9VQYKF9jE-E8c2rXsQBVAYseg8',
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
providers: [AppUpdate],
|
providers: [EchoService, AppUpdate],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
@ -1,9 +1,37 @@
|
|||||||
import { On, Update } from '../lib/decorators';
|
import { Telegraf } from 'telegraf';
|
||||||
|
import { Help, InjectBot, On, Start, Update } from '../lib/decorators';
|
||||||
|
import { Context } from '../lib/interfaces';
|
||||||
|
import { EchoService } from './echo.service';
|
||||||
|
|
||||||
@Update()
|
@Update()
|
||||||
export class AppUpdate {
|
export class AppUpdate {
|
||||||
|
constructor(
|
||||||
|
@InjectBot()
|
||||||
|
private readonly bot: Telegraf<Context>,
|
||||||
|
private readonly echoService: EchoService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@Start()
|
||||||
|
async onStart(ctx: Context): Promise<void> {
|
||||||
|
const me = await this.bot.telegram.getMe();
|
||||||
|
await ctx.reply(`Hey, I'm ${me.first_name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Help()
|
||||||
|
async onHelp(ctx: Context): Promise<void> {
|
||||||
|
await ctx.reply('Send me any text');
|
||||||
|
}
|
||||||
|
|
||||||
@On('message')
|
@On('message')
|
||||||
onMessage(): void {
|
async onMessage(ctx: Context): Promise<void> {
|
||||||
console.log('New message received');
|
console.log('New message received');
|
||||||
|
|
||||||
|
if ('text' in ctx.message) {
|
||||||
|
const messageText = ctx.message.text;
|
||||||
|
const echoText = this.echoService.echo(messageText);
|
||||||
|
await ctx.reply(echoText);
|
||||||
|
} else {
|
||||||
|
await ctx.reply('Only text messages');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
sample/echo.service.ts
Normal file
8
sample/echo.service.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class EchoService {
|
||||||
|
echo(text: string): string {
|
||||||
|
return `Echo: ${text}`;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user