feat(decorators): TelegrafAction added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 17:20:13 +03:00
parent 7d3e266f0f
commit 511f9ebd77
5 changed files with 41 additions and 0 deletions

View File

@ -10,3 +10,4 @@ export * from './telegraf-mention.decorator';
export * from './telegraf-phone.decorator'; export * from './telegraf-phone.decorator';
export * from './telegraf-hashtag.decorator'; export * from './telegraf-hashtag.decorator';
export * from './telegraf-cashtag.decorator'; export * from './telegraf-cashtag.decorator';
export * from './telegraf-action.decorator';

View File

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

View File

@ -122,4 +122,15 @@ export class TelegrafMetadataAccessor {
getTelegrafCashtagMetadata(target: Type<any> | Function) { getTelegrafCashtagMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.CASHTAG, target); return this.reflector.get(DECORATORS.CASHTAG, target);
} }
isTelegrafAction(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.ACTION, target);
}
getTelegrafActionMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.ACTION, target);
}
} }

View File

@ -15,4 +15,5 @@ export const DECORATORS = {
PHONE: `${DECORATORS_PREFIX}/PHONE`, PHONE: `${DECORATORS_PREFIX}/PHONE`,
HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`,
CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`,
ACTION: `${DECORATORS_PREFIX}/ACTION`,
}; };

View File

@ -87,6 +87,11 @@ export class TelegrafExplorer implements OnModuleInit {
instance[key], instance[key],
); );
this.handleTelegrafCashtag(instance, key, telegraf, metadata); 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 // @ts-ignore
telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
} }
handleTelegrafAction(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
) {
// @ts-ignore
telegraf.cashtag(metadata.triggers, instance[key].bind(instance));
}
} }