Skip to content

Commit

Permalink
feat(telegraf-service): support registers middleware for provided upd…
Browse files Browse the repository at this point in the history
…ate type in @TelegramActionHandler decorator
  • Loading branch information
evilsprut committed Feb 11, 2020
1 parent bdff44d commit 2e1e607
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/interfaces/handle-parameters.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContextTransformer } from './'
import { HearsTriggers } from 'telegraf'
import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types'
import { Type } from '@nestjs/common'

interface ArgumentTransformation {
Expand All @@ -9,6 +10,7 @@ interface ArgumentTransformation {

export interface HandleParameters {
onStart?: boolean
on?: UpdateType | UpdateType[] | MessageSubTypes | MessageSubTypes[]
command?: string
message?: string | RegExp
action?: HearsTriggers
Expand Down
9 changes: 9 additions & 0 deletions lib/telegraf.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class TelegrafService {
const handlers = this.createHandlers()

this.setupOnStart(handlers)
this.setupOn(handlers)
this.setupOnMessage(handlers)
this.setupOnCommand(handlers)
this.setupActions(handlers)
Expand Down Expand Up @@ -96,6 +97,14 @@ export class TelegrafService {
this.bot.start(this.adoptHandle(head(onStart)))
}

private setupOn(handlers: Handler[]): void {
const onHandlers = handlers.filter(({ config }) => config.on)

onHandlers.forEach(handler => {
this.bot.on(handler.config.on, this.adoptHandle(handler))
})
}

private setupOnMessage(handlers: Handler[]): void {
const onMessageHandlers = handlers.filter(({ config }) => config.message)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-telegraf",
"version": "0.7.1",
"version": "0.7.2",
"description": "Telegraf module for Nest framework",
"keywords": [
"nest",
Expand Down

0 comments on commit 2e1e607

Please sign in to comment.