feat: add on message decorator handler

This commit is contained in:
Igor Kamyshev 2019-05-20 07:56:11 +03:00
parent 855a1fa625
commit c47c05570b
2 changed files with 10 additions and 0 deletions

View File

@ -9,5 +9,6 @@ interface ArgumentTransformation {
export interface HandleParameters { export interface HandleParameters {
onStart?: boolean onStart?: boolean
command?: string command?: string
message?: string | RegExp
transformations?: ArgumentTransformation[] transformations?: ArgumentTransformation[]
} }

View File

@ -34,6 +34,7 @@ export class TelegramBot {
const handlers = this.createHandlers() const handlers = this.createHandlers()
this.setupOnStart(handlers) this.setupOnStart(handlers)
this.setupOnMessage(handlers)
this.setupOnCommand(handlers) this.setupOnCommand(handlers)
} }
@ -83,6 +84,14 @@ export class TelegramBot {
this.bot.start(this.adoptHandle(head(onStart))) 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 { private setupOnCommand(handlers: Handler[]): void {
const commandHandlers = handlers.filter(({ config }) => config.command) const commandHandlers = handlers.filter(({ config }) => config.command)