diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 8a61847..071c1a8 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -9,3 +9,4 @@ export * from './telegraf-entity.decorator'; export * from './telegraf-mention.decorator'; export * from './telegraf-phone.decorator'; export * from './telegraf-hashtag.decorator'; +export * from './telegraf-cashtag.decorator'; diff --git a/lib/decorators/telegraf-cashtag.decorator.ts b/lib/decorators/telegraf-cashtag.decorator.ts new file mode 100644 index 0000000..40a6038 --- /dev/null +++ b/lib/decorators/telegraf-cashtag.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Cashtag handling. + * @param cashtag Cashtag + * + * https://telegraf.js.org/#/?id=cashtag + */ +export function TelegrafCashtag(cashtag: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.CASHTAG, { cashtag }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index ba2aa76..b3c25a8 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -111,4 +111,15 @@ export class TelegrafMetadataAccessor { getTelegrafHashtagMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.HASHTAG, target); } + + isTelegrafCashtag(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.CASHTAG, target); + } + + getTelegrafCashtagMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.CASHTAG, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 3617ba7..118123c 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -14,4 +14,5 @@ export const DECORATORS = { MENTION: `${DECORATORS_PREFIX}/MENTION`, PHONE: `${DECORATORS_PREFIX}/PHONE`, HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, + CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 30cd359..bd60a95 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -82,6 +82,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafHashtag(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafCashtagMetadata( + instance[key], + ); + this.handleTelegrafCashtag(instance, key, telegraf, metadata); } }, ); @@ -187,4 +192,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); } + + handleTelegrafCashtag( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); + } }