From c47c05570ba23d379bcde29e8bdcc7639b7aaaf1 Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Mon, 20 May 2019 07:56:11 +0300 Subject: [PATCH] feat: add on message decorator handler --- lib/HandleParameters.ts | 1 + lib/TelegramBot.ts | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/HandleParameters.ts b/lib/HandleParameters.ts index b0c1551..2bccb39 100644 --- a/lib/HandleParameters.ts +++ b/lib/HandleParameters.ts @@ -9,5 +9,6 @@ interface ArgumentTransformation { export interface HandleParameters { onStart?: boolean command?: string + message?: string | RegExp transformations?: ArgumentTransformation[] } diff --git a/lib/TelegramBot.ts b/lib/TelegramBot.ts index f306150..630dac9 100644 --- a/lib/TelegramBot.ts +++ b/lib/TelegramBot.ts @@ -34,6 +34,7 @@ export class TelegramBot { const handlers = this.createHandlers() this.setupOnStart(handlers) + this.setupOnMessage(handlers) this.setupOnCommand(handlers) } @@ -83,6 +84,14 @@ export class TelegramBot { this.bot.start(this.adoptHandle(head(onStart))) } + private setupOnMessage(handlers: Handler[]): void { + const onMessageHandlers = handlers.filter(({ config }) => config.message) + + onMessageHandlers.forEach(handler => { + this.bot.hears(handler.config.message, this.adoptHandle(handler)) + }) + } + private setupOnCommand(handlers: Handler[]): void { const commandHandlers = handlers.filter(({ config }) => config.command)