feat: new decorators for telegram bot api updates (#119)

This commit is contained in:
Aleksandr Bukhalo 2020-09-09 22:46:25 +03:00 committed by GitHub
parent f496e51013
commit 6350a6e8d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 204 additions and 94 deletions

View File

@ -14,4 +14,5 @@ export * from './phone.decorator';
export * from './settings.decorator'; export * from './settings.decorator';
export * from './start.decorator'; export * from './start.decorator';
export * from './update.decorator'; export * from './update.decorator';
export * from './update-hooks.decorators';
export * from './use.decorator'; export * from './use.decorator';

View File

@ -0,0 +1,103 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
import * as tt from 'telegraf/typings/telegram-types';
export interface UpdateHookOptions {
updateType:
| tt.UpdateType
| tt.UpdateType[]
| tt.MessageSubTypes
| tt.MessageSubTypes[];
}
/**
* New incoming message of any kind text, photo, sticker, etc.
* @constructor
*/
export const OnMessage = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'message',
});
};
/**
* New version of a message that is known to the bot and was edited
* @constructor
*/
export const OnEditedMessage = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'edited_message',
});
};
/**
* New incoming channel post of any kind text, photo, sticker, etc.
* @constructor
*/
export const OnChannelPost = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'channel_post',
});
};
/**
* New version of a channel post that is known to the bot and was edited
* @constructor
*/
export const OnEditedChannelPost = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'edited_channel_post',
});
};
/**
* New incoming inline query
* @constructor
*/
export const OnInlineQuery = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'inline_query',
});
};
/**
* The result of an inline query that was chosen by a user and sent to their chat partner.
* @constructor
*/
export const OnChosenInlineResult = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'chosen_inline_result',
});
};
/**
* New incoming callback query
* @constructor
*/
export const OnCallbackQuery = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'callback_query',
});
};
/**
* New incoming shipping query. Only for invoices with flexible price
* @constructor
*/
export const OnShippingQuery = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'shipping_query',
});
};
/**
* New incoming pre-checkout query. Contains full information about checkout
* @constructor
*/
export const OnPreCheckoutQuery = (): MethodDecorator => {
return SetMetadata(DECORATORS.UPDATE_HOOK, {
updateType: 'pre_checkout_query',
});
};
// Two more decorators are missing here. For 'poll' and 'poll_answer' update types.

View File

@ -3,6 +3,6 @@ import { DECORATORS } from '../telegraf.constants';
/** /**
* `@Update` decorator, it's like NestJS `@Controller` decorator, * `@Update` decorator, it's like NestJS `@Controller` decorator,
* only for Telegram Bot API updates. * but for Telegram Bot API updates.
*/ */
export const Update = (): ClassDecorator => SetMetadata(DECORATORS.UPDATE, {}); export const Update = (): ClassDecorator => SetMetadata(DECORATORS.UPDATE, {});

View File

@ -11,6 +11,7 @@ import {
MentionOptions, MentionOptions,
OnOptions, OnOptions,
PhoneOptions, PhoneOptions,
UpdateHookOptions,
} from './decorators'; } from './decorators';
import { DECORATORS } from './telegraf.constants'; import { DECORATORS } from './telegraf.constants';
@ -18,6 +19,26 @@ import { DECORATORS } from './telegraf.constants';
export class TelegrafMetadataAccessor { export class TelegrafMetadataAccessor {
constructor(private readonly reflector: Reflector) {} constructor(private readonly reflector: Reflector) {}
isUpdate(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.UPDATE, target);
}
isUpdateHook(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.UPDATE_HOOK, target);
}
getUpdateHookMetadata(
target: Type<any> | Function,
): UpdateHookOptions | undefined {
return this.reflector.get(DECORATORS.UPDATE_HOOK, target);
}
isTelegrafUse(target: Type<any> | Function): boolean { isTelegrafUse(target: Type<any> | Function): boolean {
if (!target) { if (!target) {
return false; return false;

View File

@ -19,4 +19,5 @@ export const DECORATORS = {
INLINE_QUERY: `${DECORATORS_PREFIX}/INLINE_QUERY`, INLINE_QUERY: `${DECORATORS_PREFIX}/INLINE_QUERY`,
GAME_QUERY: `${DECORATORS_PREFIX}/GAME_QUERY`, GAME_QUERY: `${DECORATORS_PREFIX}/GAME_QUERY`,
UPDATE: `${DECORATORS_PREFIX}/UPDATE`, UPDATE: `${DECORATORS_PREFIX}/UPDATE`,
UPDATE_HOOK: `${DECORATORS_PREFIX}/UPDATE_HOOK`,
}; };

View File

@ -16,6 +16,7 @@ import {
MentionOptions, MentionOptions,
OnOptions, OnOptions,
PhoneOptions, PhoneOptions,
UpdateHookOptions,
} from './decorators'; } from './decorators';
@Injectable() @Injectable()
@ -27,12 +28,44 @@ export class TelegrafExplorer implements OnModuleInit {
private readonly metadataScanner: MetadataScanner, private readonly metadataScanner: MetadataScanner,
) {} ) {}
private telegraf: TelegrafProvider;
onModuleInit() { onModuleInit() {
this.telegraf = this.moduleRef.get<TelegrafProvider>(TELEGRAF_PROVIDER, {
strict: false,
});
this.explore(); this.explore();
} }
explore() { explore() {
/**
* Update providers section is only for decorators under Update decorator
*/
const updateProviders: InstanceWrapper[] = this.discoveryService
.getProviders()
.filter((wrapper: InstanceWrapper) =>
this.metadataAccessor.isUpdate(wrapper.metatype),
);
updateProviders.forEach((wrapper: InstanceWrapper) => {
const { instance } = wrapper;
this.metadataScanner.scanFromPrototype(
instance,
Object.getPrototypeOf(instance),
(key: string) => {
if (this.metadataAccessor.isUpdateHook(instance[key])) {
const metadata = this.metadataAccessor.getUpdateHookMetadata(
instance[key],
);
this.handleUpdateHook(instance, key, metadata);
}
},
);
});
const providers: InstanceWrapper[] = this.discoveryService.getProviders(); const providers: InstanceWrapper[] = this.discoveryService.getProviders();
providers.forEach((wrapper: InstanceWrapper) => { providers.forEach((wrapper: InstanceWrapper) => {
const { instance } = wrapper; const { instance } = wrapper;
@ -40,215 +73,166 @@ export class TelegrafExplorer implements OnModuleInit {
return; return;
} }
const telegraf: TelegrafProvider = this.moduleRef.get<TelegrafProvider>(
TELEGRAF_PROVIDER,
{
strict: false,
},
);
this.metadataScanner.scanFromPrototype( this.metadataScanner.scanFromPrototype(
instance, instance,
Object.getPrototypeOf(instance), Object.getPrototypeOf(instance),
(key: string) => { (key: string) => {
if (this.metadataAccessor.isTelegrafUse(instance[key])) { if (this.metadataAccessor.isTelegrafUse(instance[key])) {
this.handleTelegrafUse(instance, key, telegraf); this.handleTelegrafUse(instance, key);
} else if (this.metadataAccessor.isTelegrafOn(instance[key])) { } else if (this.metadataAccessor.isTelegrafOn(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafOnMetadata( const metadata = this.metadataAccessor.getTelegrafOnMetadata(
instance[key], instance[key],
); );
this.handleTelegrafOn(instance, key, telegraf, metadata); this.handleTelegrafOn(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafHears(instance[key])) { } else if (this.metadataAccessor.isTelegrafHears(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafHearsMetadata( const metadata = this.metadataAccessor.getTelegrafHearsMetadata(
instance[key], instance[key],
); );
this.handleTelegrafHears(instance, key, telegraf, metadata); this.handleTelegrafHears(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafCommand(instance[key])) { } else if (this.metadataAccessor.isTelegrafCommand(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafCommandMetadata( const metadata = this.metadataAccessor.getTelegrafCommandMetadata(
instance[key], instance[key],
); );
this.handleTelegrafCommand(instance, key, telegraf, metadata); this.handleTelegrafCommand(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafStart(instance[key])) { } else if (this.metadataAccessor.isTelegrafStart(instance[key])) {
this.handleTelegrafStart(instance, key, telegraf); this.handleTelegrafStart(instance, key);
} else if (this.metadataAccessor.isTelegrafHelp(instance[key])) { } else if (this.metadataAccessor.isTelegrafHelp(instance[key])) {
this.handleTelegrafHelp(instance, key, telegraf); this.handleTelegrafHelp(instance, key);
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) { } else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
this.handleTelegrafSettings(instance, key, telegraf); this.handleTelegrafSettings(instance, key);
} else if (this.metadataAccessor.isTelegrafEntity(instance[key])) { } else if (this.metadataAccessor.isTelegrafEntity(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafEntityMetadata( const metadata = this.metadataAccessor.getTelegrafEntityMetadata(
instance[key], instance[key],
); );
this.handleTelegrafEntity(instance, key, telegraf, metadata); this.handleTelegrafEntity(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafMention(instance[key])) { } else if (this.metadataAccessor.isTelegrafMention(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafMentionMetadata( const metadata = this.metadataAccessor.getTelegrafMentionMetadata(
instance[key], instance[key],
); );
this.handleTelegrafMention(instance, key, telegraf, metadata); this.handleTelegrafMention(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafPhone(instance[key])) { } else if (this.metadataAccessor.isTelegrafPhone(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafPhoneMetadata( const metadata = this.metadataAccessor.getTelegrafPhoneMetadata(
instance[key], instance[key],
); );
this.handleTelegrafPhone(instance, key, telegraf, metadata); this.handleTelegrafPhone(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) { } else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafHashtagMetadata( const metadata = this.metadataAccessor.getTelegrafHashtagMetadata(
instance[key], instance[key],
); );
this.handleTelegrafHashtag(instance, key, telegraf, metadata); this.handleTelegrafHashtag(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) { } else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafCashtagMetadata( const metadata = this.metadataAccessor.getTelegrafCashtagMetadata(
instance[key], instance[key],
); );
this.handleTelegrafCashtag(instance, key, telegraf, metadata); this.handleTelegrafCashtag(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafAction(instance[key])) { } else if (this.metadataAccessor.isTelegrafAction(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafActionMetadata( const metadata = this.metadataAccessor.getTelegrafActionMetadata(
instance[key], instance[key],
); );
this.handleTelegrafAction(instance, key, telegraf, metadata); this.handleTelegrafAction(instance, key, metadata);
} else if ( } else if (
this.metadataAccessor.isTelegrafInlineQuery(instance[key]) this.metadataAccessor.isTelegrafInlineQuery(instance[key])
) { ) {
const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata( const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata(
instance[key], instance[key],
); );
this.handleTelegrafInlineQuery(instance, key, telegraf, metadata); this.handleTelegrafInlineQuery(instance, key, metadata);
} else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) { } else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) {
this.handleTelegrafGameQuery(instance, key, telegraf); this.handleTelegrafGameQuery(instance, key);
} }
}, },
); );
}); });
} }
handleTelegrafUse(instance: object, key: string, telegraf: TelegrafProvider) { handleUpdateHook(instance: object, key: string, metadata: UpdateHookOptions) {
telegraf.use(instance[key].bind(instance)); this.telegraf.on(metadata.updateType, instance[key].bind(instance));
} }
handleTelegrafOn( handleTelegrafUse(instance: object, key: string) {
instance: object, this.telegraf.use(instance[key].bind(instance));
key: string,
telegraf: TelegrafProvider,
metadata: OnOptions,
) {
telegraf.on(metadata.updateTypes, instance[key].bind(instance));
} }
handleTelegrafHears( handleTelegrafOn(instance: object, key: string, metadata: OnOptions) {
instance: object, this.telegraf.on(metadata.updateTypes, instance[key].bind(instance));
key: string, }
telegraf: TelegrafProvider,
metadata: HearsOptions, handleTelegrafHears(instance: object, key: string, metadata: HearsOptions) {
) { this.telegraf.hears(metadata.triggers, instance[key].bind(instance));
telegraf.hears(metadata.triggers, instance[key].bind(instance));
} }
handleTelegrafCommand( handleTelegrafCommand(
instance: object, instance: object,
key: string, key: string,
telegraf: TelegrafProvider,
metadata: CommandOptions, metadata: CommandOptions,
) { ) {
telegraf.command(metadata.commands, instance[key].bind(instance)); this.telegraf.command(metadata.commands, instance[key].bind(instance));
} }
handleTelegrafStart( handleTelegrafStart(instance: object, key: string) {
instance: object, this.telegraf.start(instance[key].bind(instance));
key: string,
telegraf: TelegrafProvider,
) {
telegraf.start(instance[key].bind(instance));
} }
handleTelegrafHelp( handleTelegrafHelp(instance: object, key: string) {
instance: object, this.telegraf.help(instance[key].bind(instance));
key: string,
telegraf: TelegrafProvider,
) {
telegraf.help(instance[key].bind(instance));
} }
handleTelegrafSettings( handleTelegrafSettings(instance: object, key: string) {
instance: object,
key: string,
telegraf: TelegrafProvider,
) {
// @ts-ignore // @ts-ignore
telegraf.settings(instance[key].bind(instance)); this.telegraf.settings(instance[key].bind(instance));
} }
handleTelegrafEntity( handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) {
instance: object,
key: string,
telegraf: TelegrafProvider,
metadata: EntityOptions,
) {
// @ts-ignore // @ts-ignore
telegraf.entity(metadata.entity, instance[key].bind(instance)); this.telegraf.entity(metadata.entity, instance[key].bind(instance));
} }
handleTelegrafMention( handleTelegrafMention(
instance: object, instance: object,
key: string, key: string,
telegraf: TelegrafProvider,
metadata: MentionOptions, metadata: MentionOptions,
) { ) {
// @ts-ignore // @ts-ignore
telegraf.mention(metadata.username, instance[key].bind(instance)); this.telegraf.mention(metadata.username, instance[key].bind(instance));
} }
handleTelegrafPhone( handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) {
instance: object,
key: string,
telegraf: TelegrafProvider,
metadata: PhoneOptions,
) {
// @ts-ignore // @ts-ignore
telegraf.phone(metadata.phone, instance[key].bind(instance)); this.telegraf.phone(metadata.phone, instance[key].bind(instance));
} }
handleTelegrafHashtag( handleTelegrafHashtag(
instance: object, instance: object,
key: string, key: string,
telegraf: TelegrafProvider,
metadata: HashtagOptions, metadata: HashtagOptions,
) { ) {
// @ts-ignore // @ts-ignore
telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); this.telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
} }
handleTelegrafCashtag( handleTelegrafCashtag(
instance: object, instance: object,
key: string, key: string,
telegraf: TelegrafProvider,
metadata: CashtagOptions, metadata: CashtagOptions,
) { ) {
// @ts-ignore // @ts-ignore
telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); this.telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
} }
handleTelegrafAction( handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) {
instance: object, this.telegraf.action(metadata.triggers, instance[key].bind(instance));
key: string,
telegraf: TelegrafProvider,
metadata: ActionOptions,
) {
telegraf.action(metadata.triggers, instance[key].bind(instance));
} }
handleTelegrafInlineQuery( handleTelegrafInlineQuery(
instance: object, instance: object,
key: string, key: string,
telegraf: TelegrafProvider,
metadata: InlineQueryOptions, metadata: InlineQueryOptions,
) { ) {
// @ts-ignore // @ts-ignore
telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance)); this.telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));
} }
handleTelegrafGameQuery( handleTelegrafGameQuery(instance: object, key: string) {
instance: object, this.telegraf.gameQuery(instance[key].bind(instance));
key: string,
telegraf: TelegrafProvider,
) {
telegraf.gameQuery(instance[key].bind(instance));
} }
} }