mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-12-17 02:39:52 +03:00
feat: new decorators for telegram bot api updates (#119)
This commit is contained in:
committed by
GitHub
parent
f496e51013
commit
6350a6e8d1
@@ -16,6 +16,7 @@ import {
|
||||
MentionOptions,
|
||||
OnOptions,
|
||||
PhoneOptions,
|
||||
UpdateHookOptions,
|
||||
} from './decorators';
|
||||
|
||||
@Injectable()
|
||||
@@ -27,12 +28,44 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
private readonly metadataScanner: MetadataScanner,
|
||||
) {}
|
||||
|
||||
private telegraf: TelegrafProvider;
|
||||
|
||||
onModuleInit() {
|
||||
this.telegraf = this.moduleRef.get<TelegrafProvider>(TELEGRAF_PROVIDER, {
|
||||
strict: false,
|
||||
});
|
||||
this.explore();
|
||||
}
|
||||
|
||||
explore() {
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
|
||||
|
||||
providers.forEach((wrapper: InstanceWrapper) => {
|
||||
const { instance } = wrapper;
|
||||
|
||||
@@ -40,215 +73,166 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
return;
|
||||
}
|
||||
|
||||
const telegraf: TelegrafProvider = this.moduleRef.get<TelegrafProvider>(
|
||||
TELEGRAF_PROVIDER,
|
||||
{
|
||||
strict: false,
|
||||
},
|
||||
);
|
||||
|
||||
this.metadataScanner.scanFromPrototype(
|
||||
instance,
|
||||
Object.getPrototypeOf(instance),
|
||||
(key: string) => {
|
||||
if (this.metadataAccessor.isTelegrafUse(instance[key])) {
|
||||
this.handleTelegrafUse(instance, key, telegraf);
|
||||
this.handleTelegrafUse(instance, key);
|
||||
} else if (this.metadataAccessor.isTelegrafOn(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafOnMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafOn(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafOn(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafHears(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafHearsMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafHears(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafHears(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafCommand(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafCommandMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafCommand(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafCommand(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafStart(instance[key])) {
|
||||
this.handleTelegrafStart(instance, key, telegraf);
|
||||
this.handleTelegrafStart(instance, key);
|
||||
} else if (this.metadataAccessor.isTelegrafHelp(instance[key])) {
|
||||
this.handleTelegrafHelp(instance, key, telegraf);
|
||||
this.handleTelegrafHelp(instance, key);
|
||||
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
|
||||
this.handleTelegrafSettings(instance, key, telegraf);
|
||||
this.handleTelegrafSettings(instance, key);
|
||||
} else if (this.metadataAccessor.isTelegrafEntity(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafEntityMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafEntity(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafEntity(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafMention(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafMentionMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafMention(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafMention(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafPhone(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafPhoneMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafPhone(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafPhone(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafHashtagMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafHashtag(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafHashtag(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafCashtagMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafCashtag(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafCashtag(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafAction(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafActionMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafAction(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafAction(instance, key, metadata);
|
||||
} else if (
|
||||
this.metadataAccessor.isTelegrafInlineQuery(instance[key])
|
||||
) {
|
||||
const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafInlineQuery(instance, key, telegraf, metadata);
|
||||
this.handleTelegrafInlineQuery(instance, key, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) {
|
||||
this.handleTelegrafGameQuery(instance, key, telegraf);
|
||||
this.handleTelegrafGameQuery(instance, key);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
handleTelegrafUse(instance: object, key: string, telegraf: TelegrafProvider) {
|
||||
telegraf.use(instance[key].bind(instance));
|
||||
handleUpdateHook(instance: object, key: string, metadata: UpdateHookOptions) {
|
||||
this.telegraf.on(metadata.updateType, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafOn(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: OnOptions,
|
||||
) {
|
||||
telegraf.on(metadata.updateTypes, instance[key].bind(instance));
|
||||
handleTelegrafUse(instance: object, key: string) {
|
||||
this.telegraf.use(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafHears(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: HearsOptions,
|
||||
) {
|
||||
telegraf.hears(metadata.triggers, instance[key].bind(instance));
|
||||
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));
|
||||
}
|
||||
|
||||
handleTelegrafCommand(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: CommandOptions,
|
||||
) {
|
||||
telegraf.command(metadata.commands, instance[key].bind(instance));
|
||||
this.telegraf.command(metadata.commands, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafStart(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
) {
|
||||
telegraf.start(instance[key].bind(instance));
|
||||
handleTelegrafStart(instance: object, key: string) {
|
||||
this.telegraf.start(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafHelp(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
) {
|
||||
telegraf.help(instance[key].bind(instance));
|
||||
handleTelegrafHelp(instance: object, key: string) {
|
||||
this.telegraf.help(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafSettings(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
) {
|
||||
handleTelegrafSettings(instance: object, key: string) {
|
||||
// @ts-ignore
|
||||
telegraf.settings(instance[key].bind(instance));
|
||||
this.telegraf.settings(instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafEntity(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: EntityOptions,
|
||||
) {
|
||||
handleTelegrafEntity(instance: object, key: string, metadata: EntityOptions) {
|
||||
// @ts-ignore
|
||||
telegraf.entity(metadata.entity, instance[key].bind(instance));
|
||||
this.telegraf.entity(metadata.entity, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafMention(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: MentionOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
telegraf.mention(metadata.username, instance[key].bind(instance));
|
||||
this.telegraf.mention(metadata.username, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafPhone(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: PhoneOptions,
|
||||
) {
|
||||
handleTelegrafPhone(instance: object, key: string, metadata: PhoneOptions) {
|
||||
// @ts-ignore
|
||||
telegraf.phone(metadata.phone, instance[key].bind(instance));
|
||||
this.telegraf.phone(metadata.phone, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafHashtag(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: HashtagOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
|
||||
this.telegraf.hashtag(metadata.hashtag, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafCashtag(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: CashtagOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
|
||||
this.telegraf.cashtag(metadata.cashtag, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafAction(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: ActionOptions,
|
||||
) {
|
||||
telegraf.action(metadata.triggers, instance[key].bind(instance));
|
||||
handleTelegrafAction(instance: object, key: string, metadata: ActionOptions) {
|
||||
this.telegraf.action(metadata.triggers, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafInlineQuery(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
metadata: InlineQueryOptions,
|
||||
) {
|
||||
// @ts-ignore
|
||||
telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));
|
||||
this.telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafGameQuery(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: TelegrafProvider,
|
||||
) {
|
||||
telegraf.gameQuery(instance[key].bind(instance));
|
||||
handleTelegrafGameQuery(instance: object, key: string) {
|
||||
this.telegraf.gameQuery(instance[key].bind(instance));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user