diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 071c1a8..fce4328 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -10,3 +10,4 @@ export * from './telegraf-mention.decorator'; export * from './telegraf-phone.decorator'; export * from './telegraf-hashtag.decorator'; export * from './telegraf-cashtag.decorator'; +export * from './telegraf-action.decorator'; diff --git a/lib/decorators/telegraf-action.decorator.ts b/lib/decorators/telegraf-action.decorator.ts new file mode 100644 index 0000000..ed58872 --- /dev/null +++ b/lib/decorators/telegraf-action.decorator.ts @@ -0,0 +1,13 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; +import { HearsTriggers } from 'telegraf'; + +/** + * Registers middleware for handling callback_data actions with regular expressions. + * @param triggers Triggers + * + * https://telegraf.js.org/#/?id=action + */ +export function TelegrafAction(triggers: HearsTriggers): MethodDecorator { + return SetMetadata(DECORATORS.ACTION, { triggers }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index b3c25a8..634f2e9 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -122,4 +122,15 @@ export class TelegrafMetadataAccessor { getTelegrafCashtagMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.CASHTAG, target); } + + isTelegrafAction(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.ACTION, target); + } + + getTelegrafActionMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.ACTION, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 118123c..cd8d91b 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -15,4 +15,5 @@ export const DECORATORS = { PHONE: `${DECORATORS_PREFIX}/PHONE`, HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, + ACTION: `${DECORATORS_PREFIX}/ACTION`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index bd60a95..635a69c 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -87,6 +87,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafCashtag(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafAction(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafActionMetadata( + instance[key], + ); + this.handleTelegrafAction(instance, key, telegraf, metadata); } }, ); @@ -202,4 +207,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); } + + handleTelegrafAction( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.cashtag(metadata.triggers, instance[key].bind(instance)); + } }