From 334304a8236100cca48a00bdfb0d1b10011a5edd Mon Sep 17 00:00:00 2001 From: Morb0 Date: Sun, 3 Jan 2021 14:26:32 +0300 Subject: [PATCH] feat(sample-app): use include feature --- sample/app.constants.ts | 3 ++- sample/app.module.ts | 14 +++++++------ sample/echo/echo.module.ts | 9 +++++++++ sample/{ => echo}/echo.service.ts | 0 sample/{app.update.ts => echo/echo.update.ts} | 20 ++++++------------- sample/{ => echo}/scenes/hello.scene.ts | 6 +++--- sample/greeter/greeter.module.ts | 7 +++++++ sample/greeter/greeter.update.ts | 12 +++++++++++ 8 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 sample/echo/echo.module.ts rename sample/{ => echo}/echo.service.ts (100%) rename sample/{app.update.ts => echo/echo.update.ts} (63%) rename sample/{ => echo}/scenes/hello.scene.ts (76%) create mode 100644 sample/greeter/greeter.module.ts create mode 100644 sample/greeter/greeter.update.ts diff --git a/sample/app.constants.ts b/sample/app.constants.ts index c68d247..771789e 100644 --- a/sample/app.constants.ts +++ b/sample/app.constants.ts @@ -1,2 +1,3 @@ export const HELLO_SCENE_ID = 'HELLO_SCENE_ID'; -export const SUPPORT_BOT_NAME = 'support'; + +export const GreeterBotName = 'greeter'; diff --git a/sample/app.module.ts b/sample/app.module.ts index c7e7230..9b728f5 100644 --- a/sample/app.module.ts +++ b/sample/app.module.ts @@ -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 {} diff --git a/sample/echo/echo.module.ts b/sample/echo/echo.module.ts new file mode 100644 index 0000000..29c1bff --- /dev/null +++ b/sample/echo/echo.module.ts @@ -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 {} diff --git a/sample/echo.service.ts b/sample/echo/echo.service.ts similarity index 100% rename from sample/echo.service.ts rename to sample/echo/echo.service.ts diff --git a/sample/app.update.ts b/sample/echo/echo.update.ts similarity index 63% rename from sample/app.update.ts rename to sample/echo/echo.update.ts index 8c35359..f5a154d 100644 --- a/sample/app.update.ts +++ b/sample/echo/echo.update.ts @@ -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, - @InjectBot(SUPPORT_BOT_NAME) - private readonly supportBot: Telegraf, + private readonly bot: Telegraf, private readonly echoService: EchoService, ) {} @Start() async onStart(ctx: Context): Promise { - 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 { - const me = await this.supportBot.telegram.getMe(); - await ctx.reply(`Greetings from ${me.first_name}`); - } - @Command('scene') async onSceneCommand(ctx: Context): Promise { await ctx.scene.enter(HELLO_SCENE_ID); diff --git a/sample/scenes/hello.scene.ts b/sample/echo/scenes/hello.scene.ts similarity index 76% rename from sample/scenes/hello.scene.ts rename to sample/echo/scenes/hello.scene.ts index f84a6f4..6f3f2bf 100644 --- a/sample/scenes/hello.scene.ts +++ b/sample/echo/scenes/hello.scene.ts @@ -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 { diff --git a/sample/greeter/greeter.module.ts b/sample/greeter/greeter.module.ts new file mode 100644 index 0000000..40160db --- /dev/null +++ b/sample/greeter/greeter.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { GreeterUpdate } from './greeter.update'; + +@Module({ + providers: [GreeterUpdate], +}) +export class GreeterModule {} diff --git a/sample/greeter/greeter.update.ts b/sample/greeter/greeter.update.ts new file mode 100644 index 0000000..cdb0bae --- /dev/null +++ b/sample/greeter/greeter.update.ts @@ -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 { + const { first_name } = ctx.from; + await ctx.reply(`Hey ${first_name}`); + } +}