From f5d9cf13b6ef67a1f0a3b63004b2e9a67b94f31c Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:58:27 +0300 Subject: [PATCH] feat(decorators): TelegrafMention added --- lib/decorators/telegraf-mention.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 lib/decorators/telegraf-mention.decorator.ts diff --git a/lib/decorators/telegraf-mention.decorator.ts b/lib/decorators/telegraf-mention.decorator.ts new file mode 100644 index 0000000..e7a643b --- /dev/null +++ b/lib/decorators/telegraf-mention.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Mention handling. + * @param username Username + * + * https://telegraf.js.org/#/?id=mention + */ +export function TelegrafMention(username: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.MENTION, { username }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 34c5f06..03809fb 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -78,4 +78,15 @@ export class TelegrafMetadataAccessor { getTelegrafEntityMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.ENTITY, target); } + + isTelegrafMention(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.MENTION, target); + } + + getTelegrafMentionMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.MENTION, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index fa18e0a..66c3371 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -11,4 +11,5 @@ export const DECORATORS = { HELP: `${DECORATORS_PREFIX}/HELP`, SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, ENTITY: `${DECORATORS_PREFIX}/ENTITY`, + MENTION: `${DECORATORS_PREFIX}/MENTION`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 2665b28..f30fafd 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -67,6 +67,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafEntity(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafMention(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafMentionMetadata( + instance[key], + ); + this.handleTelegrafMention(instance, key, telegraf, metadata); } }, ); @@ -142,4 +147,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.entity(metadata.entity, instance[key].bind(instance)); } + + handleTelegrafMention( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.mention(metadata.username, instance[key].bind(instance)); + } }