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 {
|
|
|
|
TelegrafActionMetadata,
|
|
|
|
TelegrafCashtagMetadata,
|
|
|
|
TelegrafCommandMetadata,
|
|
|
|
TelegrafEntityMetadata,
|
|
|
|
TelegrafHashtagMetadata,
|
|
|
|
TelegrafHearsMetadata,
|
|
|
|
TelegrafInlineQueryMetadata,
|
|
|
|
TelegrafMentionMetadata,
|
|
|
|
TelegrafOnMetadata,
|
|
|
|
TelegrafPhoneMetadata,
|
|
|
|
} 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,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
onModuleInit() {
|
|
|
|
this.explore();
|
|
|
|
}
|
|
|
|
|
|
|
|
explore() {
|
|
|
|
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
|
|
|
|
providers.forEach((wrapper: InstanceWrapper) => {
|
|
|
|
const { instance } = wrapper;
|
|
|
|
|
|
|
|
if (!instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-03 09:46:28 +03:00
|
|
|
const telegraf: TelegrafProvider = this.moduleRef.get<TelegrafProvider>(
|
2020-03-19 16:21:35 +03:00
|
|
|
TELEGRAF_PROVIDER,
|
2020-05-03 09:46:28 +03:00
|
|
|
{
|
|
|
|
strict: false,
|
|
|
|
},
|
2020-03-19 16:21:35 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
this.metadataScanner.scanFromPrototype(
|
|
|
|
instance,
|
|
|
|
Object.getPrototypeOf(instance),
|
|
|
|
(key: string) => {
|
2020-03-19 16:34:44 +03:00
|
|
|
if (this.metadataAccessor.isTelegrafUse(instance[key])) {
|
|
|
|
this.handleTelegrafUse(instance, key, telegraf);
|
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, telegraf, metadata);
|
|
|
|
} else if (this.metadataAccessor.isTelegrafHears(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafHearsMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
|
|
|
this.handleTelegrafHears(instance, key, telegraf, metadata);
|
2020-03-19 16:39:47 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafCommand(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafCommandMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
|
|
|
this.handleTelegrafCommand(instance, key, telegraf, metadata);
|
2020-03-19 16:44:05 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafStart(instance[key])) {
|
|
|
|
this.handleTelegrafStart(instance, key, telegraf);
|
|
|
|
} else if (this.metadataAccessor.isTelegrafHelp(instance[key])) {
|
|
|
|
this.handleTelegrafHelp(instance, key, telegraf);
|
2020-03-19 16:46:31 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
|
|
|
|
this.handleTelegrafSettings(instance, key, telegraf);
|
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, telegraf, metadata);
|
2020-03-19 16:58:27 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafMention(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafMentionMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
|
|
|
this.handleTelegrafMention(instance, key, telegraf, 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, telegraf, metadata);
|
2020-03-19 17:14:34 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafHashtagMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
|
|
|
this.handleTelegrafHashtag(instance, key, telegraf, metadata);
|
2020-03-19 17:16:48 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafCashtagMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
|
|
|
this.handleTelegrafCashtag(instance, key, telegraf, 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, telegraf, metadata);
|
2020-03-19 17:25:47 +03:00
|
|
|
} else if (
|
|
|
|
this.metadataAccessor.isTelegrafInlineQuery(instance[key])
|
|
|
|
) {
|
|
|
|
const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata(
|
|
|
|
instance[key],
|
|
|
|
);
|
|
|
|
this.handleTelegrafInlineQuery(instance, key, telegraf, metadata);
|
2020-03-19 17:28:56 +03:00
|
|
|
} else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) {
|
|
|
|
this.handleTelegrafGameQuery(instance, key, telegraf);
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-03 09:46:28 +03:00
|
|
|
handleTelegrafUse(instance: object, key: string, telegraf: TelegrafProvider) {
|
2020-03-19 16:34:44 +03:00
|
|
|
telegraf.use(instance[key].bind(instance));
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
handleTelegrafOn(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafOnMetadata,
|
2020-03-19 16:21:35 +03:00
|
|
|
) {
|
|
|
|
telegraf.on(metadata.updateTypes, instance[key].bind(instance));
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:44:05 +03:00
|
|
|
handleTelegrafHears(
|
2020-03-19 16:21:35 +03:00
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafHearsMetadata,
|
2020-03-19 16:21:35 +03:00
|
|
|
) {
|
2020-03-19 16:44:05 +03:00
|
|
|
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-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafCommandMetadata,
|
2020-03-19 16:21:35 +03:00
|
|
|
) {
|
2020-03-19 16:44:05 +03:00
|
|
|
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-03-19 16:44:05 +03:00
|
|
|
handleTelegrafStart(
|
2020-03-19 16:39:47 +03:00
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 16:39:47 +03:00
|
|
|
) {
|
2020-03-19 16:44:05 +03:00
|
|
|
telegraf.start(instance[key].bind(instance));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTelegrafHelp(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 16:44:05 +03:00
|
|
|
) {
|
|
|
|
telegraf.help(instance[key].bind(instance));
|
2020-03-19 16:39:47 +03:00
|
|
|
}
|
2020-03-19 16:46:31 +03:00
|
|
|
|
|
|
|
handleTelegrafSettings(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 16:46:31 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.settings(instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 16:54:34 +03:00
|
|
|
|
|
|
|
handleTelegrafEntity(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafEntityMetadata,
|
2020-03-19 16:54:34 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.entity(metadata.entity, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 16:58:27 +03:00
|
|
|
|
|
|
|
handleTelegrafMention(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafMentionMetadata,
|
2020-03-19 16:58:27 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.mention(metadata.username, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 17:02:37 +03:00
|
|
|
|
|
|
|
handleTelegrafPhone(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafPhoneMetadata,
|
2020-03-19 17:02:37 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.phone(metadata.phone, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 17:14:34 +03:00
|
|
|
|
|
|
|
handleTelegrafHashtag(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafHashtagMetadata,
|
2020-03-19 17:14:34 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 17:16:48 +03:00
|
|
|
|
|
|
|
handleTelegrafCashtag(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafCashtagMetadata,
|
2020-03-19 17:16:48 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 17:20:13 +03:00
|
|
|
|
|
|
|
handleTelegrafAction(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafActionMetadata,
|
2020-03-19 17:20:13 +03:00
|
|
|
) {
|
2020-03-19 17:20:59 +03:00
|
|
|
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-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 18:20:07 +03:00
|
|
|
metadata: TelegrafInlineQueryMetadata,
|
2020-03-19 17:25:47 +03:00
|
|
|
) {
|
|
|
|
// @ts-ignore
|
|
|
|
telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 17:28:56 +03:00
|
|
|
|
|
|
|
handleTelegrafGameQuery(
|
|
|
|
instance: object,
|
|
|
|
key: string,
|
2020-05-03 09:46:28 +03:00
|
|
|
telegraf: TelegrafProvider,
|
2020-03-19 17:28:56 +03:00
|
|
|
) {
|
|
|
|
telegraf.gameQuery(instance[key].bind(instance));
|
|
|
|
}
|
2020-03-19 16:21:35 +03:00
|
|
|
}
|