feat(sample-app): use include feature

This commit is contained in:
Morb0 2021-01-03 14:26:32 +03:00
parent 82a9c259f6
commit 334304a823
8 changed files with 47 additions and 24 deletions

View File

@ -1,2 +1,3 @@
export const HELLO_SCENE_ID = 'HELLO_SCENE_ID';
export const SUPPORT_BOT_NAME = 'support';
export const GreeterBotName = 'greeter';

View File

@ -1,23 +1,25 @@
import { Module } from '@nestjs/common';
import { TelegrafModule } from '../lib';
import { EchoService } from './echo.service';
import { AppUpdate } from './app.update';
import { HelloScene } from './scenes/hello.scene';
import { EchoModule } from './echo/echo.module';
import { GreeterModule } from './greeter/greeter.module';
import { sessionMiddleware } from './middleware/session.middleware';
import { SUPPORT_BOT_NAME } from './app.constants';
import { GreeterBotName } from './app.constants';
@Module({
imports: [
TelegrafModule.forRoot({
token: '1417509321:AAEHz8a2QSAP4cTHh4Z6-ulePysFaUx4SjY', // Don't steal >:(
middlewares: [sessionMiddleware],
include: [EchoModule],
}),
TelegrafModule.forRoot({
name: SUPPORT_BOT_NAME,
name: GreeterBotName,
token: '1435922623:AAHmBmnyqroHxDbuK6OKsLV8Y_oB_Lf9E6E', // Don't steal >:(
middlewares: [sessionMiddleware],
include: [GreeterModule],
}),
EchoModule,
GreeterModule,
],
providers: [EchoService, AppUpdate, HelloScene],
})
export class AppModule {}

View 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 {}

View File

@ -1,22 +1,20 @@
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 { HELLO_SCENE_ID, SUPPORT_BOT_NAME } from './app.constants';
import { Context } from './interfaces/context.interface';
import { HELLO_SCENE_ID } from '../app.constants';
import { Context } from '../interfaces/context.interface';
@Update()
export class AppUpdate {
export class EchoUpdate {
constructor(
@InjectBot()
private readonly defaultBot: Telegraf<Context>,
@InjectBot(SUPPORT_BOT_NAME)
private readonly supportBot: Telegraf<Context>,
private readonly bot: Telegraf<Context>,
private readonly echoService: EchoService,
) {}
@Start()
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}`);
}
@ -25,12 +23,6 @@ export class AppUpdate {
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')
async onSceneCommand(ctx: Context): Promise<void> {
await ctx.scene.enter(HELLO_SCENE_ID);

View File

@ -1,6 +1,6 @@
import { HELLO_SCENE_ID } from '../app.constants';
import { Context } from '../interfaces/context.interface';
import { Scene, SceneEnter, SceneLeave, Command } from '../../lib';
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 {

View File

@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { GreeterUpdate } from './greeter.update';
@Module({
providers: [GreeterUpdate],
})
export class GreeterModule {}

View 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}`);
}
}