diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index dd968ab..759e533 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -4,4 +4,7 @@ export * from './telegraf-hears.decorator'; export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; export * from './telegraf-help.decorator'; +export * from './telegraf-settings.decorator'; export * from './telegraf-entity.decorator'; +export * from './telegraf-mention.decorator'; +export * from './telegraf-phone.decorator'; diff --git a/lib/decorators/telegraf-phone.decorator.ts b/lib/decorators/telegraf-phone.decorator.ts new file mode 100644 index 0000000..05ee3e9 --- /dev/null +++ b/lib/decorators/telegraf-phone.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Phone number handling. + * @param phone Phone number + * + * https://telegraf.js.org/#/?id=phone + */ +export function TelegrafPhone(phone: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.PHONE, { phone }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 03809fb..df440f7 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -89,4 +89,15 @@ export class TelegrafMetadataAccessor { getTelegrafMentionMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.MENTION, target); } + + isTelegrafPhone(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.PHONE, target); + } + + getTelegrafPhoneMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.PHONE, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 66c3371..80f8de8 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -12,4 +12,5 @@ export const DECORATORS = { SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, ENTITY: `${DECORATORS_PREFIX}/ENTITY`, MENTION: `${DECORATORS_PREFIX}/MENTION`, + PHONE: `${DECORATORS_PREFIX}/PHONE`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index f30fafd..f85f1dd 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -72,6 +72,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafMention(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafPhone(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafPhoneMetadata( + instance[key], + ); + this.handleTelegrafPhone(instance, key, telegraf, metadata); } }, ); @@ -157,4 +162,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.mention(metadata.username, instance[key].bind(instance)); } + + handleTelegrafPhone( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.phone(metadata.phone, instance[key].bind(instance)); + } }