2020-03-19 16:21:35 +03:00
|
|
|
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';
|
2020-03-19 18:20:07 +03:00
|
|
|
import {
|
2020-08-14 12:42:57 +03:00
|
|
|
ActionOptions,
|
|
|
|
CashtagOptions,
|
|
|
|
CommandOptions,
|
|
|
|
EntityOptions,
|
|
|
|
HashtagOptions,
|
|
|
|
HearsOptions,
|
|
|
|
InlineQueryOptions,
|
|
|
|
MentionOptions,
|
|
|
|
OnOptions,
|
|
|
|
PhoneOptions,
|
2020-09-09 22:46:25 +03:00
|
|
|
UpdateHookOptions,
|
2020-03-19 18:20:07 +03:00
|
|
|
} from './decorators';
|
2020-03-19 16:21:35 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class TelegrafExplorer implements OnModuleInit {
|
|
|
|
constructor(
|
|
|
|
private readonly moduleRef: ModuleRef,
|
|
|
|
private readonly discoveryService: DiscoveryService,
|
|
|
|
private readonly metadataAccessor: TelegrafMetadataAccessor,
|
|
|
|
private readonly metadataScanner: MetadataScanner,
|
|
|
|
) {}
|
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
private telegraf: TelegrafProvider;
|
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
onModuleInit() {
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf = this.moduleRef.get<TelegrafProvider>(TELEGRAF_PROVIDER, {
|
|
|
|
strict: false,
|
|
|
|
});
|
2020-03-19 16:21:35 +03:00
|
|
|
this.explore();
|
|
|
|
}
|
|
|
|
|
|
|
|
explore() {
|
2020-09-09 22:46:25 +03:00
|
|
|
/**
|
|
|
|
* Update providers section is only for decorators under Update decorator
|
|
|
|
*/
|
|
|
|
const updateProviders: InstanceWrapper[] = this.discoveryService
|
|
|
|
.getProviders()
|
|
|
|
.filter((wrapper: InstanceWrapper) =>
|
|
|
|
this.metadataAccessor.isUpdate(wrapper.metatype),
|
|
|
|
);
|
|
|
|
|
|
|
|
updateProviders.forEach((wrapper: InstanceWrapper) => {
|
|
|
|
const { instance } = wrapper;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
|
2020-09-09 22:46:25 +03:00
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
providers.forEach((wrapper: InstanceWrapper) => {
|
|
|
|
const { instance } = wrapper;
|
|
|
|
|
|
|
|
if (!instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.metadataScanner.scanFromPrototype(
|
|
|
|
instance,
|
|
|
|
Object.getPrototypeOf(instance),
|
|
|
|
(key: string) => {
|
2020-03-19 16:34:44 +03:00
|
|
|
if (this.metadataAccessor.isTelegrafUse(instance[key])) {
|
2020-09-09 22:46:25 +03:00
|
|
|
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],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
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],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafHears(instance, key, metadata);
|
2020-03-19 16:39:47 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafCommand(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafCommandMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafCommand(instance, key, metadata);
|
2020-03-19 16:44:05 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafStart(instance[key])) {
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafStart(instance, key);
|
2020-03-19 16:44:05 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafHelp(instance[key])) {
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafHelp(instance, key);
|
2020-03-19 16:46:31 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
|
2020-09-09 22:46:25 +03:00
|
|
|
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],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafEntity(instance, key, metadata);
|
2020-03-19 16:58:27 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafMention(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafMentionMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
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],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafPhone(instance, key, metadata);
|
2020-03-19 17:14:34 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafHashtagMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafHashtag(instance, key, metadata);
|
2020-03-19 17:16:48 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafCashtagMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
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],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafAction(instance, key, metadata);
|
2020-03-19 17:25:47 +03:00
|
|
|
} else if (
|
|
|
|
this.metadataAccessor.isTelegrafInlineQuery(instance[key])
|
|
|
|
) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafInlineQuery(instance, key, metadata);
|
2020-03-19 17:28:56 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) {
|
2020-09-09 22:46:25 +03:00
|
|
|
this.handleTelegrafGameQuery(instance, key);
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleUpdateHook(instance: object, key: string, metadata: UpdateHookOptions) {
|
|
|
|
this.telegraf.on(metadata.updateType, instance[key].bind(instance));
|
2020-03-19 16:34:44 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafUse(instance: object, key: string) {
|
|
|
|
this.telegraf.use(instance[key].bind(instance));
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
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));
|
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,
|
2020-08-14 12:42:57 +03:00
|
|
|
metadata: CommandOptions,
|
2020-03-19 16:21:35 +03:00
|
|
|
) {
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.command(metadata.commands, instance[key].bind(instance));
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|
2020-03-19 16:39:47 +03:00
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafStart(instance: object, key: string) {
|
|
|
|
this.telegraf.start(instance[key].bind(instance));
|
2020-03-19 16:44:05 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafHelp(instance: object, key: string) {
|
|
|
|
this.telegraf.help(instance[key].bind(instance));
|
2020-03-19 16:39:47 +03:00
|
|
|
}
|
2020-03-19 16:46:31 +03:00
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafSettings(instance: object, key: string) {
|
2020-03-19 16:46:31 +03:00
|
|
|
// @ts-ignore
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.settings(instance[key].bind(instance));
|
2020-03-19 16:46:31 +03:00
|
|
|
}
|
2020-03-19 16:54:34 +03:00
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) {
|
2020-03-19 16:54:34 +03:00
|
|
|
// @ts-ignore
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.entity(metadata.entity, instance[key].bind(instance));
|
2020-03-19 16:54:34 +03:00
|
|
|
}
|
2020-03-19 16:58:27 +03:00
|
|
|
|
|
|
|
handleTelegrafMention(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-08-14 12:42:57 +03:00
|
|
|
metadata: MentionOptions,
|
2020-03-19 16:58:27 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.mention(metadata.username, instance[key].bind(instance));
|
2020-03-19 16:58:27 +03:00
|
|
|
}
|
2020-03-19 17:02:37 +03:00
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) {
|
2020-03-19 17:02:37 +03:00
|
|
|
// @ts-ignore
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.phone(metadata.phone, instance[key].bind(instance));
|
2020-03-19 17:02:37 +03:00
|
|
|
}
|
2020-03-19 17:14:34 +03:00
|
|
|
|
|
|
|
handleTelegrafHashtag(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-08-14 12:42:57 +03:00
|
|
|
metadata: HashtagOptions,
|
2020-03-19 17:14:34 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
|
2020-03-19 17:14:34 +03:00
|
|
|
}
|
2020-03-19 17:16:48 +03:00
|
|
|
|
|
|
|
handleTelegrafCashtag(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-08-14 12:42:57 +03:00
|
|
|
metadata: CashtagOptions,
|
2020-03-19 17:16:48 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
2020-09-09 22:46:25 +03:00
|
|
|
this.telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
|
2020-03-19 17:16:48 +03:00
|
|
|
}
|
2020-03-19 17:20:13 +03:00
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) {
|
|
|
|
this.telegraf.action(metadata.triggers, instance[key].bind(instance));
|
2020-03-19 17:20:13 +03:00
|
|
|
}
|
2020-03-19 17:25:47 +03:00
|
|
|
|
|
|
|
handleTelegrafInlineQuery(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-08-14 12:42:57 +03:00
|
|
|
metadata: InlineQueryOptions,
|
2020-03-19 17:25:47 +03:00
|
|
|
) {
|
2020-09-10 11:23:04 +03:00
|
|
|
if (metadata.triggers) {
|
|
|
|
// @ts-ignore
|
|
|
|
this.telegraf.inlineQuery(
|
|
|
|
metadata.triggers,
|
|
|
|
instance[key].bind(instance),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.telegraf.on(metadata.updateType, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 17:25:47 +03:00
|
|
|
}
|
2020-03-19 17:28:56 +03:00
|
|
|
|
2020-09-09 22:46:25 +03:00
|
|
|
handleTelegrafGameQuery(instance: object, key: string) {
|
|
|
|
this.telegraf.gameQuery(instance[key].bind(instance));
|
2020-03-19 17:28:56 +03:00
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|