mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-30 20:31:21 +03:00
feat(sample-app): use include feature
This commit is contained in:
9
sample/echo/echo.module.ts
Normal file
9
sample/echo/echo.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { EchoUpdate } from './echo.update';
|
||||
import { EchoService } from './echo.service';
|
||||
import { HelloScene } from './scenes/hello.scene';
|
||||
|
||||
@Module({
|
||||
providers: [EchoUpdate, EchoService, HelloScene],
|
||||
})
|
||||
export class EchoModule {}
|
8
sample/echo/echo.service.ts
Normal file
8
sample/echo/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}`;
|
||||
}
|
||||
}
|
43
sample/echo/echo.update.ts
Normal file
43
sample/echo/echo.update.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { Command, Help, InjectBot, On, Start, Update } from '../../lib';
|
||||
import { EchoService } from './echo.service';
|
||||
import { HELLO_SCENE_ID } from '../app.constants';
|
||||
import { Context } from '../interfaces/context.interface';
|
||||
|
||||
@Update()
|
||||
export class EchoUpdate {
|
||||
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');
|
||||
}
|
||||
|
||||
@Command('scene')
|
||||
async onSceneCommand(ctx: Context): Promise<void> {
|
||||
await ctx.scene.enter(HELLO_SCENE_ID);
|
||||
}
|
||||
|
||||
@On('message')
|
||||
async onMessage(ctx: Context): Promise<void> {
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
29
sample/echo/scenes/hello.scene.ts
Normal file
29
sample/echo/scenes/hello.scene.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { HELLO_SCENE_ID } from '../../app.constants';
|
||||
import { Context } from '../../interfaces/context.interface';
|
||||
import { Scene, SceneEnter, SceneLeave, Command } from '../../../lib';
|
||||
|
||||
@Scene(HELLO_SCENE_ID)
|
||||
export class HelloScene {
|
||||
@SceneEnter()
|
||||
async onSceneEnter(ctx: Context): Promise<void> {
|
||||
console.log('Enter to scene');
|
||||
await ctx.reply('Welcome on scene ✋');
|
||||
}
|
||||
|
||||
@SceneLeave()
|
||||
async onSceneLeave(ctx: Context): Promise<void> {
|
||||
console.log('Leave from scene');
|
||||
await ctx.reply('Bye Bye 👋');
|
||||
}
|
||||
|
||||
@Command('hello')
|
||||
async onHelloCommand(ctx: Context): Promise<void> {
|
||||
console.log('Use say hello');
|
||||
await ctx.reply('Hi');
|
||||
}
|
||||
|
||||
@Command('leave')
|
||||
async onLeaveCommand(ctx: Context): Promise<void> {
|
||||
await ctx.scene.leave();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user