mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-25 15:34: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 './inject-bot.decorator';
|
||||||
export * from './inline-query.decorator';
|
export * from './core';
|
||||||
export * from './mention.decorator';
|
export * from './listeners';
|
||||||
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';
|
|
||||||
|
@ -7,7 +7,7 @@ import { ListenerType } from '../../enums/listener-type.enum';
|
|||||||
import { TelegrafCommand } from '../../telegraf.types';
|
import { TelegrafCommand } from '../../telegraf.types';
|
||||||
|
|
||||||
export interface CommandOptions {
|
export interface CommandOptions {
|
||||||
commands: TelegrafCommand;
|
command: TelegrafCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -15,11 +15,11 @@ export interface CommandOptions {
|
|||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=command
|
* @see https://telegraf.js.org/#/?id=command
|
||||||
*/
|
*/
|
||||||
export const Command = (commands: TelegrafCommand): MethodDecorator => {
|
export const Command = (command: TelegrafCommand): MethodDecorator => {
|
||||||
return applyDecorators(
|
return applyDecorators(
|
||||||
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Command),
|
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Command),
|
||||||
SetMetadata(TELEGRAF_LISTENER_OPTIONS, {
|
SetMetadata(TELEGRAF_LISTENER_OPTIONS, {
|
||||||
commands,
|
command: command,
|
||||||
} as CommandOptions),
|
} 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',
|
Phone = 'phone',
|
||||||
Hashtag = 'hashtag',
|
Hashtag = 'hashtag',
|
||||||
Cashtag = 'cashtag',
|
Cashtag = 'cashtag',
|
||||||
|
Email = 'email',
|
||||||
|
Url = 'url',
|
||||||
|
TextLink = 'textLink',
|
||||||
|
TextMention = 'textMention',
|
||||||
Action = 'action',
|
Action = 'action',
|
||||||
InlineQuery = 'inlineQuery',
|
InlineQuery = 'inlineQuery',
|
||||||
GameQuery = 'gameQuery',
|
GameQuery = 'gameQuery',
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
import { Context as TelegrafContext } from 'telegraf';
|
import { Context as TelegrafContext } from 'telegraf';
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||||
export interface Context extends TelegrafContext {}
|
export interface Context extends TelegrafContext {}
|
||||||
|
@ -1,17 +1,10 @@
|
|||||||
import { ModuleMetadata, Type } from '@nestjs/common/interfaces';
|
import { ModuleMetadata, Type } from '@nestjs/common/interfaces';
|
||||||
import {
|
import { TelegrafLaunchOption, TelegrafOption } from '../telegraf.types';
|
||||||
TelegrafOptions,
|
|
||||||
LaunchPollingOptions,
|
|
||||||
LaunchWebhookOptions,
|
|
||||||
} from 'telegraf/typings/telegraf';
|
|
||||||
|
|
||||||
export interface TelegrafModuleOptions {
|
export interface TelegrafModuleOptions {
|
||||||
token: string;
|
token: string;
|
||||||
options?: TelegrafOptions;
|
options?: TelegrafOption;
|
||||||
launchOptions?: {
|
launchOptions?: TelegrafLaunchOption;
|
||||||
polling?: LaunchPollingOptions;
|
|
||||||
webhook?: LaunchWebhookOptions;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TelegrafOptionsFactory {
|
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 { Reflector } from '@nestjs/core';
|
||||||
import {
|
import {
|
||||||
ActionOptions,
|
TELEGRAF_LISTENER_TYPE,
|
||||||
CashtagOptions,
|
TELEGRAF_MODULE_OPTIONS,
|
||||||
CommandOptions,
|
} from './telegraf.constants';
|
||||||
EntityOptions,
|
import { ListenerType } from './enums/listener-type.enum';
|
||||||
HashtagOptions,
|
|
||||||
HearsOptions,
|
|
||||||
InlineQueryOptions,
|
|
||||||
MentionOptions,
|
|
||||||
OnOptions,
|
|
||||||
PhoneOptions,
|
|
||||||
UpdateHookOptions,
|
|
||||||
} from './decorators';
|
|
||||||
import { DECORATORS } from './telegraf.constants';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegrafMetadataAccessor {
|
export class TelegrafMetadataAccessor {
|
||||||
constructor(private readonly reflector: Reflector) {}
|
constructor(private readonly reflector: Reflector) {}
|
||||||
|
|
||||||
isUpdate(target: Type<any> | Function): boolean {
|
getListenerType(target: Function): ListenerType | undefined {
|
||||||
if (!target) {
|
return this.reflector.get<ListenerType>(TELEGRAF_LISTENER_TYPE, target);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !!this.reflector.get(DECORATORS.UPDATE, target);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isUpdateHook(target: Type<any> | Function): boolean {
|
getListenerOptions<T>(target: Function): unknown | undefined {
|
||||||
if (!target) {
|
return this.reflector.get<unknown>(TELEGRAF_MODULE_OPTIONS, 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,24 @@
|
|||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
import { DiscoveryService, ModuleRef } from '@nestjs/core';
|
import { DiscoveryService, ModuleRef } from '@nestjs/core';
|
||||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
||||||
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
||||||
import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
|
import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
|
||||||
import { TelegrafProvider } from './telegraf.provider';
|
import { TelegrafProvider } from './telegraf.provider';
|
||||||
import { TELEGRAF_PROVIDER } from './telegraf.constants';
|
import { TELEGRAF_PROVIDER } from './telegraf.constants';
|
||||||
|
import { ListenerType } from './enums/listener-type.enum';
|
||||||
import {
|
import {
|
||||||
ActionOptions,
|
ActionOptions,
|
||||||
CashtagOptions,
|
CashtagOptions,
|
||||||
CommandOptions,
|
CommandOptions,
|
||||||
EntityOptions,
|
EmailOptions,
|
||||||
HashtagOptions,
|
HashtagOptions,
|
||||||
HearsOptions,
|
HearsOptions,
|
||||||
InlineQueryOptions,
|
InlineQueryOptions,
|
||||||
MentionOptions,
|
MentionOptions,
|
||||||
OnOptions,
|
OnOptions,
|
||||||
PhoneOptions,
|
PhoneOptions,
|
||||||
UpdateHookOptions,
|
TextLinkOptions,
|
||||||
|
TextMentionOptions,
|
||||||
|
UrlOptions,
|
||||||
} from './decorators';
|
} from './decorators';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -30,209 +32,129 @@ export class TelegrafExplorer implements OnModuleInit {
|
|||||||
|
|
||||||
private telegraf: TelegrafProvider;
|
private telegraf: TelegrafProvider;
|
||||||
|
|
||||||
onModuleInit() {
|
onModuleInit(): void {
|
||||||
this.telegraf = this.moduleRef.get<TelegrafProvider>(TELEGRAF_PROVIDER, {
|
this.telegraf = this.moduleRef.get<TelegrafProvider>(TELEGRAF_PROVIDER, {
|
||||||
strict: false,
|
strict: false,
|
||||||
});
|
});
|
||||||
this.explore();
|
this.explore();
|
||||||
}
|
}
|
||||||
|
|
||||||
explore() {
|
explore(): void {
|
||||||
/**
|
this.discoveryService
|
||||||
* Update providers section is only for decorators under Update decorator
|
|
||||||
*/
|
|
||||||
const updateProviders: InstanceWrapper[] = this.discoveryService
|
|
||||||
.getProviders()
|
.getProviders()
|
||||||
.filter((wrapper: InstanceWrapper) =>
|
.filter((wrapper) => wrapper.instance)
|
||||||
this.metadataAccessor.isUpdate(wrapper.metatype),
|
.forEach((wrapper) => {
|
||||||
);
|
|
||||||
|
|
||||||
updateProviders.forEach((wrapper: InstanceWrapper) => {
|
|
||||||
const { instance } = wrapper;
|
const { instance } = wrapper;
|
||||||
|
|
||||||
|
const prototype = Object.getPrototypeOf(instance);
|
||||||
this.metadataScanner.scanFromPrototype(
|
this.metadataScanner.scanFromPrototype(
|
||||||
instance,
|
instance,
|
||||||
Object.getPrototypeOf(instance),
|
prototype,
|
||||||
(key: string) => {
|
(methodKey: string) => {
|
||||||
if (this.metadataAccessor.isUpdateHook(instance[key])) {
|
this.registerIfListener(instance, methodKey);
|
||||||
const metadata = this.metadataAccessor.getUpdateHookMetadata(
|
|
||||||
instance[key],
|
|
||||||
);
|
|
||||||
this.handleUpdateHook(instance, key, metadata);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
|
|
||||||
|
|
||||||
providers.forEach((wrapper: InstanceWrapper) => {
|
|
||||||
const { instance } = wrapper;
|
|
||||||
|
|
||||||
if (!instance) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
private registerIfListener(
|
||||||
this.telegraf.on(metadata.updateType, instance[key].bind(instance));
|
instance: Record<string, Function>,
|
||||||
}
|
methodKey: string,
|
||||||
|
): void {
|
||||||
|
const methodRef = instance[methodKey];
|
||||||
|
const middlewareFn = methodRef.bind(instance);
|
||||||
|
|
||||||
handleTelegrafUse(instance: object, key: string) {
|
const listenerType = this.metadataAccessor.getListenerType(methodRef);
|
||||||
this.telegraf.use(instance[key].bind(instance));
|
if (!listenerType) return;
|
||||||
}
|
|
||||||
|
|
||||||
handleTelegrafOn(instance: object, key: string, metadata: OnOptions) {
|
const listenerOptions = this.metadataAccessor.getListenerOptions(methodRef);
|
||||||
this.telegraf.on(metadata.updateTypes, instance[key].bind(instance));
|
|
||||||
}
|
|
||||||
|
|
||||||
handleTelegrafHears(instance: object, key: string, metadata: HearsOptions) {
|
switch (listenerType) {
|
||||||
this.telegraf.hears(metadata.triggers, instance[key].bind(instance));
|
case ListenerType.On: {
|
||||||
|
const { updateTypes } = listenerOptions as OnOptions;
|
||||||
|
this.telegraf.on(updateTypes, middlewareFn);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Use: {
|
||||||
handleTelegrafCommand(
|
this.telegraf.use(middlewareFn);
|
||||||
instance: object,
|
break;
|
||||||
key: string,
|
|
||||||
metadata: CommandOptions,
|
|
||||||
) {
|
|
||||||
this.telegraf.command(metadata.commands, instance[key].bind(instance));
|
|
||||||
}
|
}
|
||||||
|
case ListenerType.Start: {
|
||||||
handleTelegrafStart(instance: object, key: string) {
|
this.telegraf.start(middlewareFn);
|
||||||
this.telegraf.start(instance[key].bind(instance));
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Help: {
|
||||||
handleTelegrafHelp(instance: object, key: string) {
|
this.telegraf.help(middlewareFn);
|
||||||
this.telegraf.help(instance[key].bind(instance));
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Settings: {
|
||||||
handleTelegrafSettings(instance: object, key: string) {
|
this.telegraf.settings(middlewareFn);
|
||||||
this.telegraf.settings(instance[key].bind(instance));
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Hears: {
|
||||||
handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) {
|
const { triggers } = listenerOptions as HearsOptions;
|
||||||
this.telegraf.entity(metadata.entity, instance[key].bind(instance));
|
this.telegraf.hears(triggers, middlewareFn);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Command: {
|
||||||
handleTelegrafMention(
|
const { command } = listenerOptions as CommandOptions;
|
||||||
instance: object,
|
this.telegraf.command(command, middlewareFn);
|
||||||
key: string,
|
break;
|
||||||
metadata: MentionOptions,
|
|
||||||
) {
|
|
||||||
this.telegraf.mention(metadata.username, instance[key].bind(instance));
|
|
||||||
}
|
}
|
||||||
|
case ListenerType.Action: {
|
||||||
handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) {
|
const { triggers } = listenerOptions as ActionOptions;
|
||||||
this.telegraf.phone(metadata.phone, instance[key].bind(instance));
|
this.telegraf.action(triggers, middlewareFn);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Mention: {
|
||||||
handleTelegrafHashtag(
|
const { mention } = listenerOptions as MentionOptions;
|
||||||
instance: object,
|
this.telegraf.mention(mention, middlewareFn);
|
||||||
key: string,
|
break;
|
||||||
metadata: HashtagOptions,
|
|
||||||
) {
|
|
||||||
this.telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
|
|
||||||
}
|
}
|
||||||
|
case ListenerType.Phone: {
|
||||||
handleTelegrafCashtag(
|
const { phone } = listenerOptions as PhoneOptions;
|
||||||
instance: object,
|
this.telegraf.phone(phone, middlewareFn);
|
||||||
key: string,
|
break;
|
||||||
metadata: CashtagOptions,
|
|
||||||
) {
|
|
||||||
this.telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
|
|
||||||
}
|
}
|
||||||
|
case ListenerType.Hashtag: {
|
||||||
handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) {
|
const { hashtag } = listenerOptions as HashtagOptions;
|
||||||
this.telegraf.action(metadata.triggers, instance[key].bind(instance));
|
this.telegraf.hashtag(hashtag, middlewareFn);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case ListenerType.Cashtag: {
|
||||||
handleTelegrafInlineQuery(
|
const { cashtag } = listenerOptions as CashtagOptions;
|
||||||
instance: object,
|
this.telegraf.cashtag(cashtag, middlewareFn);
|
||||||
key: string,
|
break;
|
||||||
metadata: InlineQueryOptions,
|
}
|
||||||
) {
|
case ListenerType.Email: {
|
||||||
if (metadata.triggers) {
|
const { email } = listenerOptions as EmailOptions;
|
||||||
this.telegraf.inlineQuery(
|
this.telegraf.email(email, middlewareFn);
|
||||||
metadata.triggers,
|
break;
|
||||||
instance[key].bind(instance),
|
}
|
||||||
);
|
case ListenerType.Url: {
|
||||||
} else {
|
const { url } = listenerOptions as UrlOptions;
|
||||||
this.telegraf.on(metadata.updateType, instance[key].bind(instance));
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 TelegrafActionTriggers = Parameters<Composer<Context>['action']>[0];
|
||||||
export type TelegrafHearsTriggers = Parameters<Composer<Context>['hears']>[0];
|
export type TelegrafHearsTriggers = Parameters<Composer<Context>['hears']>[0];
|
||||||
export type TelegrafInlineQueryTriggers = Parameters<
|
export type TelegrafInlineQueryTriggers = Parameters<
|
||||||
Composer<Context>['inlineQuery']
|
Composer<Context>['inlineQuery']
|
||||||
>[0];
|
>[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 TelegrafCashtag = Parameters<Composer<Context>['cashtag']>[0];
|
||||||
export type TelegrafHashtag = Parameters<Composer<Context>['hashtag']>[0];
|
export type TelegrafHashtag = Parameters<Composer<Context>['hashtag']>[0];
|
||||||
export type TelegrafCommand = Parameters<Composer<Context>['command']>[0];
|
export type TelegrafCommand = Parameters<Composer<Context>['command']>[0];
|
||||||
export type TelegrafMention = Parameters<Composer<Context>['mention']>[0];
|
export type TelegrafMention = Parameters<Composer<Context>['mention']>[0];
|
||||||
export type TelegrafPhone = Parameters<Composer<Context>['phone']>[0];
|
export type TelegrafPhone = Parameters<Composer<Context>['phone']>[0];
|
||||||
export type TelegrafUpdateType = Parameters<Composer<Context>['on']>[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