feat(decorators): TelegrafPhone added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 17:02:37 +03:00
parent f5d9cf13b6
commit 225c931dbb
5 changed files with 42 additions and 0 deletions

View File

@ -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';

View File

@ -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 });
}

View File

@ -89,4 +89,15 @@ export class TelegrafMetadataAccessor {
getTelegrafMentionMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.MENTION, target);
}
isTelegrafPhone(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.PHONE, target);
}
getTelegrafPhoneMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.PHONE, target);
}
}

View File

@ -12,4 +12,5 @@ export const DECORATORS = {
SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`,
ENTITY: `${DECORATORS_PREFIX}/ENTITY`,
MENTION: `${DECORATORS_PREFIX}/MENTION`,
PHONE: `${DECORATORS_PREFIX}/PHONE`,
};

View File

@ -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<ContextMessageUpdate>,
metadata: any,
) {
// @ts-ignore
telegraf.phone(metadata.phone, instance[key].bind(instance));
}
}