mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-11 14:48:10 +03:00
fix(): missing decorators & refactor
This commit is contained in:
parent
90cb548551
commit
f13eebe685
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling callback_data actions with regular expressions.
|
* Registers middleware for handling callback_data actions with regular expressions.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=action
|
* @see https://telegraf.js.org/#/?id=action
|
||||||
*/
|
*/
|
||||||
export const Action = createUpdateListenerDecorator('action');
|
export const Action = createListenerDecorator('action');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cashtag handling.
|
* Cashtag handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=cashtag
|
* @see https://telegraf.js.org/#/?id=cashtag
|
||||||
*/
|
*/
|
||||||
export const Cashtag = createUpdateListenerDecorator('cashtag');
|
export const Cashtag = createMissedListenerDecorator<[string | string[]]>('cashtag');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command handling.
|
* Command handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=command
|
* @see https://telegraf.js.org/#/?id=command
|
||||||
*/
|
*/
|
||||||
export const Command = createUpdateListenerDecorator('command');
|
export const Command = createListenerDecorator('command');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with email entity.
|
* Registers middleware for handling messages with email entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-email
|
* @see https://telegraf.js.org/#/?id=telegraf-email
|
||||||
*/
|
*/
|
||||||
export const Email = createUpdateListenerDecorator('email');
|
export const Email = createMissedListenerDecorator<[string | string[]]>('email');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling callback_data actions with game query.
|
* Registers middleware for handling callback_data actions with game query.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||||
*/
|
*/
|
||||||
export const GameQuery = createUpdateListenerDecorator('gameQuery');
|
export const GameQuery = createListenerDecorator('gameQuery');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashtag handling.
|
* Hashtag handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=hashtag
|
* @see https://telegraf.js.org/#/?id=hashtag
|
||||||
*/
|
*/
|
||||||
export const Hashtag = createUpdateListenerDecorator('hashtag');
|
export const Hashtag = createMissedListenerDecorator<[string | string[]]>('hashtag');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling text messages.
|
* Registers middleware for handling text messages.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=hears
|
* @see https://telegraf.js.org/#/?id=hears
|
||||||
*/
|
*/
|
||||||
export const Hears = createUpdateListenerDecorator('hears');
|
export const Hears = createListenerDecorator('hears');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for /help command.
|
* Handler for /help command.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=help
|
* @see https://telegraf.js.org/#/?id=help
|
||||||
*/
|
*/
|
||||||
export const Help = createUpdateListenerDecorator('help');
|
export const Help = createListenerDecorator('help');
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
import { HearsTriggers } from 'telegraf/typings/composer';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling inline_query actions with regular expressions.
|
* Registers middleware for handling inline_query actions with regular expressions.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||||
*/
|
*/
|
||||||
export const InlineQuery = createUpdateListenerDecorator('inlineQuery');
|
export const InlineQuery = createMissedListenerDecorator<[HearsTriggers<unknown>]>('inlineQuery');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mention handling.
|
* Mention handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=mention
|
* @see https://telegraf.js.org/#/?id=mention
|
||||||
*/
|
*/
|
||||||
export const Mention = createUpdateListenerDecorator('mention');
|
export const Mention = createMissedListenerDecorator<[string | string[]]>('mention');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for provided update type.
|
* Registers middleware for provided update type.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=on
|
* @see https://telegraf.js.org/#/?id=on
|
||||||
*/
|
*/
|
||||||
export const On = createUpdateListenerDecorator('on');
|
export const On = createListenerDecorator('on');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phone number handling.
|
* Phone number handling.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=phone
|
* @see https://telegraf.js.org/#/?id=phone
|
||||||
*/
|
*/
|
||||||
export const Phone = createUpdateListenerDecorator('phone');
|
export const Phone = createMissedListenerDecorator<[string | string[]]>('phone');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for /settings command.
|
* Handler for /settings command.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=settings
|
* @see https://telegraf.js.org/#/?id=settings
|
||||||
*/
|
*/
|
||||||
export const Settings = createUpdateListenerDecorator('settings');
|
export const Settings = createMissedListenerDecorator<[]>('settings');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for /start command.
|
* Handler for /start command.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=start
|
* @see https://telegraf.js.org/#/?id=start
|
||||||
*/
|
*/
|
||||||
export const Start = createUpdateListenerDecorator('start');
|
export const Start = createListenerDecorator('start');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with text_link entity.
|
* Registers middleware for handling messages with text_link entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||||
*/
|
*/
|
||||||
export const TextLink = createUpdateListenerDecorator('textLink');
|
export const TextLink = createMissedListenerDecorator<[string | string[]]>('textLink');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with text_mention entity.
|
* Registers middleware for handling messages with text_mention entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||||
*/
|
*/
|
||||||
export const TextMention = createUpdateListenerDecorator('textMention');
|
export const TextMention = createMissedListenerDecorator<[string | string[]]>('textMention');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createMissedListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers middleware for handling messages with url entity.
|
* Registers middleware for handling messages with url entity.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=telegraf-url
|
* @see https://telegraf.js.org/#/?id=telegraf-url
|
||||||
*/
|
*/
|
||||||
export const Url = createUpdateListenerDecorator('url');
|
export const Url = createMissedListenerDecorator<[string | string[]]>('url');
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { createUpdateListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a middleware.
|
* Registers a middleware.
|
||||||
*
|
*
|
||||||
* @see https://telegraf.js.org/#/?id=use
|
* @see https://telegraf.js.org/#/?id=use
|
||||||
*/
|
*/
|
||||||
export const Use = createUpdateListenerDecorator('use');
|
export const Use = createListenerDecorator('use');
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
import { createSceneListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
export const SceneEnter = createSceneListenerDecorator('enter');
|
export const SceneEnter = createListenerDecorator('enter');
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
import { createSceneListenerDecorator } from '../../helpers';
|
import { createListenerDecorator } from '../../utils';
|
||||||
|
|
||||||
export const SceneLeave = createSceneListenerDecorator('leave');
|
export const SceneLeave = createListenerDecorator('leave');
|
||||||
|
13
lib/index.ts
13
lib/index.ts
@ -1,15 +1,6 @@
|
|||||||
export * as Composer from 'telegraf/composer';
|
|
||||||
export * as Markup from 'telegraf/markup';
|
|
||||||
export * as BaseScene from 'telegraf/scenes/base';
|
|
||||||
export * as session from 'telegraf/session';
|
|
||||||
export * as Stage from 'telegraf/stage';
|
|
||||||
export * as WizardScene from 'telegraf/scenes/wizard';
|
|
||||||
export * as Extra from 'telegraf/extra';
|
|
||||||
|
|
||||||
export * from './decorators';
|
export * from './decorators';
|
||||||
export * from './interfaces';
|
export * from './interfaces';
|
||||||
export * from './helpers';
|
|
||||||
export * from './utils';
|
export * from './utils';
|
||||||
export * from './telegraf.module';
|
|
||||||
export * from './types';
|
export * from './types';
|
||||||
export { Telegraf } from 'telegraf';
|
export * from './telegraf.constants';
|
||||||
|
export * from './telegraf.module';
|
||||||
|
@ -1,3 +1,2 @@
|
|||||||
export * from './telegraf-options.interface';
|
export * from './telegraf-options.interface';
|
||||||
export * from './listener-metadata.interface';
|
export * from './listener-metadata.interface';
|
||||||
export * from './update-metadata.interface';
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||||
import { Module } from '@nestjs/core/injector/module';
|
import { Module } from '@nestjs/core/injector/module';
|
||||||
import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash';
|
import { flattenDeep, identity, isEmpty } from 'lodash';
|
||||||
import { UpdateMetadata } from '../interfaces';
|
|
||||||
|
|
||||||
export class BaseExplorerService {
|
export class BaseExplorerService {
|
||||||
getModules(
|
getModules(
|
||||||
@ -21,11 +20,11 @@ export class BaseExplorerService {
|
|||||||
): Module[] {
|
): Module[] {
|
||||||
const modules = [...modulesContainer.values()];
|
const modules = [...modulesContainer.values()];
|
||||||
return modules.filter(({ metatype }) =>
|
return modules.filter(({ metatype }) =>
|
||||||
include.some((item) => item === metatype),
|
include.includes(metatype),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
flatMap<T = UpdateMetadata>(
|
flatMap<T>(
|
||||||
modules: Module[],
|
modules: Module[],
|
||||||
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
|
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
|
||||||
): T[] {
|
): T[] {
|
||||||
@ -37,23 +36,4 @@ export class BaseExplorerService {
|
|||||||
};
|
};
|
||||||
return flattenDeep(invokeMap()).filter(identity);
|
return flattenDeep(invokeMap()).filter(identity);
|
||||||
}
|
}
|
||||||
|
|
||||||
groupMetadata(resolvers: UpdateMetadata[]) {
|
|
||||||
const groupByType = groupBy(
|
|
||||||
resolvers,
|
|
||||||
(metadata: UpdateMetadata) => metadata.type,
|
|
||||||
);
|
|
||||||
const groupedMetadata = mapValues(
|
|
||||||
groupByType,
|
|
||||||
(resolversArr: UpdateMetadata[]) =>
|
|
||||||
resolversArr.reduce(
|
|
||||||
(prev, curr) => ({
|
|
||||||
...prev,
|
|
||||||
[curr.name]: curr.callback,
|
|
||||||
}),
|
|
||||||
{},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return groupedMetadata;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
export * from './updates-explorer.service';
|
export * from './listeners-explorer.service';
|
||||||
export * from './metadata-accessor.service';
|
export * from './metadata-accessor.service';
|
||||||
export * from './scenes-explorer.service';
|
export * from './listeners-explorer.service';
|
||||||
export * from './updates-explorer.service';
|
|
||||||
|
@ -12,7 +12,7 @@ import { BaseExplorerService } from './base-explorer.service';
|
|||||||
import { getBotToken } from '../utils';
|
import { getBotToken } from '../utils';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegrafExplorerService
|
export class ListenersExplorerService
|
||||||
extends BaseExplorerService
|
extends BaseExplorerService
|
||||||
implements OnModuleInit {
|
implements OnModuleInit {
|
||||||
private readonly bot: Telegraf<any>;
|
private readonly bot: Telegraf<any>;
|
||||||
|
@ -18,13 +18,6 @@ export class MetadataAccessorService {
|
|||||||
return !!this.reflector.get(UPDATE_METADATA, target);
|
return !!this.reflector.get(UPDATE_METADATA, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
isUpdateListener(target: Function) {
|
|
||||||
if (!target) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !!this.reflector.get(UPDATE_LISTENER_METADATA, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
isScene(target: Function): boolean {
|
isScene(target: Function): boolean {
|
||||||
if (!target) {
|
if (!target) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -16,18 +16,15 @@ import {
|
|||||||
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
||||||
import {
|
import {
|
||||||
MetadataAccessorService,
|
MetadataAccessorService,
|
||||||
ScenesExplorerService,
|
ListenersExplorerService,
|
||||||
UpdatesExplorerService,
|
|
||||||
} from './services';
|
} from './services';
|
||||||
import { getBotToken } from './utils';
|
import { getBotToken, createBotFactory } from './utils';
|
||||||
import { createBotFactory } from './utils/create-bot-factory.util';
|
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
imports: [DiscoveryModule],
|
imports: [DiscoveryModule],
|
||||||
providers: [
|
providers: [
|
||||||
UpdatesExplorerService,
|
ListenersExplorerService,
|
||||||
ScenesExplorerService,
|
|
||||||
MetadataAccessorService,
|
MetadataAccessorService,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
@ -17,5 +17,5 @@ export type ComposerMethodArgs<
|
|||||||
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
|
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
|
||||||
> = Filter<Parameters<T[U]>, Middleware<never>>;
|
> = Filter<Parameters<T[U]>, Middleware<never>>;
|
||||||
|
|
||||||
export type UpdateMethods = OnlyFunctionPropertyNames<Composer<never>>;
|
export type ComposerMethods = OnlyFunctionPropertyNames<Composer<never>>;
|
||||||
export type SceneMethods = OnlyFunctionPropertyNames<BaseScene<never>>;
|
export type SceneMethods = OnlyFunctionPropertyNames<BaseScene<never>>;
|
||||||
|
@ -4,11 +4,24 @@ import { ComposerMethodArgs, SceneMethods } from '../types';
|
|||||||
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
||||||
import { ListenerMetadata } from '../interfaces';
|
import { ListenerMetadata } from '../interfaces';
|
||||||
|
|
||||||
export function createListenerDecorator<Method extends SceneMethods>(
|
export function createListenerDecorator<TMethod extends SceneMethods>(
|
||||||
method: Method,
|
method: TMethod,
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
...args: ComposerMethodArgs<Scene<never>, Method>
|
...args: ComposerMethodArgs<Scene<never>, TMethod>
|
||||||
|
): MethodDecorator => {
|
||||||
|
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||||
|
method,
|
||||||
|
args,
|
||||||
|
} as ListenerMetadata);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createMissedListenerDecorator<TArgs extends any[]>(
|
||||||
|
method: string,
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
...args: TArgs
|
||||||
): MethodDecorator => {
|
): MethodDecorator => {
|
||||||
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||||
method,
|
method,
|
||||||
|
@ -1 +1,3 @@
|
|||||||
export * from './get-bot-token.util';
|
export * from './get-bot-token.util';
|
||||||
|
export * from './create-bot-factory.util';
|
||||||
|
export * from './create-listener-decorator.util';
|
||||||
|
Loading…
Reference in New Issue
Block a user