mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 15:04:38 +03:00
feat(wip): rewrite explorer logic & update launch options & add miss decorators
This commit is contained in:
parent
b394f5274b
commit
61c6c07482
3
lib/decorators/core/index.ts
Normal file
3
lib/decorators/core/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './on.decorator';
|
||||
export * from './update.decorator';
|
||||
export * from './use.decorator';
|
@ -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';
|
||||
|
@ -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),
|
||||
);
|
||||
};
|
||||
|
25
lib/decorators/listeners/email.decorator.ts
Normal file
25
lib/decorators/listeners/email.decorator.ts
Normal file
@ -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),
|
||||
);
|
||||
};
|
16
lib/decorators/listeners/index.ts
Normal file
16
lib/decorators/listeners/index.ts
Normal file
@ -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';
|
25
lib/decorators/listeners/text-link.decorator.ts
Normal file
25
lib/decorators/listeners/text-link.decorator.ts
Normal file
@ -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),
|
||||
);
|
||||
};
|
25
lib/decorators/listeners/text-mention.decorator.ts
Normal file
25
lib/decorators/listeners/text-mention.decorator.ts
Normal file
@ -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),
|
||||
);
|
||||
};
|
25
lib/decorators/listeners/url.decorator.ts
Normal file
25
lib/decorators/listeners/url.decorator.ts
Normal file
@ -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),
|
||||
);
|
||||
};
|
@ -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');
|
1
lib/enums/index.ts
Normal file
1
lib/enums/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './listener-type.enum';
|
@ -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',
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { Context as TelegrafContext } from 'telegraf';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface Context extends TelegrafContext {}
|
||||
|
@ -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 {
|
||||
|
4
lib/middleware/decorators/middleware.decorator.ts
Normal file
4
lib/middleware/decorators/middleware.decorator.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
|
||||
export const Middleware = (): ClassDecorator =>
|
||||
SetMetadata('TELEGRAF_MIDDLEWARE', true);
|
@ -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<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.UPDATE, target);
|
||||
getListenerType(target: Function): ListenerType | undefined {
|
||||
return this.reflector.get<ListenerType>(TELEGRAF_LISTENER_TYPE, 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 {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.USE, target);
|
||||
}
|
||||
|
||||
isTelegrafOn(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.ON, target);
|
||||
}
|
||||
|
||||
getTelegrafOnMetadata(target: Type<any> | Function): OnOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.ON, target);
|
||||
}
|
||||
|
||||
isTelegrafHears(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.HEARS, target);
|
||||
}
|
||||
|
||||
getTelegrafHearsMetadata(
|
||||
target: Type<any> | Function,
|
||||
): HearsOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.HEARS, target);
|
||||
}
|
||||
|
||||
isTelegrafCommand(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.COMMAND, target);
|
||||
}
|
||||
|
||||
getTelegrafCommandMetadata(
|
||||
target: Type<any> | Function,
|
||||
): CommandOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.COMMAND, target);
|
||||
}
|
||||
|
||||
isTelegrafStart(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.START, target);
|
||||
}
|
||||
|
||||
isTelegrafHelp(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.HELP, target);
|
||||
}
|
||||
|
||||
isTelegrafSettings(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.SETTINGS, target);
|
||||
}
|
||||
|
||||
isTelegrafEntity(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.ENTITY, target);
|
||||
}
|
||||
|
||||
getTelegrafEntityMetadata(
|
||||
target: Type<any> | Function,
|
||||
): EntityOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.ENTITY, target);
|
||||
}
|
||||
|
||||
isTelegrafMention(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.MENTION, target);
|
||||
}
|
||||
|
||||
getTelegrafMentionMetadata(
|
||||
target: Type<any> | Function,
|
||||
): MentionOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.MENTION, target);
|
||||
}
|
||||
|
||||
isTelegrafPhone(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.PHONE, target);
|
||||
}
|
||||
|
||||
getTelegrafPhoneMetadata(
|
||||
target: Type<any> | Function,
|
||||
): PhoneOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.PHONE, target);
|
||||
}
|
||||
|
||||
isTelegrafHashtag(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.HASHTAG, target);
|
||||
}
|
||||
|
||||
getTelegrafHashtagMetadata(
|
||||
target: Type<any> | Function,
|
||||
): HashtagOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.HASHTAG, target);
|
||||
}
|
||||
|
||||
isTelegrafCashtag(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.CASHTAG, target);
|
||||
}
|
||||
|
||||
getTelegrafCashtagMetadata(
|
||||
target: Type<any> | Function,
|
||||
): CashtagOptions | undefined {
|
||||
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,
|
||||
): ActionOptions | undefined {
|
||||
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,
|
||||
): InlineQueryOptions | undefined {
|
||||
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);
|
||||
getListenerOptions<T>(target: Function): unknown | undefined {
|
||||
return this.reflector.get<unknown>(TELEGRAF_MODULE_OPTIONS, target);
|
||||
}
|
||||
}
|
||||
|
@ -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<TelegrafProvider>(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<string, Function>,
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,22 @@
|
||||
import { Context, Composer } from 'telegraf';
|
||||
import { Composer, Telegraf } from 'telegraf';
|
||||
import { Context } from './interfaces';
|
||||
|
||||
export type TelegrafActionTriggers = Parameters<Composer<Context>['action']>[0];
|
||||
export type TelegrafHearsTriggers = Parameters<Composer<Context>['hears']>[0];
|
||||
export type TelegrafInlineQueryTriggers = Parameters<
|
||||
Composer<Context>['inlineQuery']
|
||||
>[0];
|
||||
export type TelegrafEmail = Parameters<Composer<Context>['email']>[0];
|
||||
export type TelegrafUrl = Parameters<Composer<Context>['url']>[0];
|
||||
export type TelegrafTextLink = Parameters<Composer<Context>['textLink']>[0];
|
||||
export type TelegrafTextMention = Parameters<
|
||||
Composer<Context>['textMention']
|
||||
>[0];
|
||||
export type TelegrafCashtag = Parameters<Composer<Context>['cashtag']>[0];
|
||||
export type TelegrafHashtag = Parameters<Composer<Context>['hashtag']>[0];
|
||||
export type TelegrafCommand = Parameters<Composer<Context>['command']>[0];
|
||||
export type TelegrafMention = Parameters<Composer<Context>['mention']>[0];
|
||||
export type TelegrafPhone = Parameters<Composer<Context>['phone']>[0];
|
||||
export type TelegrafUpdateType = Parameters<Composer<Context>['on']>[0];
|
||||
export type TelegrafOption = ConstructorParameters<typeof Telegraf>[1];
|
||||
export type TelegrafLaunchOption = Parameters<Telegraf['launch']>[0];
|
||||
|
Loading…
Reference in New Issue
Block a user