feat(decorators): TelegrafGameQuery added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 17:28:56 +03:00
parent f65905ed0d
commit a2eb619e6b
5 changed files with 30 additions and 0 deletions

View File

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

View File

@ -0,0 +1,11 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
/**
* Registers middleware for handling callback_data actions with game query.
*
* https://telegraf.js.org/#/?id=inlinequery
*/
export function TelegrafGameQuery(): MethodDecorator {
return SetMetadata(DECORATORS.GAME_QUERY, {});
}

View File

@ -144,4 +144,11 @@ export class TelegrafMetadataAccessor {
getTelegrafInlineQueryMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.INLINE_QUERY, target);
}
isTelegrafGameQuery(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.GAME_QUERY, target);
}
}

View File

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

View File

@ -99,6 +99,8 @@ export class TelegrafExplorer implements OnModuleInit {
instance[key],
);
this.handleTelegrafInlineQuery(instance, key, telegraf, metadata);
} else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) {
this.handleTelegrafGameQuery(instance, key, telegraf);
}
},
);
@ -233,4 +235,12 @@ export class TelegrafExplorer implements OnModuleInit {
// @ts-ignore
telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));
}
handleTelegrafGameQuery(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
) {
telegraf.gameQuery(instance[key].bind(instance));
}
}