feat(decorators): TelegrafInlineQuery added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 17:25:47 +03:00
parent 76e0eee3ad
commit f65905ed0d
5 changed files with 44 additions and 0 deletions

View File

@ -11,3 +11,4 @@ export * from './telegraf-phone.decorator';
export * from './telegraf-hashtag.decorator';
export * from './telegraf-cashtag.decorator';
export * from './telegraf-action.decorator';
export * from './telegraf-inline-query.decorator';

View File

@ -0,0 +1,14 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
export type Triggers = string | string[] | RegExp | RegExp[];
/**
* Registers middleware for handling inline_query actions with regular expressions.
* @param triggers Triggers
*
* https://telegraf.js.org/#/?id=inlinequery
*/
export function TelegrafInlineQuery(triggers: Triggers): MethodDecorator {
return SetMetadata(DECORATORS.INLINE_QUERY, { triggers });
}

View File

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

View File

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

View File

@ -92,6 +92,13 @@ export class TelegrafExplorer implements OnModuleInit {
instance[key],
);
this.handleTelegrafAction(instance, key, telegraf, metadata);
} else if (
this.metadataAccessor.isTelegrafInlineQuery(instance[key])
) {
const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata(
instance[key],
);
this.handleTelegrafInlineQuery(instance, key, telegraf, metadata);
}
},
);
@ -216,4 +223,14 @@ export class TelegrafExplorer implements OnModuleInit {
) {
telegraf.action(metadata.triggers, instance[key].bind(instance));
}
handleTelegrafInlineQuery(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
metadata: any,
) {
// @ts-ignore
telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));
}
}