mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
feat(): multiple instances support
This commit is contained in:
59
lib/services/base-explorer.service.ts
Normal file
59
lib/services/base-explorer.service.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||
import { Module } from '@nestjs/core/injector/module';
|
||||
import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash';
|
||||
import { UpdateMetadata } from '../interfaces';
|
||||
|
||||
export class BaseExplorerService {
|
||||
getModules(
|
||||
modulesContainer: Map<string, Module>,
|
||||
include: Function[],
|
||||
): Module[] {
|
||||
if (!include || isEmpty(include)) {
|
||||
return [...modulesContainer.values()];
|
||||
}
|
||||
const whitelisted = this.includeWhitelisted(modulesContainer, include);
|
||||
return whitelisted;
|
||||
}
|
||||
|
||||
includeWhitelisted(
|
||||
modulesContainer: Map<string, Module>,
|
||||
include: Function[],
|
||||
): Module[] {
|
||||
const modules = [...modulesContainer.values()];
|
||||
return modules.filter(({ metatype }) =>
|
||||
include.some((item) => item === metatype),
|
||||
);
|
||||
}
|
||||
|
||||
flatMap<T = UpdateMetadata>(
|
||||
modules: Module[],
|
||||
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
|
||||
): T[] {
|
||||
const invokeMap = () => {
|
||||
return modules.map((moduleRef) => {
|
||||
const providers = [...moduleRef.providers.values()];
|
||||
return providers.map((wrapper) => callback(wrapper, moduleRef));
|
||||
});
|
||||
};
|
||||
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;
|
||||
}
|
||||
}
|
2
lib/services/index.ts
Normal file
2
lib/services/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './updates-explorer.service';
|
||||
export * from './metadata-accessor.service';
|
204
lib/services/metadata-accessor.service.ts
Normal file
204
lib/services/metadata-accessor.service.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
import { Injectable, Type } 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';
|
||||
|
||||
@Injectable()
|
||||
export class MetadataAccessorService {
|
||||
constructor(private readonly reflector: Reflector) {}
|
||||
|
||||
isUpdate(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.UPDATE, target);
|
||||
}
|
||||
|
||||
isUpdateHook(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.UPDATE_HOOK, target);
|
||||
}
|
||||
|
||||
getUpdateHookMetadata(
|
||||
target: Type<any> | Function,
|
||||
): UpdateHookOptions | undefined {
|
||||
return this.reflector.get(DECORATORS.UPDATE_HOOK, target);
|
||||
}
|
||||
|
||||
isTelegrafUse(target: Type<any> | Function): boolean {
|
||||
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);
|
||||
}
|
||||
}
|
267
lib/services/updates-explorer.service.ts
Normal file
267
lib/services/updates-explorer.service.ts
Normal file
@@ -0,0 +1,267 @@
|
||||
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import { DiscoveryService, ModuleRef, ModulesContainer } from '@nestjs/core';
|
||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
||||
import { MetadataAccessorService } from './metadata-accessor.service';
|
||||
import {
|
||||
TELEGRAF_BOT_NAME,
|
||||
TELEGRAF_MODULE_OPTIONS,
|
||||
} from '../telegraf.constants';
|
||||
import {
|
||||
ActionOptions,
|
||||
CashtagOptions,
|
||||
CommandOptions,
|
||||
EntityOptions,
|
||||
HashtagOptions,
|
||||
HearsOptions,
|
||||
InlineQueryOptions,
|
||||
MentionOptions,
|
||||
OnOptions,
|
||||
PhoneOptions,
|
||||
UpdateHookOptions,
|
||||
} from '../decorators';
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { TelegrafModuleOptions } from '../interfaces';
|
||||
import { BaseExplorerService } from './base-explorer.service';
|
||||
import { Module } from '@nestjs/core/injector/module';
|
||||
|
||||
@Injectable()
|
||||
export class UpdatesExplorerService
|
||||
extends BaseExplorerService
|
||||
implements OnModuleInit {
|
||||
private readonly logger = new Logger(UpdatesExplorerService.name);
|
||||
|
||||
constructor(
|
||||
@Inject(TELEGRAF_BOT_NAME)
|
||||
private readonly botName: string,
|
||||
@Inject(TELEGRAF_MODULE_OPTIONS)
|
||||
private readonly telegrafModuleOptions: TelegrafModuleOptions,
|
||||
private readonly moduleRef: ModuleRef,
|
||||
private readonly discoveryService: DiscoveryService,
|
||||
private readonly metadataAccessor: MetadataAccessorService,
|
||||
private readonly metadataScanner: MetadataScanner,
|
||||
private readonly modulesContainer: ModulesContainer,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
private bot: Telegraf<any>;
|
||||
|
||||
onModuleInit(): void {
|
||||
this.logger.debug(this.botName);
|
||||
this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, {
|
||||
strict: false,
|
||||
});
|
||||
this.explore();
|
||||
}
|
||||
|
||||
explore() {
|
||||
const modules = this.getModules(
|
||||
this.modulesContainer,
|
||||
this.telegrafModuleOptions.include || [],
|
||||
);
|
||||
const updates = this.flatMap(modules, (instance, moduleRef) =>
|
||||
this.applyUpdates(instance, moduleRef),
|
||||
);
|
||||
}
|
||||
|
||||
private applyUpdates(wrapper: InstanceWrapper, moduleRef: Module) {
|
||||
const { instance } = wrapper;
|
||||
if (!instance) {
|
||||
return undefined;
|
||||
}
|
||||
const prototype = Object.getPrototypeOf(instance);
|
||||
|
||||
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
|
||||
const updateProviders: InstanceWrapper[] = providers.filter(
|
||||
(wrapper: InstanceWrapper) =>
|
||||
this.metadataAccessor.isUpdate(wrapper.metatype),
|
||||
);
|
||||
|
||||
updateProviders.forEach((wrapper: InstanceWrapper) => {
|
||||
const { instance } = wrapper;
|
||||
if (!instance) {
|
||||
return undefined;
|
||||
}
|
||||
this.metadataScanner.scanFromPrototype(instance, prototype, (name) => {
|
||||
if (this.metadataAccessor.isUpdateHook(instance[name])) {
|
||||
const metadata = this.metadataAccessor.getUpdateHookMetadata(
|
||||
instance[name],
|
||||
);
|
||||
this.handleUpdateHook(instance, name, metadata);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
providers.forEach((wrapper: InstanceWrapper) => {
|
||||
const { instance } = wrapper;
|
||||
if (!instance) {
|
||||
return undefined;
|
||||
}
|
||||
this.metadataScanner.scanFromPrototype(
|
||||
instance,
|
||||
prototype,
|
||||
(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.bot.on(metadata.updateType, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafUse(instance: object, key: string) {
|
||||
this.bot.use(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafOn(instance: object, key: string, metadata: OnOptions) {
|
||||
this.bot.on(metadata.updateTypes, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafHears(instance: object, key: string, metadata: HearsOptions) {
|
||||
this.bot.hears(metadata.triggers, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafCommand(
|
||||
instance: object,
|
||||
key: string,
|
||||
metadata: CommandOptions,
|
||||
) {
|
||||
this.bot.command(metadata.commands, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafStart(instance: object, key: string) {
|
||||
this.bot.start(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafHelp(instance: object, key: string) {
|
||||
this.bot.help(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafSettings(instance: object, key: string) {
|
||||
// @ts-ignore
|
||||
this.bot.settings(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) {
|
||||
// @ts-ignore
|
||||
this.bot.entity(metadata.entity, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafMention(
|
||||
instance: object,
|
||||
key: string,
|
||||
metadata: MentionOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
this.bot.mention(metadata.username, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) {
|
||||
// @ts-ignore
|
||||
this.bot.phone(metadata.phone, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafHashtag(
|
||||
instance: object,
|
||||
key: string,
|
||||
metadata: HashtagOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
this.bot.hashtag(metadata.hashtag, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafCashtag(
|
||||
instance: object,
|
||||
key: string,
|
||||
metadata: CashtagOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
this.bot.cashtag(metadata.cashtag, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) {
|
||||
this.bot.action(metadata.triggers, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafInlineQuery(
|
||||
instance: object,
|
||||
key: string,
|
||||
metadata: InlineQueryOptions,
|
||||
) {
|
||||
if (metadata.triggers) {
|
||||
// @ts-ignore
|
||||
this.bot.inlineQuery(metadata.triggers, instance[key].bind(instance));
|
||||
} else {
|
||||
this.bot.on(metadata.updateType, instance[key].bind(instance));
|
||||
}
|
||||
}
|
||||
|
||||
handleTelegrafGameQuery(instance: object, key: string) {
|
||||
this.bot.gameQuery(instance[key].bind(instance));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user