nestjs-telegraf/lib/services/updates-explorer.service.ts

268 lines
9.0 KiB
TypeScript
Raw Normal View History

2021-01-02 01:27:01 +03:00
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, ModuleRef, ModulesContainer } from '@nestjs/core';
2020-03-19 16:21:35 +03:00
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
2021-01-02 01:27:01 +03:00
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,
2021-01-02 01:27:01 +03:00
} from '../decorators';
import { Telegraf } from 'telegraf';
import { TelegrafModuleOptions } from '../interfaces';
import { BaseExplorerService } from './base-explorer.service';
import { Module } from '@nestjs/core/injector/module';
2020-03-19 16:21:35 +03:00
@Injectable()
2021-01-02 01:27:01 +03:00
export class UpdatesExplorerService
extends BaseExplorerService
implements OnModuleInit {
private readonly logger = new Logger(UpdatesExplorerService.name);
2020-03-19 16:21:35 +03:00
constructor(
2021-01-02 01:27:01 +03:00
@Inject(TELEGRAF_BOT_NAME)
private readonly botName: string,
@Inject(TELEGRAF_MODULE_OPTIONS)
private readonly telegrafModuleOptions: TelegrafModuleOptions,
2020-03-19 16:21:35 +03:00
private readonly moduleRef: ModuleRef,
private readonly discoveryService: DiscoveryService,
2021-01-02 01:27:01 +03:00
private readonly metadataAccessor: MetadataAccessorService,
2020-03-19 16:21:35 +03:00
private readonly metadataScanner: MetadataScanner,
2021-01-02 01:27:01 +03:00
private readonly modulesContainer: ModulesContainer,
) {
super();
}
2020-03-19 16:21:35 +03:00
2021-01-02 01:27:01 +03:00
private bot: Telegraf<any>;
2021-01-02 01:27:01 +03:00
onModuleInit(): void {
this.logger.debug(this.botName);
this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, {
strict: false,
});
2020-03-19 16:21:35 +03:00
this.explore();
}
explore() {
2021-01-02 01:27:01 +03:00
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),
2021-01-02 01:27:01 +03:00
);
updateProviders.forEach((wrapper: InstanceWrapper) => {
const { instance } = wrapper;
2021-01-02 01:27:01 +03:00
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);
}
});
});
2020-03-19 16:21:35 +03:00
providers.forEach((wrapper: InstanceWrapper) => {
const { instance } = wrapper;
if (!instance) {
2021-01-02 01:27:01 +03:00
return undefined;
2020-03-19 16:21:35 +03:00
}
this.metadataScanner.scanFromPrototype(
instance,
2021-01-02 01:27:01 +03:00
prototype,
2020-03-19 16:21:35 +03:00
(key: string) => {
2020-03-19 16:34:44 +03:00
if (this.metadataAccessor.isTelegrafUse(instance[key])) {
this.handleTelegrafUse(instance, key);
2020-03-19 16:21:35 +03:00
} else if (this.metadataAccessor.isTelegrafOn(instance[key])) {
const metadata = this.metadataAccessor.getTelegrafOnMetadata(
instance[key],
);
this.handleTelegrafOn(instance, key, metadata);
2020-03-19 16:21:35 +03:00
} 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);
2020-03-19 16:44:05 +03:00
} else if (this.metadataAccessor.isTelegrafStart(instance[key])) {
this.handleTelegrafStart(instance, key);
2020-03-19 16:44:05 +03:00
} else if (this.metadataAccessor.isTelegrafHelp(instance[key])) {
this.handleTelegrafHelp(instance, key);
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
this.handleTelegrafSettings(instance, key);
2020-03-19 16:54:34 +03:00
} 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);
2020-03-19 17:02:37 +03:00
} 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);
2020-03-19 17:20:13 +03:00
} 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);
2020-03-19 16:21:35 +03:00
}
},
);
});
}
handleUpdateHook(instance: object, key: string, metadata: UpdateHookOptions) {
2021-01-02 01:27:01 +03:00
this.bot.on(metadata.updateType, instance[key].bind(instance));
2020-03-19 16:34:44 +03:00
}
handleTelegrafUse(instance: object, key: string) {
2021-01-02 01:27:01 +03:00
this.bot.use(instance[key].bind(instance));
2020-03-19 16:21:35 +03:00
}
handleTelegrafOn(instance: object, key: string, metadata: OnOptions) {
2021-01-02 01:27:01 +03:00
this.bot.on(metadata.updateTypes, instance[key].bind(instance));
}
handleTelegrafHears(instance: object, key: string, metadata: HearsOptions) {
2021-01-02 01:27:01 +03:00
this.bot.hears(metadata.triggers, instance[key].bind(instance));
2020-03-19 16:21:35 +03:00
}
2020-03-19 16:44:05 +03:00
handleTelegrafCommand(
2020-03-19 16:21:35 +03:00
instance: object,
key: string,
metadata: CommandOptions,
2020-03-19 16:21:35 +03:00
) {
2021-01-02 01:27:01 +03:00
this.bot.command(metadata.commands, instance[key].bind(instance));
2020-03-19 16:21:35 +03:00
}
handleTelegrafStart(instance: object, key: string) {
2021-01-02 01:27:01 +03:00
this.bot.start(instance[key].bind(instance));
2020-03-19 16:44:05 +03:00
}
handleTelegrafHelp(instance: object, key: string) {
2021-01-02 01:27:01 +03:00
this.bot.help(instance[key].bind(instance));
}
handleTelegrafSettings(instance: object, key: string) {
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.settings(instance[key].bind(instance));
}
2020-03-19 16:54:34 +03:00
handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) {
2020-03-19 16:54:34 +03:00
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.entity(metadata.entity, instance[key].bind(instance));
2020-03-19 16:54:34 +03:00
}
handleTelegrafMention(
instance: object,
key: string,
metadata: MentionOptions,
) {
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.mention(metadata.username, instance[key].bind(instance));
}
2020-03-19 17:02:37 +03:00
handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) {
2020-03-19 17:02:37 +03:00
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.phone(metadata.phone, instance[key].bind(instance));
2020-03-19 17:02:37 +03:00
}
handleTelegrafHashtag(
instance: object,
key: string,
metadata: HashtagOptions,
) {
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.hashtag(metadata.hashtag, instance[key].bind(instance));
}
handleTelegrafCashtag(
instance: object,
key: string,
metadata: CashtagOptions,
) {
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.cashtag(metadata.cashtag, instance[key].bind(instance));
}
2020-03-19 17:20:13 +03:00
handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) {
2021-01-02 01:27:01 +03:00
this.bot.action(metadata.triggers, instance[key].bind(instance));
2020-03-19 17:20:13 +03:00
}
handleTelegrafInlineQuery(
instance: object,
key: string,
metadata: InlineQueryOptions,
) {
if (metadata.triggers) {
// @ts-ignore
2021-01-02 01:27:01 +03:00
this.bot.inlineQuery(metadata.triggers, instance[key].bind(instance));
} else {
2021-01-02 01:27:01 +03:00
this.bot.on(metadata.updateType, instance[key].bind(instance));
}
}
handleTelegrafGameQuery(instance: object, key: string) {
2021-01-02 01:27:01 +03:00
this.bot.gameQuery(instance[key].bind(instance));
}
2020-03-19 16:21:35 +03:00
}