From f65905ed0dc556a2f848e8f86d9aa33e6b71b04d Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:25:47 +0300 Subject: [PATCH] feat(decorators): TelegrafInlineQuery added --- lib/decorators/index.ts | 1 + .../telegraf-inline-query.decorator.ts | 14 ++++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 17 +++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 lib/decorators/telegraf-inline-query.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index fce4328..8bc9e67 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -11,3 +11,4 @@ export * from './telegraf-phone.decorator'; export * from './telegraf-hashtag.decorator'; export * from './telegraf-cashtag.decorator'; export * from './telegraf-action.decorator'; +export * from './telegraf-inline-query.decorator'; diff --git a/lib/decorators/telegraf-inline-query.decorator.ts b/lib/decorators/telegraf-inline-query.decorator.ts new file mode 100644 index 0000000..2f3b6e6 --- /dev/null +++ b/lib/decorators/telegraf-inline-query.decorator.ts @@ -0,0 +1,14 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +export type Triggers = string | string[] | RegExp | RegExp[]; + +/** + * Registers middleware for handling inline_query actions with regular expressions. + * @param triggers Triggers + * + * https://telegraf.js.org/#/?id=inlinequery + */ +export function TelegrafInlineQuery(triggers: Triggers): MethodDecorator { + return SetMetadata(DECORATORS.INLINE_QUERY, { triggers }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 634f2e9..94eb7d9 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -133,4 +133,15 @@ export class TelegrafMetadataAccessor { getTelegrafActionMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.ACTION, target); } + + isTelegrafInlineQuery(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.INLINE_QUERY, target); + } + + getTelegrafInlineQueryMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.INLINE_QUERY, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index cd8d91b..e86ca9a 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -16,4 +16,5 @@ export const DECORATORS = { HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, ACTION: `${DECORATORS_PREFIX}/ACTION`, + INLINE_QUERY: `${DECORATORS_PREFIX}/INLINE_QUERY`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 830cc9c..2148959 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -92,6 +92,13 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafAction(instance, key, telegraf, metadata); + } else if ( + this.metadataAccessor.isTelegrafInlineQuery(instance[key]) + ) { + const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata( + instance[key], + ); + this.handleTelegrafInlineQuery(instance, key, telegraf, metadata); } }, ); @@ -216,4 +223,14 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.action(metadata.triggers, instance[key].bind(instance)); } + + handleTelegrafInlineQuery( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance)); + } }