diff --git a/lib/decorators/core/index.ts b/lib/decorators/core/index.ts new file mode 100644 index 0000000..c7875e3 --- /dev/null +++ b/lib/decorators/core/index.ts @@ -0,0 +1,3 @@ +export * from './on.decorator'; +export * from './update.decorator'; +export * from './use.decorator'; diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index c49f9c0..dd680b1 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -1,18 +1,3 @@ -export * from './action.decorator'; -export * from './cashtag.decorator'; -export * from './command.decorator'; -export * from './entity.decorator'; -export * from './game-query.decorator'; -export * from './hashtag.decorator'; -export * from './hears.decorator'; -export * from './help.decorator'; export * from './inject-bot.decorator'; -export * from './inline-query.decorator'; -export * from './mention.decorator'; -export * from './on.decorator'; -export * from './phone.decorator'; -export * from './settings.decorator'; -export * from './start.decorator'; -export * from './update.decorator'; -export * from './update-hooks.decorators'; -export * from './core/use.decorator'; +export * from './core'; +export * from './listeners'; diff --git a/lib/decorators/listeners/command.decorator.ts b/lib/decorators/listeners/command.decorator.ts index ee3de72..683b405 100644 --- a/lib/decorators/listeners/command.decorator.ts +++ b/lib/decorators/listeners/command.decorator.ts @@ -7,7 +7,7 @@ import { ListenerType } from '../../enums/listener-type.enum'; import { TelegrafCommand } from '../../telegraf.types'; export interface CommandOptions { - commands: TelegrafCommand; + command: TelegrafCommand; } /** @@ -15,11 +15,11 @@ export interface CommandOptions { * * @see https://telegraf.js.org/#/?id=command */ -export const Command = (commands: TelegrafCommand): MethodDecorator => { +export const Command = (command: TelegrafCommand): MethodDecorator => { return applyDecorators( SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Command), SetMetadata(TELEGRAF_LISTENER_OPTIONS, { - commands, + command: command, } as CommandOptions), ); }; diff --git a/lib/decorators/listeners/email.decorator.ts b/lib/decorators/listeners/email.decorator.ts new file mode 100644 index 0000000..a0b0b91 --- /dev/null +++ b/lib/decorators/listeners/email.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafEmail } from '../../telegraf.types'; + +export interface EmailOptions { + email: TelegrafEmail; +} + +/** + * Registers middleware for handling messages with email entity. + * + * @see https://telegraf.js.org/#/?id=telegraf-email + */ +export const Email = (email: TelegrafEmail): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Email), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + email, + } as EmailOptions), + ); +}; diff --git a/lib/decorators/listeners/index.ts b/lib/decorators/listeners/index.ts new file mode 100644 index 0000000..2108a90 --- /dev/null +++ b/lib/decorators/listeners/index.ts @@ -0,0 +1,16 @@ +export * from './action.decorator'; +export * from './cashtag.decorator'; +export * from './command.decorator'; +export * from './game-query.decorator'; +export * from './hashtag.decorator'; +export * from './hears.decorator'; +export * from './help.decorator'; +export * from './inline-query.decorator'; +export * from './mention.decorator'; +export * from './phone.decorator'; +export * from './settings.decorator'; +export * from './start.decorator'; +export * from './email.decorator'; +export * from './url.decorator'; +export * from './text-link.decorator'; +export * from './text-mention.decorator'; diff --git a/lib/decorators/listeners/text-link.decorator.ts b/lib/decorators/listeners/text-link.decorator.ts new file mode 100644 index 0000000..f6a484e --- /dev/null +++ b/lib/decorators/listeners/text-link.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafTextLink } from '../../telegraf.types'; + +export interface TextLinkOptions { + link: TelegrafTextLink; +} + +/** + * Registers middleware for handling messages with text_link entity. + * + * @see https://telegraf.js.org/#/?id=telegraf-textlink + */ +export const TetxLink = (link: TelegrafTextLink): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.TextLink), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + link, + } as TextLinkOptions), + ); +}; diff --git a/lib/decorators/listeners/text-mention.decorator.ts b/lib/decorators/listeners/text-mention.decorator.ts new file mode 100644 index 0000000..aea29ce --- /dev/null +++ b/lib/decorators/listeners/text-mention.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafTextMention } from '../../telegraf.types'; + +export interface TextMentionOptions { + mention: TelegrafTextMention; +} + +/** + * Registers middleware for handling messages with text_mention entity. + * + * @see https://telegraf.js.org/#/?id=telegraf-textlink + */ +export const TetxMention = (mention: TelegrafTextMention): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.TextMention), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + mention, + } as TextMentionOptions), + ); +}; diff --git a/lib/decorators/listeners/url.decorator.ts b/lib/decorators/listeners/url.decorator.ts new file mode 100644 index 0000000..e8c1448 --- /dev/null +++ b/lib/decorators/listeners/url.decorator.ts @@ -0,0 +1,25 @@ +import { applyDecorators, SetMetadata } from '@nestjs/common'; +import { + TELEGRAF_LISTENER_OPTIONS, + TELEGRAF_LISTENER_TYPE, +} from '../../telegraf.constants'; +import { ListenerType } from '../../enums/listener-type.enum'; +import { TelegrafUrl } from '../../telegraf.types'; + +export interface UrlOptions { + url: TelegrafUrl; +} + +/** + * Registers middleware for handling messages with url entity. + * + * @see https://telegraf.js.org/#/?id=telegraf-url + */ +export const Url = (url: TelegrafUrl): MethodDecorator => { + return applyDecorators( + SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Url), + SetMetadata(TELEGRAF_LISTENER_OPTIONS, { + url, + } as UrlOptions), + ); +}; diff --git a/lib/decorators/updates.decorator.ts b/lib/decorators/updates.decorator.ts deleted file mode 100644 index b186a21..0000000 --- a/lib/decorators/updates.decorator.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { On } from './core/on.decorator'; - -/** - * New incoming message of any kind — text, photo, sticker, etc. - * @constructor - */ -export const Message = (): MethodDecorator => On('message'); - -/** - * New version of a message that is known to the bot and was edited - * @constructor - */ -export const EditedMessage = (): MethodDecorator => On('edited_message'); - -/** - * New incoming channel post of any kind — text, photo, sticker, etc. - * @constructor - */ -export const ChannelPost = (): MethodDecorator => On('channel_post'); - -/** - * New version of a channel post that is known to the bot and was edited - * @constructor - */ -export const EditedChannelPost = (): MethodDecorator => - On('edited_channel_post'); - -/** - * New incoming inline query - * See this decorator in inline-query.decorator.ts - * @constructor - */ -// export const InlineQuery = (): MethodDecorator => On('inline_query'); - -/** - * The result of an inline query that was chosen by a user and sent to their chat partner. - * @constructor - */ -export const ChosenInlineResult = (): MethodDecorator => - On('chosen_inline_result'); - -/** - * New incoming callback query - * @constructor - */ -export const CallbackQuery = (): MethodDecorator => On('callback_query'); - -/** - * New incoming shipping query. Only for invoices with flexible price - * @constructor - */ -export const ShippingQuery = (): MethodDecorator => On('shipping_query'); - -/** - * New incoming pre-checkout query. Contains full information about checkout - * @constructor - */ -export const PreCheckoutQuery = (): MethodDecorator => On('pre_checkout_query'); - -/** - * New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot - * @constructor - */ -export const Poll = (): MethodDecorator => On('poll'); - -/** - * A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself. - * @constructor - */ -export const PollAnswer = (): MethodDecorator => On('poll_answer'); diff --git a/lib/enums/index.ts b/lib/enums/index.ts new file mode 100644 index 0000000..1f890a3 --- /dev/null +++ b/lib/enums/index.ts @@ -0,0 +1 @@ +export * from './listener-type.enum'; diff --git a/lib/enums/listener-type.enum.ts b/lib/enums/listener-type.enum.ts index 5ebf548..932382f 100644 --- a/lib/enums/listener-type.enum.ts +++ b/lib/enums/listener-type.enum.ts @@ -10,6 +10,10 @@ export enum ListenerType { Phone = 'phone', Hashtag = 'hashtag', Cashtag = 'cashtag', + Email = 'email', + Url = 'url', + TextLink = 'textLink', + TextMention = 'textMention', Action = 'action', InlineQuery = 'inlineQuery', GameQuery = 'gameQuery', diff --git a/lib/interfaces/context.interface.ts b/lib/interfaces/context.interface.ts index d670e24..97001b3 100644 --- a/lib/interfaces/context.interface.ts +++ b/lib/interfaces/context.interface.ts @@ -1,3 +1,4 @@ import { Context as TelegrafContext } from 'telegraf'; +// eslint-disable-next-line @typescript-eslint/no-empty-interface export interface Context extends TelegrafContext {} diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index 13c729d..9f7dc6d 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -1,17 +1,10 @@ import { ModuleMetadata, Type } from '@nestjs/common/interfaces'; -import { - TelegrafOptions, - LaunchPollingOptions, - LaunchWebhookOptions, -} from 'telegraf/typings/telegraf'; +import { TelegrafLaunchOption, TelegrafOption } from '../telegraf.types'; export interface TelegrafModuleOptions { token: string; - options?: TelegrafOptions; - launchOptions?: { - polling?: LaunchPollingOptions; - webhook?: LaunchWebhookOptions; - }; + options?: TelegrafOption; + launchOptions?: TelegrafLaunchOption; } export interface TelegrafOptionsFactory { diff --git a/lib/middleware/decorators/middleware.decorator.ts b/lib/middleware/decorators/middleware.decorator.ts new file mode 100644 index 0000000..29541ec --- /dev/null +++ b/lib/middleware/decorators/middleware.decorator.ts @@ -0,0 +1,4 @@ +import { SetMetadata } from '@nestjs/common'; + +export const Middleware = (): ClassDecorator => + SetMetadata('TELEGRAF_MIDDLEWARE', true); diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index b204d60..504db39 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -1,204 +1,20 @@ -import { Injectable, Type } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { - ActionOptions, - CashtagOptions, - CommandOptions, - EntityOptions, - HashtagOptions, - HearsOptions, - InlineQueryOptions, - MentionOptions, - OnOptions, - PhoneOptions, - UpdateHookOptions, -} from './decorators'; -import { DECORATORS } from './telegraf.constants'; + TELEGRAF_LISTENER_TYPE, + TELEGRAF_MODULE_OPTIONS, +} from './telegraf.constants'; +import { ListenerType } from './enums/listener-type.enum'; @Injectable() export class TelegrafMetadataAccessor { constructor(private readonly reflector: Reflector) {} - isUpdate(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.UPDATE, target); + getListenerType(target: Function): ListenerType | undefined { + return this.reflector.get(TELEGRAF_LISTENER_TYPE, target); } - isUpdateHook(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.UPDATE_HOOK, target); - } - - getUpdateHookMetadata( - target: Type | Function, - ): UpdateHookOptions | undefined { - return this.reflector.get(DECORATORS.UPDATE_HOOK, target); - } - - isTelegrafUse(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.USE, target); - } - - isTelegrafOn(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.ON, target); - } - - getTelegrafOnMetadata(target: Type | Function): OnOptions | undefined { - return this.reflector.get(DECORATORS.ON, target); - } - - isTelegrafHears(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.HEARS, target); - } - - getTelegrafHearsMetadata( - target: Type | Function, - ): HearsOptions | undefined { - return this.reflector.get(DECORATORS.HEARS, target); - } - - isTelegrafCommand(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.COMMAND, target); - } - - getTelegrafCommandMetadata( - target: Type | Function, - ): CommandOptions | undefined { - return this.reflector.get(DECORATORS.COMMAND, target); - } - - isTelegrafStart(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.START, target); - } - - isTelegrafHelp(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.HELP, target); - } - - isTelegrafSettings(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.SETTINGS, target); - } - - isTelegrafEntity(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.ENTITY, target); - } - - getTelegrafEntityMetadata( - target: Type | Function, - ): EntityOptions | undefined { - return this.reflector.get(DECORATORS.ENTITY, target); - } - - isTelegrafMention(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.MENTION, target); - } - - getTelegrafMentionMetadata( - target: Type | Function, - ): MentionOptions | undefined { - return this.reflector.get(DECORATORS.MENTION, target); - } - - isTelegrafPhone(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.PHONE, target); - } - - getTelegrafPhoneMetadata( - target: Type | Function, - ): PhoneOptions | undefined { - return this.reflector.get(DECORATORS.PHONE, target); - } - - isTelegrafHashtag(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.HASHTAG, target); - } - - getTelegrafHashtagMetadata( - target: Type | Function, - ): HashtagOptions | undefined { - return this.reflector.get(DECORATORS.HASHTAG, target); - } - - isTelegrafCashtag(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.CASHTAG, target); - } - - getTelegrafCashtagMetadata( - target: Type | Function, - ): CashtagOptions | undefined { - return this.reflector.get(DECORATORS.CASHTAG, target); - } - - isTelegrafAction(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.ACTION, target); - } - - getTelegrafActionMetadata( - target: Type | Function, - ): ActionOptions | undefined { - return this.reflector.get(DECORATORS.ACTION, target); - } - - isTelegrafInlineQuery(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.INLINE_QUERY, target); - } - - getTelegrafInlineQueryMetadata( - target: Type | Function, - ): InlineQueryOptions | undefined { - return this.reflector.get(DECORATORS.INLINE_QUERY, target); - } - - isTelegrafGameQuery(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.GAME_QUERY, target); + getListenerOptions(target: Function): unknown | undefined { + return this.reflector.get(TELEGRAF_MODULE_OPTIONS, target); } } diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 42aa3c5..da3a6b6 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -1,22 +1,24 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import { DiscoveryService, ModuleRef } from '@nestjs/core'; -import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; import { MetadataScanner } from '@nestjs/core/metadata-scanner'; import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; import { TelegrafProvider } from './telegraf.provider'; import { TELEGRAF_PROVIDER } from './telegraf.constants'; +import { ListenerType } from './enums/listener-type.enum'; import { ActionOptions, CashtagOptions, CommandOptions, - EntityOptions, + EmailOptions, HashtagOptions, HearsOptions, InlineQueryOptions, MentionOptions, OnOptions, PhoneOptions, - UpdateHookOptions, + TextLinkOptions, + TextMentionOptions, + UrlOptions, } from './decorators'; @Injectable() @@ -30,209 +32,129 @@ export class TelegrafExplorer implements OnModuleInit { private telegraf: TelegrafProvider; - onModuleInit() { + onModuleInit(): void { this.telegraf = this.moduleRef.get(TELEGRAF_PROVIDER, { strict: false, }); this.explore(); } - explore() { - /** - * Update providers section is only for decorators under Update decorator - */ - const updateProviders: InstanceWrapper[] = this.discoveryService + explore(): void { + this.discoveryService .getProviders() - .filter((wrapper: InstanceWrapper) => - this.metadataAccessor.isUpdate(wrapper.metatype), - ); + .filter((wrapper) => wrapper.instance) + .forEach((wrapper) => { + const { instance } = wrapper; - updateProviders.forEach((wrapper: InstanceWrapper) => { - const { instance } = wrapper; + const prototype = Object.getPrototypeOf(instance); + this.metadataScanner.scanFromPrototype( + instance, + prototype, + (methodKey: string) => { + this.registerIfListener(instance, methodKey); + }, + ); + }); + } - 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); - } - }, - ); - }); + private registerIfListener( + instance: Record, + methodKey: string, + ): void { + const methodRef = instance[methodKey]; + const middlewareFn = methodRef.bind(instance); - const providers: InstanceWrapper[] = this.discoveryService.getProviders(); + const listenerType = this.metadataAccessor.getListenerType(methodRef); + if (!listenerType) return; - providers.forEach((wrapper: InstanceWrapper) => { - const { instance } = wrapper; + const listenerOptions = this.metadataAccessor.getListenerOptions(methodRef); - if (!instance) { - return; + switch (listenerType) { + case ListenerType.On: { + const { updateTypes } = listenerOptions as OnOptions; + this.telegraf.on(updateTypes, middlewareFn); + break; + } + case ListenerType.Use: { + this.telegraf.use(middlewareFn); + break; + } + case ListenerType.Start: { + this.telegraf.start(middlewareFn); + break; + } + case ListenerType.Help: { + this.telegraf.help(middlewareFn); + break; + } + case ListenerType.Settings: { + this.telegraf.settings(middlewareFn); + break; + } + case ListenerType.Hears: { + const { triggers } = listenerOptions as HearsOptions; + this.telegraf.hears(triggers, middlewareFn); + break; + } + case ListenerType.Command: { + const { command } = listenerOptions as CommandOptions; + this.telegraf.command(command, middlewareFn); + break; + } + case ListenerType.Action: { + const { triggers } = listenerOptions as ActionOptions; + this.telegraf.action(triggers, middlewareFn); + break; + } + case ListenerType.Mention: { + const { mention } = listenerOptions as MentionOptions; + this.telegraf.mention(mention, middlewareFn); + break; + } + case ListenerType.Phone: { + const { phone } = listenerOptions as PhoneOptions; + this.telegraf.phone(phone, middlewareFn); + break; + } + case ListenerType.Hashtag: { + const { hashtag } = listenerOptions as HashtagOptions; + this.telegraf.hashtag(hashtag, middlewareFn); + break; + } + case ListenerType.Cashtag: { + const { cashtag } = listenerOptions as CashtagOptions; + this.telegraf.cashtag(cashtag, middlewareFn); + break; + } + case ListenerType.Email: { + const { email } = listenerOptions as EmailOptions; + this.telegraf.email(email, middlewareFn); + break; + } + case ListenerType.Url: { + const { url } = listenerOptions as UrlOptions; + this.telegraf.url(url, middlewareFn); + break; + } + case ListenerType.TextLink: { + const { link } = listenerOptions as TextLinkOptions; + this.telegraf.textLink(link, middlewareFn); + break; + } + case ListenerType.TextMention: { + const { mention } = listenerOptions as TextMentionOptions; + this.telegraf.textMention(mention, middlewareFn); + break; + } + case ListenerType.InlineQuery: { + const { triggers } = listenerOptions as InlineQueryOptions; + this.telegraf.inlineQuery(triggers, middlewareFn); + break; + } + case ListenerType.GameQuery: { + this.telegraf.gameQuery(middlewareFn); + break; } - - this.metadataScanner.scanFromPrototype( - instance, - Object.getPrototypeOf(instance), - (key: string) => { - if (this.metadataAccessor.isTelegrafUse(instance[key])) { - this.handleTelegrafUse(instance, key); - } else if (this.metadataAccessor.isTelegrafOn(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafOnMetadata( - instance[key], - ); - this.handleTelegrafOn(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafHears(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafHearsMetadata( - instance[key], - ); - this.handleTelegrafHears(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafCommand(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafCommandMetadata( - instance[key], - ); - this.handleTelegrafCommand(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafStart(instance[key])) { - this.handleTelegrafStart(instance, key); - } else if (this.metadataAccessor.isTelegrafHelp(instance[key])) { - this.handleTelegrafHelp(instance, key); - } else if (this.metadataAccessor.isTelegrafSettings(instance[key])) { - this.handleTelegrafSettings(instance, key); - } else if (this.metadataAccessor.isTelegrafEntity(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafEntityMetadata( - instance[key], - ); - this.handleTelegrafEntity(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafMention(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafMentionMetadata( - instance[key], - ); - this.handleTelegrafMention(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafPhone(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafPhoneMetadata( - instance[key], - ); - this.handleTelegrafPhone(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafHashtagMetadata( - instance[key], - ); - this.handleTelegrafHashtag(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafCashtagMetadata( - instance[key], - ); - this.handleTelegrafCashtag(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafAction(instance[key])) { - const metadata = this.metadataAccessor.getTelegrafActionMetadata( - instance[key], - ); - this.handleTelegrafAction(instance, key, metadata); - } else if ( - this.metadataAccessor.isTelegrafInlineQuery(instance[key]) - ) { - const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata( - instance[key], - ); - this.handleTelegrafInlineQuery(instance, key, metadata); - } else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) { - this.handleTelegrafGameQuery(instance, key); - } - }, - ); - }); - } - - handleUpdateHook(instance: object, key: string, metadata: UpdateHookOptions) { - this.telegraf.on(metadata.updateType, instance[key].bind(instance)); - } - - handleTelegrafUse(instance: object, key: string) { - this.telegraf.use(instance[key].bind(instance)); - } - - handleTelegrafOn(instance: object, key: string, metadata: OnOptions) { - this.telegraf.on(metadata.updateTypes, instance[key].bind(instance)); - } - - handleTelegrafHears(instance: object, key: string, metadata: HearsOptions) { - this.telegraf.hears(metadata.triggers, instance[key].bind(instance)); - } - - handleTelegrafCommand( - instance: object, - key: string, - metadata: CommandOptions, - ) { - this.telegraf.command(metadata.commands, instance[key].bind(instance)); - } - - handleTelegrafStart(instance: object, key: string) { - this.telegraf.start(instance[key].bind(instance)); - } - - handleTelegrafHelp(instance: object, key: string) { - this.telegraf.help(instance[key].bind(instance)); - } - - handleTelegrafSettings(instance: object, key: string) { - this.telegraf.settings(instance[key].bind(instance)); - } - - handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) { - this.telegraf.entity(metadata.entity, instance[key].bind(instance)); - } - - handleTelegrafMention( - instance: object, - key: string, - metadata: MentionOptions, - ) { - this.telegraf.mention(metadata.username, instance[key].bind(instance)); - } - - handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) { - this.telegraf.phone(metadata.phone, instance[key].bind(instance)); - } - - handleTelegrafHashtag( - instance: object, - key: string, - metadata: HashtagOptions, - ) { - this.telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); - } - - handleTelegrafCashtag( - instance: object, - key: string, - metadata: CashtagOptions, - ) { - this.telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); - } - - handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) { - this.telegraf.action(metadata.triggers, instance[key].bind(instance)); - } - - handleTelegrafInlineQuery( - instance: object, - key: string, - metadata: InlineQueryOptions, - ) { - if (metadata.triggers) { - this.telegraf.inlineQuery( - metadata.triggers, - instance[key].bind(instance), - ); - } else { - this.telegraf.on(metadata.updateType, instance[key].bind(instance)); } } - - handleTelegrafGameQuery(instance: object, key: string) { - this.telegraf.gameQuery(instance[key].bind(instance)); - } } diff --git a/lib/telegraf.types.ts b/lib/telegraf.types.ts index f46c261..e2b5e52 100644 --- a/lib/telegraf.types.ts +++ b/lib/telegraf.types.ts @@ -1,13 +1,22 @@ -import { Context, Composer } from 'telegraf'; +import { Composer, Telegraf } from 'telegraf'; +import { Context } from './interfaces'; export type TelegrafActionTriggers = Parameters['action']>[0]; export type TelegrafHearsTriggers = Parameters['hears']>[0]; export type TelegrafInlineQueryTriggers = Parameters< Composer['inlineQuery'] >[0]; +export type TelegrafEmail = Parameters['email']>[0]; +export type TelegrafUrl = Parameters['url']>[0]; +export type TelegrafTextLink = Parameters['textLink']>[0]; +export type TelegrafTextMention = Parameters< + Composer['textMention'] +>[0]; export type TelegrafCashtag = Parameters['cashtag']>[0]; export type TelegrafHashtag = Parameters['hashtag']>[0]; export type TelegrafCommand = Parameters['command']>[0]; export type TelegrafMention = Parameters['mention']>[0]; export type TelegrafPhone = Parameters['phone']>[0]; export type TelegrafUpdateType = Parameters['on']>[0]; +export type TelegrafOption = ConstructorParameters[1]; +export type TelegrafLaunchOption = Parameters[0];