diff --git a/lib/decorators/telegraf-action.decorator.ts b/lib/decorators/telegraf-action.decorator.ts index ed58872..2fd6781 100644 --- a/lib/decorators/telegraf-action.decorator.ts +++ b/lib/decorators/telegraf-action.decorator.ts @@ -2,12 +2,20 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; import { HearsTriggers } from 'telegraf'; +export type TelegrafActionTriggers = HearsTriggers; + +export interface TelegrafActionMetadata { + triggers: TelegrafActionTriggers; +} + /** * Registers middleware for handling callback_data actions with regular expressions. * @param triggers Triggers * * https://telegraf.js.org/#/?id=action */ -export function TelegrafAction(triggers: HearsTriggers): MethodDecorator { +export function TelegrafAction( + triggers: TelegrafActionTriggers, +): MethodDecorator { return SetMetadata(DECORATORS.ACTION, { triggers }); } diff --git a/lib/decorators/telegraf-cashtag.decorator.ts b/lib/decorators/telegraf-cashtag.decorator.ts index 40a6038..136c8c8 100644 --- a/lib/decorators/telegraf-cashtag.decorator.ts +++ b/lib/decorators/telegraf-cashtag.decorator.ts @@ -1,12 +1,20 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; +export type TelegrafCashtagCashtag = string | string[]; + +export interface TelegrafCashtagMetadata { + cashtag: TelegrafCashtagCashtag; +} + /** * Cashtag handling. * @param cashtag Cashtag * * https://telegraf.js.org/#/?id=cashtag */ -export function TelegrafCashtag(cashtag: string | string[]): MethodDecorator { +export function TelegrafCashtag( + cashtag: TelegrafCashtagCashtag, +): MethodDecorator { return SetMetadata(DECORATORS.CASHTAG, { cashtag }); } diff --git a/lib/decorators/telegraf-command.decorator.ts b/lib/decorators/telegraf-command.decorator.ts index bb20866..7f1570c 100644 --- a/lib/decorators/telegraf-command.decorator.ts +++ b/lib/decorators/telegraf-command.decorator.ts @@ -1,12 +1,20 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; +export type TelegrafCommandCommands = string | string[]; + +export interface TelegrafCommandMetadata { + commands: TelegrafCommandCommands; +} + /** * Command handling. * @param commands Commands * * https://telegraf.js.org/#/?id=command */ -export function TelegrafCommand(commands: string | string[]): MethodDecorator { +export function TelegrafCommand( + commands: TelegrafCommandCommands, +): MethodDecorator { return SetMetadata(DECORATORS.COMMAND, { commands }); } diff --git a/lib/decorators/telegraf-entity.decorator.ts b/lib/decorators/telegraf-entity.decorator.ts index cf1173d..b32c173 100644 --- a/lib/decorators/telegraf-entity.decorator.ts +++ b/lib/decorators/telegraf-entity.decorator.ts @@ -1,7 +1,16 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; -export type Entity = string | string[] | RegExp | RegExp[] | Function; +export type TelegrafEntityEntity = + | string + | string[] + | RegExp + | RegExp[] + | Function; + +export interface TelegrafEntityMetadata { + entity: TelegrafEntityEntity; +} /** * Entity handling. @@ -9,6 +18,6 @@ export type Entity = string | string[] | RegExp | RegExp[] | Function; * * https://telegraf.js.org/#/?id=entity */ -export function TelegrafEntity(entity: Entity): MethodDecorator { +export function TelegrafEntity(entity: TelegrafEntityEntity): MethodDecorator { return SetMetadata(DECORATORS.ENTITY, { entity }); } diff --git a/lib/decorators/telegraf-hashtag.decorator.ts b/lib/decorators/telegraf-hashtag.decorator.ts index 7ab3942..3265213 100644 --- a/lib/decorators/telegraf-hashtag.decorator.ts +++ b/lib/decorators/telegraf-hashtag.decorator.ts @@ -1,12 +1,20 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; +export type TelegrafHashtagHashtag = string | string[]; + +export interface TelegrafHashtagMetadata { + hashtag: TelegrafHashtagHashtag; +} + /** * Hashtag handling. * @param hashtag Hashtag * * https://telegraf.js.org/#/?id=hashtag */ -export function TelegrafHashtag(hashtag: string | string[]): MethodDecorator { +export function TelegrafHashtag( + hashtag: TelegrafHashtagHashtag, +): MethodDecorator { return SetMetadata(DECORATORS.HASHTAG, { hashtag }); } diff --git a/lib/decorators/telegraf-hears.decorator.ts b/lib/decorators/telegraf-hears.decorator.ts index b9c44fb..17b8d1b 100644 --- a/lib/decorators/telegraf-hears.decorator.ts +++ b/lib/decorators/telegraf-hears.decorator.ts @@ -2,12 +2,20 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; import { HearsTriggers } from 'telegraf'; +export type TelegrafHearsTriggers = HearsTriggers; + +export interface TelegrafHearsMetadata { + triggers: TelegrafHearsTriggers; +} + /** * Registers middleware for handling text messages. * @param triggers Triggers * * https://telegraf.js.org/#/?id=hears */ -export function TelegrafHears(triggers: HearsTriggers): MethodDecorator { +export function TelegrafHears( + triggers: TelegrafHearsTriggers, +): MethodDecorator { return SetMetadata(DECORATORS.HEARS, { triggers: triggers }); } diff --git a/lib/decorators/telegraf-inline-query.decorator.ts b/lib/decorators/telegraf-inline-query.decorator.ts index 2f3b6e6..1a4cc4f 100644 --- a/lib/decorators/telegraf-inline-query.decorator.ts +++ b/lib/decorators/telegraf-inline-query.decorator.ts @@ -1,7 +1,11 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; -export type Triggers = string | string[] | RegExp | RegExp[]; +export type TelegrafInlineQueryTriggers = string | string[] | RegExp | RegExp[]; + +export interface TelegrafInlineQueryMetadata { + triggers: TelegrafInlineQueryTriggers; +} /** * Registers middleware for handling inline_query actions with regular expressions. @@ -9,6 +13,8 @@ export type Triggers = string | string[] | RegExp | RegExp[]; * * https://telegraf.js.org/#/?id=inlinequery */ -export function TelegrafInlineQuery(triggers: Triggers): MethodDecorator { +export function TelegrafInlineQuery( + triggers: TelegrafInlineQueryTriggers, +): MethodDecorator { return SetMetadata(DECORATORS.INLINE_QUERY, { triggers }); } diff --git a/lib/decorators/telegraf-mention.decorator.ts b/lib/decorators/telegraf-mention.decorator.ts index e7a643b..90dbc6b 100644 --- a/lib/decorators/telegraf-mention.decorator.ts +++ b/lib/decorators/telegraf-mention.decorator.ts @@ -1,12 +1,20 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; +export type TelegrafMentionUsername = string | string[]; + +export interface TelegrafMentionMetadata { + username: TelegrafMentionUsername; +} + /** * Mention handling. * @param username Username * * https://telegraf.js.org/#/?id=mention */ -export function TelegrafMention(username: string | string[]): MethodDecorator { +export function TelegrafMention( + username: TelegrafMentionUsername, +): MethodDecorator { return SetMetadata(DECORATORS.MENTION, { username }); } diff --git a/lib/decorators/telegraf-on.decorator.ts b/lib/decorators/telegraf-on.decorator.ts index b6a2a60..4fe3ac0 100644 --- a/lib/decorators/telegraf-on.decorator.ts +++ b/lib/decorators/telegraf-on.decorator.ts @@ -2,6 +2,16 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types'; +export type TelegrafOnUpdateTypes = + | UpdateType + | UpdateType[] + | MessageSubTypes + | MessageSubTypes[]; + +export interface TelegrafOnMetadata { + updateTypes: TelegrafOnUpdateTypes; +} + /** * Registers middleware for provided update type. * @param updateTypes Update type @@ -9,7 +19,7 @@ import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types'; * https://telegraf.js.org/#/?id=on */ export function TelegrafOn( - updateTypes: UpdateType | UpdateType[] | MessageSubTypes | MessageSubTypes[], + updateTypes: TelegrafOnUpdateTypes, ): MethodDecorator { return SetMetadata(DECORATORS.ON, { updateTypes: updateTypes }); } diff --git a/lib/decorators/telegraf-phone.decorator.ts b/lib/decorators/telegraf-phone.decorator.ts index 05ee3e9..8182150 100644 --- a/lib/decorators/telegraf-phone.decorator.ts +++ b/lib/decorators/telegraf-phone.decorator.ts @@ -1,12 +1,18 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; +export type TelegrafPhonePhone = string | string[]; + +export interface TelegrafPhoneMetadata { + phone: TelegrafPhonePhone; +} + /** * Phone number handling. * @param phone Phone number * * https://telegraf.js.org/#/?id=phone */ -export function TelegrafPhone(phone: string | string[]): MethodDecorator { +export function TelegrafPhone(phone: TelegrafPhonePhone): MethodDecorator { return SetMetadata(DECORATORS.PHONE, { phone }); } diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index b4da7bc..b35b8cf 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -1,6 +1,18 @@ import { Injectable, Type } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; -import { TelegrafStart } from './decorators'; +import { + TelegrafActionMetadata, + TelegrafCashtagMetadata, + TelegrafCommandMetadata, + TelegrafEntityMetadata, + TelegrafHashtagMetadata, + TelegrafHearsMetadata, + TelegrafInlineQueryMetadata, + TelegrafMentionMetadata, + TelegrafOnMetadata, + TelegrafPhoneMetadata, + TelegrafStart, +} from './decorators'; import { DECORATORS } from './telegraf.constants'; @Injectable() @@ -21,7 +33,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.ON, target); } - getTelegrafOnMetadata(target: Type | Function) { + getTelegrafOnMetadata( + target: Type | Function, + ): TelegrafOnMetadata | undefined { return this.reflector.get(DECORATORS.ON, target); } @@ -32,7 +46,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.HEARS, target); } - getTelegrafHearsMetadata(target: Type | Function) { + getTelegrafHearsMetadata( + target: Type | Function, + ): TelegrafHearsMetadata | undefined { return this.reflector.get(DECORATORS.HEARS, target); } @@ -43,7 +59,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.COMMAND, target); } - getTelegrafCommandMetadata(target: Type | Function) { + getTelegrafCommandMetadata( + target: Type | Function, + ): TelegrafCommandMetadata | undefined { return this.reflector.get(DECORATORS.COMMAND, target); } @@ -75,7 +93,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.ENTITY, target); } - getTelegrafEntityMetadata(target: Type | Function) { + getTelegrafEntityMetadata( + target: Type | Function, + ): TelegrafEntityMetadata | undefined { return this.reflector.get(DECORATORS.ENTITY, target); } @@ -86,7 +106,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.MENTION, target); } - getTelegrafMentionMetadata(target: Type | Function) { + getTelegrafMentionMetadata( + target: Type | Function, + ): TelegrafMentionMetadata | undefined { return this.reflector.get(DECORATORS.MENTION, target); } @@ -97,7 +119,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.PHONE, target); } - getTelegrafPhoneMetadata(target: Type | Function) { + getTelegrafPhoneMetadata( + target: Type | Function, + ): TelegrafPhoneMetadata | undefined { return this.reflector.get(DECORATORS.PHONE, target); } @@ -108,7 +132,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.HASHTAG, target); } - getTelegrafHashtagMetadata(target: Type | Function) { + getTelegrafHashtagMetadata( + target: Type | Function, + ): TelegrafHashtagMetadata | undefined { return this.reflector.get(DECORATORS.HASHTAG, target); } @@ -119,7 +145,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.CASHTAG, target); } - getTelegrafCashtagMetadata(target: Type | Function) { + getTelegrafCashtagMetadata( + target: Type | Function, + ): TelegrafCashtagMetadata | undefined { return this.reflector.get(DECORATORS.CASHTAG, target); } @@ -130,7 +158,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.ACTION, target); } - getTelegrafActionMetadata(target: Type | Function) { + getTelegrafActionMetadata( + target: Type | Function, + ): TelegrafActionMetadata | undefined { return this.reflector.get(DECORATORS.ACTION, target); } @@ -141,7 +171,9 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.INLINE_QUERY, target); } - getTelegrafInlineQueryMetadata(target: Type | Function) { + getTelegrafInlineQueryMetadata( + target: Type | Function, + ): TelegrafInlineQueryMetadata | undefined { return this.reflector.get(DECORATORS.INLINE_QUERY, target); } diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 26af964..503d68b 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -7,6 +7,18 @@ import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; import { TelegrafProvider } from './telegraf.provider'; import { TELEGRAF_PROVIDER } from './telegraf.constants'; import { ContextMessageUpdate } from 'telegraf'; +import { + TelegrafActionMetadata, + TelegrafCashtagMetadata, + TelegrafCommandMetadata, + TelegrafEntityMetadata, + TelegrafHashtagMetadata, + TelegrafHearsMetadata, + TelegrafInlineQueryMetadata, + TelegrafMentionMetadata, + TelegrafOnMetadata, + TelegrafPhoneMetadata, +} from './decorators'; @Injectable() export class TelegrafExplorer implements OnModuleInit { @@ -119,7 +131,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafOnMetadata, ) { telegraf.on(metadata.updateTypes, instance[key].bind(instance)); } @@ -128,7 +140,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafHearsMetadata, ) { telegraf.hears(metadata.triggers, instance[key].bind(instance)); } @@ -137,7 +149,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafCommandMetadata, ) { telegraf.command(metadata.commands, instance[key].bind(instance)); } @@ -171,7 +183,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafEntityMetadata, ) { // @ts-ignore telegraf.entity(metadata.entity, instance[key].bind(instance)); @@ -181,7 +193,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafMentionMetadata, ) { // @ts-ignore telegraf.mention(metadata.username, instance[key].bind(instance)); @@ -191,7 +203,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafPhoneMetadata, ) { // @ts-ignore telegraf.phone(metadata.phone, instance[key].bind(instance)); @@ -201,7 +213,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafHashtagMetadata, ) { // @ts-ignore telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); @@ -211,7 +223,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafCashtagMetadata, ) { // @ts-ignore telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); @@ -221,7 +233,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafActionMetadata, ) { telegraf.action(metadata.triggers, instance[key].bind(instance)); } @@ -230,7 +242,7 @@ export class TelegrafExplorer implements OnModuleInit { instance: object, key: string, telegraf: Telegraf, - metadata: any, + metadata: TelegrafInlineQueryMetadata, ) { // @ts-ignore telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance)); diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts index 4f16c60..b13626f 100644 --- a/lib/telegraf.provider.ts +++ b/lib/telegraf.provider.ts @@ -3,6 +3,7 @@ import { Inject, OnApplicationBootstrap, Logger, + OnApplicationShutdown, } from '@nestjs/common'; import Telegraf, { ContextMessageUpdate } from 'telegraf'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; @@ -12,7 +13,7 @@ import { TelegrafModuleOptions } from './interfaces'; // @ts-ignore export class TelegrafProvider extends Telegraf - implements OnApplicationBootstrap { + implements OnApplicationBootstrap, OnApplicationShutdown { private logger = new Logger('Telegraf'); constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) { @@ -25,4 +26,8 @@ export class TelegrafProvider }); this.startPolling(); } + + async onApplicationShutdown(signal?: string) { + await this.stop(); + } }