mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-03-31 13:53:42 +03:00
feat(sample-app): use include feature
This commit is contained in:
parent
82a9c259f6
commit
334304a823
@ -1,2 +1,3 @@
|
|||||||
export const HELLO_SCENE_ID = 'HELLO_SCENE_ID';
|
export const HELLO_SCENE_ID = 'HELLO_SCENE_ID';
|
||||||
export const SUPPORT_BOT_NAME = 'support';
|
|
||||||
|
export const GreeterBotName = 'greeter';
|
||||||
|
@ -1,23 +1,25 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TelegrafModule } from '../lib';
|
import { TelegrafModule } from '../lib';
|
||||||
import { EchoService } from './echo.service';
|
import { EchoModule } from './echo/echo.module';
|
||||||
import { AppUpdate } from './app.update';
|
import { GreeterModule } from './greeter/greeter.module';
|
||||||
import { HelloScene } from './scenes/hello.scene';
|
|
||||||
import { sessionMiddleware } from './middleware/session.middleware';
|
import { sessionMiddleware } from './middleware/session.middleware';
|
||||||
import { SUPPORT_BOT_NAME } from './app.constants';
|
import { GreeterBotName } from './app.constants';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TelegrafModule.forRoot({
|
TelegrafModule.forRoot({
|
||||||
token: '1417509321:AAEHz8a2QSAP4cTHh4Z6-ulePysFaUx4SjY', // Don't steal >:(
|
token: '1417509321:AAEHz8a2QSAP4cTHh4Z6-ulePysFaUx4SjY', // Don't steal >:(
|
||||||
middlewares: [sessionMiddleware],
|
middlewares: [sessionMiddleware],
|
||||||
|
include: [EchoModule],
|
||||||
}),
|
}),
|
||||||
TelegrafModule.forRoot({
|
TelegrafModule.forRoot({
|
||||||
name: SUPPORT_BOT_NAME,
|
name: GreeterBotName,
|
||||||
token: '1435922623:AAHmBmnyqroHxDbuK6OKsLV8Y_oB_Lf9E6E', // Don't steal >:(
|
token: '1435922623:AAHmBmnyqroHxDbuK6OKsLV8Y_oB_Lf9E6E', // Don't steal >:(
|
||||||
middlewares: [sessionMiddleware],
|
middlewares: [sessionMiddleware],
|
||||||
|
include: [GreeterModule],
|
||||||
}),
|
}),
|
||||||
|
EchoModule,
|
||||||
|
GreeterModule,
|
||||||
],
|
],
|
||||||
providers: [EchoService, AppUpdate, HelloScene],
|
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
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 {}
|
@ -1,22 +1,20 @@
|
|||||||
import { Telegraf } from 'telegraf';
|
import { Telegraf } from 'telegraf';
|
||||||
import { Command, Help, InjectBot, On, Start, Update } from '../lib';
|
import { Command, Help, InjectBot, On, Start, Update } from '../../lib';
|
||||||
import { EchoService } from './echo.service';
|
import { EchoService } from './echo.service';
|
||||||
import { HELLO_SCENE_ID, SUPPORT_BOT_NAME } from './app.constants';
|
import { HELLO_SCENE_ID } from '../app.constants';
|
||||||
import { Context } from './interfaces/context.interface';
|
import { Context } from '../interfaces/context.interface';
|
||||||
|
|
||||||
@Update()
|
@Update()
|
||||||
export class AppUpdate {
|
export class EchoUpdate {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectBot()
|
@InjectBot()
|
||||||
private readonly defaultBot: Telegraf<Context>,
|
private readonly bot: Telegraf<Context>,
|
||||||
@InjectBot(SUPPORT_BOT_NAME)
|
|
||||||
private readonly supportBot: Telegraf<Context>,
|
|
||||||
private readonly echoService: EchoService,
|
private readonly echoService: EchoService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Start()
|
@Start()
|
||||||
async onStart(ctx: Context): Promise<void> {
|
async onStart(ctx: Context): Promise<void> {
|
||||||
const me = await this.defaultBot.telegram.getMe();
|
const me = await this.bot.telegram.getMe();
|
||||||
await ctx.reply(`Hey, I'm ${me.first_name}`);
|
await ctx.reply(`Hey, I'm ${me.first_name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,12 +23,6 @@ export class AppUpdate {
|
|||||||
await ctx.reply('Send me any text');
|
await ctx.reply('Send me any text');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command('support')
|
|
||||||
async onSupportCommand(ctx: Context): Promise<void> {
|
|
||||||
const me = await this.supportBot.telegram.getMe();
|
|
||||||
await ctx.reply(`Greetings from ${me.first_name}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Command('scene')
|
@Command('scene')
|
||||||
async onSceneCommand(ctx: Context): Promise<void> {
|
async onSceneCommand(ctx: Context): Promise<void> {
|
||||||
await ctx.scene.enter(HELLO_SCENE_ID);
|
await ctx.scene.enter(HELLO_SCENE_ID);
|
@ -1,6 +1,6 @@
|
|||||||
import { HELLO_SCENE_ID } from '../app.constants';
|
import { HELLO_SCENE_ID } from '../../app.constants';
|
||||||
import { Context } from '../interfaces/context.interface';
|
import { Context } from '../../interfaces/context.interface';
|
||||||
import { Scene, SceneEnter, SceneLeave, Command } from '../../lib';
|
import { Scene, SceneEnter, SceneLeave, Command } from '../../../lib';
|
||||||
|
|
||||||
@Scene(HELLO_SCENE_ID)
|
@Scene(HELLO_SCENE_ID)
|
||||||
export class HelloScene {
|
export class HelloScene {
|
7
sample/greeter/greeter.module.ts
Normal file
7
sample/greeter/greeter.module.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { GreeterUpdate } from './greeter.update';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [GreeterUpdate],
|
||||||
|
})
|
||||||
|
export class GreeterModule {}
|
12
sample/greeter/greeter.update.ts
Normal file
12
sample/greeter/greeter.update.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { Hears } from '../../lib';
|
||||||
|
import { Context } from '../interfaces/context.interface';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class GreeterUpdate {
|
||||||
|
@Hears(['hi', 'hello', 'hey', 'qq'])
|
||||||
|
async onGreetings(ctx: Context): Promise<void> {
|
||||||
|
const { first_name } = ctx.from;
|
||||||
|
await ctx.reply(`Hey ${first_name}`);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user