From 5cf452784f7e4b99466ab92b4a245a6de76dbbab Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Tue, 17 Mar 2020 09:47:30 +0300 Subject: [PATCH 01/23] chore: new prettier config --- .prettierrc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.prettierrc b/.prettierrc index eaaa46c..e9c0f50 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,4 @@ { - "trailingComma": "es5", - "singleQuote": true, - "semi": false + "trailingComma": "none", + "singleQuote": true } From 9fedcb964fe1b836b81133b0556188ea57a5668f Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:21:35 +0300 Subject: [PATCH 02/23] feat: complete rewrite --- .prettierrc | 2 +- lib/decorators/index.ts | 6 +- lib/decorators/pipe-context.decorator.ts | 18 -- lib/decorators/telegraf-hears.decorator.ts | 13 ++ lib/decorators/telegraf-on.decorator.ts | 15 ++ lib/decorators/telegraf-start.decorator.ts | 11 ++ .../telegram-action-handler.decorator.ts | 44 ----- lib/decorators/telegram-catch.decorator.ts | 18 -- lib/exeptions/index.ts | 1 - .../invalid-configuration.exeption.ts | 10 - lib/index.ts | 8 +- .../context-transformer.interface.ts | 5 - lib/interfaces/handle-parameters.interface.ts | 18 -- lib/interfaces/handler.interface.ts | 6 - lib/interfaces/index.ts | 6 +- lib/interfaces/telegraf-options.interface.ts | 19 +- .../telegram-error-handler.interface.ts | 5 - lib/telegraf-core.module.ts | 83 ++++++++ lib/telegraf-metadata.accessor.ts | 38 ++++ lib/telegraf-telegram.service.ts | 13 -- lib/telegraf.constants.ts | 11 +- lib/telegraf.explorer.ts | 85 ++++++++ lib/telegraf.module.ts | 58 +----- lib/telegraf.provider.ts | 28 +++ lib/telegraf.service.ts | 183 ------------------ 25 files changed, 309 insertions(+), 395 deletions(-) delete mode 100644 lib/decorators/pipe-context.decorator.ts create mode 100644 lib/decorators/telegraf-hears.decorator.ts create mode 100644 lib/decorators/telegraf-on.decorator.ts create mode 100644 lib/decorators/telegraf-start.decorator.ts delete mode 100644 lib/decorators/telegram-action-handler.decorator.ts delete mode 100644 lib/decorators/telegram-catch.decorator.ts delete mode 100644 lib/exeptions/index.ts delete mode 100644 lib/exeptions/invalid-configuration.exeption.ts delete mode 100644 lib/interfaces/context-transformer.interface.ts delete mode 100644 lib/interfaces/handle-parameters.interface.ts delete mode 100644 lib/interfaces/handler.interface.ts delete mode 100644 lib/interfaces/telegram-error-handler.interface.ts create mode 100644 lib/telegraf-core.module.ts create mode 100644 lib/telegraf-metadata.accessor.ts delete mode 100644 lib/telegraf-telegram.service.ts create mode 100644 lib/telegraf.explorer.ts create mode 100644 lib/telegraf.provider.ts delete mode 100644 lib/telegraf.service.ts diff --git a/.prettierrc b/.prettierrc index e9c0f50..6e778b4 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,4 @@ { - "trailingComma": "none", + "trailingComma": "all", "singleQuote": true } diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 246b998..d001649 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -1,3 +1,3 @@ -export * from './pipe-context.decorator' -export * from './telegram-action-handler.decorator' -export * from './telegram-catch.decorator' +export * from './telegraf-on.decorator'; +export * from './telegraf-hears.decorator'; +export * from './telegraf-start.decorator'; diff --git a/lib/decorators/pipe-context.decorator.ts b/lib/decorators/pipe-context.decorator.ts deleted file mode 100644 index 01aeec0..0000000 --- a/lib/decorators/pipe-context.decorator.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Type } from '@nestjs/common' -import { ContextTransformer } from '../interfaces' -import { addHandlerToStore } from './' - -export const PipeContext = (transform: Type>) => ( - target: Object, - propertyKey: string, - parameterIndex: number, -) => { - addHandlerToStore(target, propertyKey, { - transformations: [ - { - index: parameterIndex, - transform, - }, - ], - }) -} diff --git a/lib/decorators/telegraf-hears.decorator.ts b/lib/decorators/telegraf-hears.decorator.ts new file mode 100644 index 0000000..b9c44fb --- /dev/null +++ b/lib/decorators/telegraf-hears.decorator.ts @@ -0,0 +1,13 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; +import { HearsTriggers } from 'telegraf'; + +/** + * Registers middleware for handling text messages. + * @param triggers Triggers + * + * https://telegraf.js.org/#/?id=hears + */ +export function TelegrafHears(triggers: HearsTriggers): MethodDecorator { + return SetMetadata(DECORATORS.HEARS, { triggers: triggers }); +} diff --git a/lib/decorators/telegraf-on.decorator.ts b/lib/decorators/telegraf-on.decorator.ts new file mode 100644 index 0000000..b6a2a60 --- /dev/null +++ b/lib/decorators/telegraf-on.decorator.ts @@ -0,0 +1,15 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; +import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types'; + +/** + * Registers middleware for provided update type. + * @param updateTypes Update type + * + * https://telegraf.js.org/#/?id=on + */ +export function TelegrafOn( + updateTypes: UpdateType | UpdateType[] | MessageSubTypes | MessageSubTypes[], +): MethodDecorator { + return SetMetadata(DECORATORS.ON, { updateTypes: updateTypes }); +} diff --git a/lib/decorators/telegraf-start.decorator.ts b/lib/decorators/telegraf-start.decorator.ts new file mode 100644 index 0000000..3e0c820 --- /dev/null +++ b/lib/decorators/telegraf-start.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Handler for /start command. + * + * https://telegraf.js.org/#/?id=start + */ +export function TelegrafStart(): MethodDecorator { + return SetMetadata(DECORATORS.START, {}); +} diff --git a/lib/decorators/telegram-action-handler.decorator.ts b/lib/decorators/telegram-action-handler.decorator.ts deleted file mode 100644 index b19a619..0000000 --- a/lib/decorators/telegram-action-handler.decorator.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { HandleParameters } from '../interfaces' - -type Decorator = ( - params: HandleParameters, -) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void - -type HandlerDecorator = Decorator & { - handlers?: Map> -} - -export const TelegramActionHandler: HandlerDecorator = ( - parameters: HandleParameters, -) => (target: any, propertyKey: string) => { - // eslint-disable-next-line no-use-before-define - addHandlerToStore(target, propertyKey, parameters) -} - -export const addHandlerToStore = ( - instance: Object, - name: string, - config: HandleParameters, -) => { - const handlerClass = instance.constructor - - if (!TelegramActionHandler.handlers) { - TelegramActionHandler.handlers = new Map() - } - - if (!TelegramActionHandler.handlers.get(handlerClass)) { - TelegramActionHandler.handlers.set(handlerClass, new Map()) - } - - const oldParameters = - TelegramActionHandler.handlers.get(handlerClass).get(name) || {} - - TelegramActionHandler.handlers.get(handlerClass).set(name, { - ...oldParameters, - ...config, - transformations: [ - ...(oldParameters.transformations || []), - ...(config.transformations || []), - ], - }) -} diff --git a/lib/decorators/telegram-catch.decorator.ts b/lib/decorators/telegram-catch.decorator.ts deleted file mode 100644 index f924ea9..0000000 --- a/lib/decorators/telegram-catch.decorator.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Type } from '@nestjs/common' -import { TelegramErrorHandler } from '../interfaces' - -type Decorator = (error: any) => ClassDecorator - -type HandlerDecorator = Decorator & { - handlers?: Map> -} - -export const TelegramCatch: HandlerDecorator = error => target => { - if (!TelegramCatch.handlers) { - TelegramCatch.handlers = new Map() - } - - TelegramCatch.handlers.set(error, target as any) - - return target -} diff --git a/lib/exeptions/index.ts b/lib/exeptions/index.ts deleted file mode 100644 index 40f3902..0000000 --- a/lib/exeptions/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './invalid-configuration.exeption' diff --git a/lib/exeptions/invalid-configuration.exeption.ts b/lib/exeptions/invalid-configuration.exeption.ts deleted file mode 100644 index deddd73..0000000 --- a/lib/exeptions/invalid-configuration.exeption.ts +++ /dev/null @@ -1,10 +0,0 @@ -export class InvalidConfigurationException extends Error { - public constructor( - public readonly invalidField, - public readonly invalidCause, - ) { - super( - `Options validation failed, "${invalidField}" invalid — ${invalidCause}`, - ) - } -} diff --git a/lib/index.ts b/lib/index.ts index d7b43b6..6ca2523 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,5 +1,3 @@ -export * from './telegraf.module' -export * from './interfaces' -export * from './decorators' -export * from './telegraf.service' -export * from './telegraf-telegram.service' +export * from './telegraf.module'; +export * from './interfaces'; +export * from './decorators'; diff --git a/lib/interfaces/context-transformer.interface.ts b/lib/interfaces/context-transformer.interface.ts deleted file mode 100644 index 61422ac..0000000 --- a/lib/interfaces/context-transformer.interface.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { ContextMessageUpdate } from 'telegraf' - -export interface ContextTransformer { - transform: (ctx: ContextMessageUpdate) => Promise -} diff --git a/lib/interfaces/handle-parameters.interface.ts b/lib/interfaces/handle-parameters.interface.ts deleted file mode 100644 index 561953c..0000000 --- a/lib/interfaces/handle-parameters.interface.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { ContextTransformer } from './' -import { HearsTriggers } from 'telegraf' -import { UpdateType, MessageSubTypes } from 'telegraf/typings/telegram-types' -import { Type } from '@nestjs/common' - -interface ArgumentTransformation { - index: number - transform: Type -} - -export interface HandleParameters { - onStart?: boolean - on?: UpdateType | UpdateType[] | MessageSubTypes | MessageSubTypes[] - command?: string - message?: string | RegExp - action?: HearsTriggers - transformations?: ArgumentTransformation[] -} diff --git a/lib/interfaces/handler.interface.ts b/lib/interfaces/handler.interface.ts deleted file mode 100644 index ac61f9b..0000000 --- a/lib/interfaces/handler.interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { HandleParameters } from './' - -export interface Handler { - handle: (...args: any[]) => Promise - config: HandleParameters -} diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 4f31766..23e7d9f 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -1,5 +1 @@ -export * from './telegraf-options.interface' -export * from './handler.interface' -export * from './handle-parameters.interface' -export * from './telegram-error-handler.interface' -export * from './context-transformer.interface' +export * from './telegraf-options.interface'; diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index 64b70a6..e3b1969 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -1,22 +1,21 @@ -import { ModuleMetadata, Type } from '@nestjs/common/interfaces' -import { TelegrafOptions } from 'telegraf' +import { ModuleMetadata, Type } from '@nestjs/common/interfaces'; +import { TelegrafOptions } from 'telegraf'; export interface TelegrafModuleOptions { - token: string - sitePublicUrl?: string - telegrafOptions?: TelegrafOptions + token: string; + options?: TelegrafOptions; } export interface TelegrafOptionsFactory { - createTelegrafOptions(): TelegrafModuleOptions + createTelegrafOptions(): TelegrafModuleOptions; } export interface TelegrafModuleAsyncOptions extends Pick { - useExisting?: Type - useClass?: Type + useExisting?: Type; + useClass?: Type; useFactory?: ( ...args: any[] - ) => Promise | TelegrafModuleOptions - inject?: any[] + ) => Promise | TelegrafModuleOptions; + inject?: any[]; } diff --git a/lib/interfaces/telegram-error-handler.interface.ts b/lib/interfaces/telegram-error-handler.interface.ts deleted file mode 100644 index 893f309..0000000 --- a/lib/interfaces/telegram-error-handler.interface.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { ContextMessageUpdate } from 'telegraf' - -export interface TelegramErrorHandler { - catch(ctx: ContextMessageUpdate, error: E): Promise -} diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts new file mode 100644 index 0000000..0e45913 --- /dev/null +++ b/lib/telegraf-core.module.ts @@ -0,0 +1,83 @@ +import { Module, DynamicModule, Provider, Type } from '@nestjs/common'; +import { + TelegrafModuleOptions, + TelegrafModuleAsyncOptions, + TelegrafOptionsFactory, +} from './interfaces'; +import { + TELEGRAF_MODULE_OPTIONS, + TELEGRAF_PROVIDER, +} from './telegraf.constants'; +import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; +import { TelegrafExplorer } from './telegraf.explorer'; +import { DiscoveryModule } from '@nestjs/core'; +import { TelegrafProvider } from './telegraf.provider'; + +@Module({ + imports: [DiscoveryModule], + providers: [TelegrafMetadataAccessor, TelegrafExplorer], +}) +export class TelegrafCoreModule { + public static forRoot(options: TelegrafModuleOptions): DynamicModule { + return { + module: TelegrafCoreModule, + providers: [], + exports: [], + }; + } + + public static forRootAsync( + options: TelegrafModuleAsyncOptions, + ): DynamicModule { + const telegrafProvider = { + provide: TELEGRAF_PROVIDER, + useClass: TelegrafProvider, + inject: [TELEGRAF_MODULE_OPTIONS], + }; + const asyncProviders = this.createAsyncProviders(options); + return { + module: TelegrafCoreModule, + imports: options.imports, + providers: [...asyncProviders, telegrafProvider], + exports: [telegrafProvider], + }; + } + + private static createAsyncProviders( + options: TelegrafModuleAsyncOptions, + ): Provider[] { + if (options.useExisting || options.useFactory) { + return [this.createAsyncOptionsProvider(options)]; + } + const useClass = options.useClass as Type; + return [ + this.createAsyncOptionsProvider(options), + { + provide: useClass, + useClass, + }, + ]; + } + + private static createAsyncOptionsProvider( + options: TelegrafModuleAsyncOptions, + ): Provider { + if (options.useFactory) { + return { + provide: TELEGRAF_MODULE_OPTIONS, + useFactory: options.useFactory, + inject: options.inject || [], + }; + } + // `as Type` is a workaround for microsoft/TypeScript#31603 + const inject = [ + (options.useClass || options.useExisting) as Type, + ]; + return { + provide: TELEGRAF_MODULE_OPTIONS, + useFactory: async (optionsFactory: TelegrafOptionsFactory) => + await optionsFactory.createTelegrafOptions(), + inject, + }; + } +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts new file mode 100644 index 0000000..d4bc776 --- /dev/null +++ b/lib/telegraf-metadata.accessor.ts @@ -0,0 +1,38 @@ +import { Injectable, Type } from '@nestjs/common'; +import { Reflector } from '@nestjs/core'; +import { TelegrafStart } from './decorators'; +import { DECORATORS } from './telegraf.constants'; + +@Injectable() +export class TelegrafMetadataAccessor { + constructor(private readonly reflector: Reflector) {} + + isTelegrafStart(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.START, target); + } + + isTelegrafOn(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.ON, target); + } + + getTelegrafOnMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.ON, target); + } + + isTelegrafHears(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.HEARS, target); + } + + getTelegrafHearsMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.HEARS, target); + } +} diff --git a/lib/telegraf-telegram.service.ts b/lib/telegraf-telegram.service.ts deleted file mode 100644 index 8b262b7..0000000 --- a/lib/telegraf-telegram.service.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Injectable, Inject } from '@nestjs/common'; -const Telegram = require('telegraf/telegram'); -import { Telegram as TelegramClient } from 'telegraf'; - -import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; -import { TelegrafModuleOptions } from './interfaces'; - -@Injectable() -export class TelegrafTelegramService extends TelegramClient { - constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) { - super(options.token, {}); - } -} diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index b62570c..edfedfd 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1,2 +1,9 @@ -export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS' -export const TokenInjectionToken = Symbol('TokenInjectionToken') +export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; +export const TELEGRAF_PROVIDER = 'TELEGRAF_PROVIDER'; + +export const DECORATORS_PREFIX = 'TELEGRAF'; +export const DECORATORS = { + ON: `${DECORATORS_PREFIX}/ON`, + HEARS: `${DECORATORS_PREFIX}/HEARS`, + START: `${DECORATORS_PREFIX}/START`, +}; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts new file mode 100644 index 0000000..ebe0405 --- /dev/null +++ b/lib/telegraf.explorer.ts @@ -0,0 +1,85 @@ +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 Telegraf from 'telegraf'; +import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; +import { TelegrafProvider } from './telegraf.provider'; +import { TELEGRAF_PROVIDER } from './telegraf.constants'; +import { ContextMessageUpdate } from 'telegraf'; + +@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; + } + + const telegraf = this.moduleRef.get>( + TELEGRAF_PROVIDER, + { strict: false }, + ); + + this.metadataScanner.scanFromPrototype( + instance, + Object.getPrototypeOf(instance), + (key: string) => { + if (this.metadataAccessor.isTelegrafStart(instance[key])) { + this.handleTelegrafStart(instance, key, telegraf); + } 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); + } + }, + ); + }); + } + + handleTelegrafOn( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + telegraf.on(metadata.updateTypes, instance[key].bind(instance)); + } + + handleTelegrafStart( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.start(instance[key].bind(instance)); + } + + handleTelegrafHears( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + telegraf.hears(metadata.triggers, instance[key].bind(instance)); + } +} diff --git a/lib/telegraf.module.ts b/lib/telegraf.module.ts index 9bf8840..a0c34de 100644 --- a/lib/telegraf.module.ts +++ b/lib/telegraf.module.ts @@ -1,63 +1,25 @@ -import { Module, DynamicModule, Provider } from '@nestjs/common'; +import { Module, DynamicModule } from '@nestjs/common'; +import { TelegrafCoreModule } from './telegraf-core.module'; import { + TelegrafModuleOptions, TelegrafModuleAsyncOptions, - TelegrafOptionsFactory } from './interfaces'; -import { - TELEGRAF_MODULE_OPTIONS, - TokenInjectionToken -} from './telegraf.constants'; -import { TelegrafService, TelegrafTelegramService } from './'; @Module({}) export class TelegrafModule { - static fromFactory(options: TelegrafModuleAsyncOptions): DynamicModule { + public static forRoot(options?: TelegrafModuleOptions): DynamicModule { return { module: TelegrafModule, - imports: options.imports || [], - providers: [ - ...this.createAsyncProviders(options), - TelegrafService, - TelegrafTelegramService, - { - provide: TokenInjectionToken, - useClass: options.useClass - } - ], - exports: [TelegrafService, TelegrafTelegramService] + imports: [TelegrafCoreModule.forRoot(options)], }; } - private static createAsyncProviders( - options: TelegrafModuleAsyncOptions - ): Provider[] { - if (options.useExisting || options.useFactory) { - return [this.createAsyncOptionsProvider(options)]; - } - return [ - this.createAsyncOptionsProvider(options), - { - provide: options.useClass, - useClass: options.useClass - } - ]; - } - - private static createAsyncOptionsProvider( - options: TelegrafModuleAsyncOptions - ): Provider { - if (options.useFactory) { - return { - provide: TELEGRAF_MODULE_OPTIONS, - useFactory: options.useFactory, - inject: options.inject || [] - }; - } + public static forRootAsync( + options: TelegrafModuleAsyncOptions, + ): DynamicModule { return { - provide: TELEGRAF_MODULE_OPTIONS, - useFactory: async (optionsFactory: TelegrafOptionsFactory) => - await optionsFactory.createTelegrafOptions(), - inject: [options.useExisting || options.useClass] + module: TelegrafModule, + imports: [TelegrafCoreModule.forRootAsync(options)], }; } } diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts new file mode 100644 index 0000000..4f16c60 --- /dev/null +++ b/lib/telegraf.provider.ts @@ -0,0 +1,28 @@ +import { + Injectable, + Inject, + OnApplicationBootstrap, + Logger, +} from '@nestjs/common'; +import Telegraf, { ContextMessageUpdate } from 'telegraf'; +import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; +import { TelegrafModuleOptions } from './interfaces'; + +@Injectable() +// @ts-ignore +export class TelegrafProvider + extends Telegraf + implements OnApplicationBootstrap { + private logger = new Logger('Telegraf'); + + constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) { + super(options.token, options.options); + } + + onApplicationBootstrap() { + this.catch(e => { + this.logger.error(e); + }); + this.startPolling(); + } +} diff --git a/lib/telegraf.service.ts b/lib/telegraf.service.ts deleted file mode 100644 index 5ba6e8a..0000000 --- a/lib/telegraf.service.ts +++ /dev/null @@ -1,183 +0,0 @@ -import { Inject, Injectable, Logger } from '@nestjs/common' -import { ModuleRef } from '@nestjs/core' -import { flatten, head } from 'lodash' -import Telegraf, { ContextMessageUpdate } from 'telegraf' -import { TelegramActionHandler, TelegramCatch } from './decorators' -import { InvalidConfigurationException } from './exeptions' -import { - ContextTransformer, - Handler, - TelegrafOptionsFactory, - TelegramErrorHandler, -} from './interfaces' -import { TokenInjectionToken } from './telegraf.constants' - -@Injectable() -export class TelegrafService { - private readonly logger = new Logger(TelegrafService.name, true) - private readonly sitePublicUrl?: string - public readonly bot: Telegraf - private ref: ModuleRef - - public constructor( - @Inject(TokenInjectionToken) options: TelegrafOptionsFactory - ) { - const { - token, - sitePublicUrl, - telegrafOptions, - } = options.createTelegrafOptions() - this.sitePublicUrl = sitePublicUrl - this.bot = new Telegraf(token, telegrafOptions) - } - - public init(ref: ModuleRef, devMode: boolean = false) { - this.ref = ref - - const handlers = this.createHandlers() - - this.setupOnStart(handlers) - this.setupOn(handlers) - this.setupOnMessage(handlers) - this.setupOnCommand(handlers) - this.setupActions(handlers) - - if (devMode) { - this.startPolling() - } - } - - public getMiddleware(path: string) { - if (!this.sitePublicUrl) { - throw new InvalidConfigurationException( - 'sitePublicUrl', - 'does not exist, but webook used' - ) - } - - const url = `${this.sitePublicUrl}/${path}` - - this.bot.telegram - .setWebhook(url) - .then(() => this.logger.log(`Webhook set success @ ${url}`)) - - return this.bot.webhookCallback(`/${path}`) - } - - public startPolling() { - this.bot.telegram.deleteWebhook().then( - () => this.bot.startPolling(), - () => { - // okay, never mind - } - ) - } - - private createHandlers(): Handler[] { - return flatten( - Array.from((TelegramActionHandler.handlers || new Map()).entries()).map( - ([handlerClass, classConfig]) => { - const handlerInstance = this.ref.get(handlerClass, { strict: false }) - - return Array.from(classConfig.entries()).map( - ([methodName, methodCondig]) => ({ - handle: handlerInstance[methodName].bind(handlerInstance), - config: methodCondig, - }) - ) - } - ) - ) - } - - private setupOnStart(handlers: Handler[]): void { - const onStart = handlers.filter(({ config }) => config.onStart) - - if (onStart.length !== 1) { - throw new Error() - } - - this.bot.start(this.adoptHandle(head(onStart))) - } - - private setupOn(handlers: Handler[]): void { - const onHandlers = handlers.filter(({ config }) => config.on) - - onHandlers.forEach(handler => { - this.bot.on(handler.config.on, this.adoptHandle(handler)) - }) - } - - private setupOnMessage(handlers: Handler[]): void { - const onMessageHandlers = handlers.filter( - ({ config }) => config.message !== undefined - ) - - onMessageHandlers.forEach(handler => { - if (handler.config.message) { - this.bot.hears(handler.config.message, this.adoptHandle(handler)) - } else { - this.bot.on('message', this.adoptHandle(handler)) - } - }) - } - - private setupOnCommand(handlers: Handler[]): void { - const commandHandlers = handlers.filter(({ config }) => config.command) - - commandHandlers.forEach(handler => { - this.bot.command(handler.config.command, this.adoptHandle(handler)) - }) - } - - private setupActions(handlers: Handler[]): void { - const commandHandlers = handlers.filter(({ config }) => config.action) - - commandHandlers.forEach(handler => { - this.bot.action(handler.config.action, this.adoptHandle(handler)) - }) - } - - private adoptHandle({ handle, config }: Handler) { - const errorHandler = this.createCatch() - - return async (ctx: ContextMessageUpdate) => { - const args = await Promise.all( - (config.transformations || []) - .sort((a, b) => a.index - b.index) - .map(({ transform }) => - this.ref - .get(transform, { strict: false }) - .transform(ctx) - ) - ) - - return handle(ctx, ...args).catch(errorHandler(ctx)) - } - } - - private createCatch() { - const handlers = Array.from( - (TelegramCatch.handlers || new Map()).entries() - ).map(([errorType, handlerType]) => { - const handler = this.ref.get(handlerType, { - strict: false, - }) - - return { - errorType, - handler, - } - }) - - return (ctx: ContextMessageUpdate) => (e: any) => { - for (const { errorType, handler } of handlers) { - if (e instanceof (errorType as any)) { - return handler.catch(ctx, e) - } - } - - throw e - } - } -} From 6fddfd26f0d8725586b215c44d05365c237fba3b Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:34:44 +0300 Subject: [PATCH 03/23] feat: TelegrafUse decorator added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-use.decorator.ts | 11 +++++++++++ lib/telegraf-metadata.accessor.ts | 7 +++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 12 +++++++++++- 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/decorators/telegraf-use.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index d001649..42c00a1 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -1,3 +1,4 @@ +export * from './telegraf-use.decorator'; export * from './telegraf-on.decorator'; export * from './telegraf-hears.decorator'; export * from './telegraf-start.decorator'; diff --git a/lib/decorators/telegraf-use.decorator.ts b/lib/decorators/telegraf-use.decorator.ts new file mode 100644 index 0000000..2ff0dd7 --- /dev/null +++ b/lib/decorators/telegraf-use.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Registers a middleware. + * + * https://telegraf.js.org/#/?id=use + */ +export function TelegrafUse(): MethodDecorator { + return SetMetadata(DECORATORS.USE, {}); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index d4bc776..cad0d9b 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -7,6 +7,13 @@ import { DECORATORS } from './telegraf.constants'; export class TelegrafMetadataAccessor { constructor(private readonly reflector: Reflector) {} + isTelegrafUse(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.USE, target); + } + isTelegrafStart(target: Type | Function): boolean { if (!target) { return false; diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index edfedfd..d68f1aa 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -3,6 +3,7 @@ export const TELEGRAF_PROVIDER = 'TELEGRAF_PROVIDER'; export const DECORATORS_PREFIX = 'TELEGRAF'; export const DECORATORS = { + USE: `${DECORATORS_PREFIX}/USE`, ON: `${DECORATORS_PREFIX}/ON`, HEARS: `${DECORATORS_PREFIX}/HEARS`, START: `${DECORATORS_PREFIX}/START`, diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index ebe0405..feda0f0 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -39,7 +39,9 @@ export class TelegrafExplorer implements OnModuleInit { instance, Object.getPrototypeOf(instance), (key: string) => { - if (this.metadataAccessor.isTelegrafStart(instance[key])) { + if (this.metadataAccessor.isTelegrafUse(instance[key])) { + this.handleTelegrafUse(instance, key, telegraf); + } else if (this.metadataAccessor.isTelegrafStart(instance[key])) { this.handleTelegrafStart(instance, key, telegraf); } else if (this.metadataAccessor.isTelegrafOn(instance[key])) { const metadata = this.metadataAccessor.getTelegrafOnMetadata( @@ -57,6 +59,14 @@ export class TelegrafExplorer implements OnModuleInit { }); } + handleTelegrafUse( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.use(instance[key].bind(instance)); + } + handleTelegrafOn( instance: object, key: string, From c0ffdeff3498f801d0cf1e571c014cc30bfbdb40 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:39:47 +0300 Subject: [PATCH 04/23] feat(decorators): TelegrafCommand added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-command.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 14 ++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 lib/decorators/telegraf-command.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 42c00a1..c94e0a3 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -1,4 +1,5 @@ export * from './telegraf-use.decorator'; export * from './telegraf-on.decorator'; export * from './telegraf-hears.decorator'; +export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; diff --git a/lib/decorators/telegraf-command.decorator.ts b/lib/decorators/telegraf-command.decorator.ts new file mode 100644 index 0000000..bb20866 --- /dev/null +++ b/lib/decorators/telegraf-command.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Command handling. + * @param commands Commands + * + * https://telegraf.js.org/#/?id=command + */ +export function TelegrafCommand(commands: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.COMMAND, { commands }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index cad0d9b..d419ece 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -42,4 +42,15 @@ export class TelegrafMetadataAccessor { getTelegrafHearsMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.HEARS, target); } + + isTelegrafCommand(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.COMMAND, target); + } + + getTelegrafCommandMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.COMMAND, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index d68f1aa..114821e 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -6,5 +6,6 @@ export const DECORATORS = { USE: `${DECORATORS_PREFIX}/USE`, ON: `${DECORATORS_PREFIX}/ON`, HEARS: `${DECORATORS_PREFIX}/HEARS`, + COMMAND: `${DECORATORS_PREFIX}/COMMAND`, START: `${DECORATORS_PREFIX}/START`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index feda0f0..c2f12cc 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -53,6 +53,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafHears(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafCommand(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafCommandMetadata( + instance[key], + ); + this.handleTelegrafCommand(instance, key, telegraf, metadata); } }, ); @@ -92,4 +97,13 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.hears(metadata.triggers, instance[key].bind(instance)); } + + handleTelegrafCommand( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + telegraf.command(metadata.commands, instance[key].bind(instance)); + } } From fa3a7f52584727ca856786defdd078c585bf76cb Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:44:05 +0300 Subject: [PATCH 05/23] feat(decorators): TelegrafHelp added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-help.decorator.ts | 11 +++++++++ lib/telegraf-metadata.accessor.ts | 21 ++++++++++------ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 30 +++++++++++++++-------- 5 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 lib/decorators/telegraf-help.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index c94e0a3..63c6636 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -3,3 +3,4 @@ export * from './telegraf-on.decorator'; export * from './telegraf-hears.decorator'; export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; +export * from './telegraf-help.decorator'; diff --git a/lib/decorators/telegraf-help.decorator.ts b/lib/decorators/telegraf-help.decorator.ts new file mode 100644 index 0000000..7bade62 --- /dev/null +++ b/lib/decorators/telegraf-help.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Handler for /help command. + * + * https://telegraf.js.org/#/?id=help + */ +export function TelegrafHelp(): MethodDecorator { + return SetMetadata(DECORATORS.HELP, {}); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index d419ece..30ce062 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -14,13 +14,6 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.USE, target); } - isTelegrafStart(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.START, target); - } - isTelegrafOn(target: Type | Function): boolean { if (!target) { return false; @@ -53,4 +46,18 @@ export class TelegrafMetadataAccessor { getTelegrafCommandMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.COMMAND, target); } + + isTelegrafStart(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.START, target); + } + + isTelegrafHelp(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.HELP, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 114821e..78ee19a 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -8,4 +8,5 @@ export const DECORATORS = { HEARS: `${DECORATORS_PREFIX}/HEARS`, COMMAND: `${DECORATORS_PREFIX}/COMMAND`, START: `${DECORATORS_PREFIX}/START`, + HELP: `${DECORATORS_PREFIX}/HELP`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index c2f12cc..2187ffd 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -41,8 +41,6 @@ export class TelegrafExplorer implements OnModuleInit { (key: string) => { if (this.metadataAccessor.isTelegrafUse(instance[key])) { this.handleTelegrafUse(instance, key, telegraf); - } else if (this.metadataAccessor.isTelegrafStart(instance[key])) { - this.handleTelegrafStart(instance, key, telegraf); } else if (this.metadataAccessor.isTelegrafOn(instance[key])) { const metadata = this.metadataAccessor.getTelegrafOnMetadata( instance[key], @@ -58,6 +56,10 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafCommand(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafStart(instance[key])) { + this.handleTelegrafStart(instance, key, telegraf); + } else if (this.metadataAccessor.isTelegrafHelp(instance[key])) { + this.handleTelegrafHelp(instance, key, telegraf); } }, ); @@ -81,14 +83,6 @@ export class TelegrafExplorer implements OnModuleInit { telegraf.on(metadata.updateTypes, instance[key].bind(instance)); } - handleTelegrafStart( - instance: object, - key: string, - telegraf: Telegraf, - ) { - telegraf.start(instance[key].bind(instance)); - } - handleTelegrafHears( instance: object, key: string, @@ -106,4 +100,20 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.command(metadata.commands, instance[key].bind(instance)); } + + handleTelegrafStart( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.start(instance[key].bind(instance)); + } + + handleTelegrafHelp( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.help(instance[key].bind(instance)); + } } From 55c6f939a4cfa56e12238b3493f84d11891d240a Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:46:31 +0300 Subject: [PATCH 06/23] feat(decorators): TelegrafSettings added --- lib/decorators/telegraf-settings.decorator.ts | 11 +++++++++++ lib/telegraf-metadata.accessor.ts | 7 +++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 11 +++++++++++ 4 files changed, 30 insertions(+) create mode 100644 lib/decorators/telegraf-settings.decorator.ts diff --git a/lib/decorators/telegraf-settings.decorator.ts b/lib/decorators/telegraf-settings.decorator.ts new file mode 100644 index 0000000..aed3741 --- /dev/null +++ b/lib/decorators/telegraf-settings.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Handler for /settings command. + * + * https://telegraf.js.org/#/?id=settings + */ +export function TelegrafSettings(): MethodDecorator { + return SetMetadata(DECORATORS.SETTINGS, {}); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 30ce062..7895bce 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -60,4 +60,11 @@ export class TelegrafMetadataAccessor { } return !!this.reflector.get(DECORATORS.HELP, target); } + + isTelegrafSettings(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.SETTINGS, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 78ee19a..e48a9a8 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -9,4 +9,5 @@ export const DECORATORS = { COMMAND: `${DECORATORS_PREFIX}/COMMAND`, START: `${DECORATORS_PREFIX}/START`, HELP: `${DECORATORS_PREFIX}/HELP`, + SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 2187ffd..1528843 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -60,6 +60,8 @@ export class TelegrafExplorer implements OnModuleInit { this.handleTelegrafStart(instance, key, telegraf); } else if (this.metadataAccessor.isTelegrafHelp(instance[key])) { this.handleTelegrafHelp(instance, key, telegraf); + } else if (this.metadataAccessor.isTelegrafSettings(instance[key])) { + this.handleTelegrafSettings(instance, key, telegraf); } }, ); @@ -116,4 +118,13 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.help(instance[key].bind(instance)); } + + handleTelegrafSettings( + instance: object, + key: string, + telegraf: Telegraf, + ) { + // @ts-ignore + telegraf.settings(instance[key].bind(instance)); + } } From 8bd20131a10840a391fc52f5aabe7d04d948acf2 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:54:34 +0300 Subject: [PATCH 07/23] feat(decorators): TelegrafEntity added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-entity.decorator.ts | 14 ++++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 lib/decorators/telegraf-entity.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 63c6636..dd968ab 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -4,3 +4,4 @@ export * from './telegraf-hears.decorator'; export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; export * from './telegraf-help.decorator'; +export * from './telegraf-entity.decorator'; diff --git a/lib/decorators/telegraf-entity.decorator.ts b/lib/decorators/telegraf-entity.decorator.ts new file mode 100644 index 0000000..cf1173d --- /dev/null +++ b/lib/decorators/telegraf-entity.decorator.ts @@ -0,0 +1,14 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +export type Entity = string | string[] | RegExp | RegExp[] | Function; + +/** + * Entity handling. + * @param entity Entity name + * + * https://telegraf.js.org/#/?id=entity + */ +export function TelegrafEntity(entity: Entity): MethodDecorator { + return SetMetadata(DECORATORS.ENTITY, { entity }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 7895bce..34c5f06 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -67,4 +67,15 @@ export class TelegrafMetadataAccessor { } return !!this.reflector.get(DECORATORS.SETTINGS, target); } + + isTelegrafEntity(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.ENTITY, target); + } + + getTelegrafEntityMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.ENTITY, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index e48a9a8..fa18e0a 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -10,4 +10,5 @@ export const DECORATORS = { START: `${DECORATORS_PREFIX}/START`, HELP: `${DECORATORS_PREFIX}/HELP`, SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, + ENTITY: `${DECORATORS_PREFIX}/ENTITY`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 1528843..2665b28 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -62,6 +62,11 @@ export class TelegrafExplorer implements OnModuleInit { this.handleTelegrafHelp(instance, key, telegraf); } else if (this.metadataAccessor.isTelegrafSettings(instance[key])) { this.handleTelegrafSettings(instance, key, telegraf); + } else if (this.metadataAccessor.isTelegrafEntity(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafEntityMetadata( + instance[key], + ); + this.handleTelegrafEntity(instance, key, telegraf, metadata); } }, ); @@ -127,4 +132,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.settings(instance[key].bind(instance)); } + + handleTelegrafEntity( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.entity(metadata.entity, instance[key].bind(instance)); + } } From f5d9cf13b6ef67a1f0a3b63004b2e9a67b94f31c Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:58:27 +0300 Subject: [PATCH 08/23] feat(decorators): TelegrafMention added --- lib/decorators/telegraf-mention.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 lib/decorators/telegraf-mention.decorator.ts diff --git a/lib/decorators/telegraf-mention.decorator.ts b/lib/decorators/telegraf-mention.decorator.ts new file mode 100644 index 0000000..e7a643b --- /dev/null +++ b/lib/decorators/telegraf-mention.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Mention handling. + * @param username Username + * + * https://telegraf.js.org/#/?id=mention + */ +export function TelegrafMention(username: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.MENTION, { username }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 34c5f06..03809fb 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -78,4 +78,15 @@ export class TelegrafMetadataAccessor { getTelegrafEntityMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.ENTITY, target); } + + isTelegrafMention(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.MENTION, target); + } + + getTelegrafMentionMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.MENTION, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index fa18e0a..66c3371 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -11,4 +11,5 @@ export const DECORATORS = { HELP: `${DECORATORS_PREFIX}/HELP`, SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, ENTITY: `${DECORATORS_PREFIX}/ENTITY`, + MENTION: `${DECORATORS_PREFIX}/MENTION`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 2665b28..f30fafd 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -67,6 +67,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafEntity(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafMention(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafMentionMetadata( + instance[key], + ); + this.handleTelegrafMention(instance, key, telegraf, metadata); } }, ); @@ -142,4 +147,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.entity(metadata.entity, instance[key].bind(instance)); } + + handleTelegrafMention( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.mention(metadata.username, instance[key].bind(instance)); + } } From 225c931dbbdcbcb94198323b755b68b85e3acbc6 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:02:37 +0300 Subject: [PATCH 09/23] feat(decorators): TelegrafPhone added --- lib/decorators/index.ts | 3 +++ lib/decorators/telegraf-phone.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 lib/decorators/telegraf-phone.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index dd968ab..759e533 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -4,4 +4,7 @@ export * from './telegraf-hears.decorator'; export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; export * from './telegraf-help.decorator'; +export * from './telegraf-settings.decorator'; export * from './telegraf-entity.decorator'; +export * from './telegraf-mention.decorator'; +export * from './telegraf-phone.decorator'; diff --git a/lib/decorators/telegraf-phone.decorator.ts b/lib/decorators/telegraf-phone.decorator.ts new file mode 100644 index 0000000..05ee3e9 --- /dev/null +++ b/lib/decorators/telegraf-phone.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Phone number handling. + * @param phone Phone number + * + * https://telegraf.js.org/#/?id=phone + */ +export function TelegrafPhone(phone: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.PHONE, { phone }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 03809fb..df440f7 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -89,4 +89,15 @@ export class TelegrafMetadataAccessor { getTelegrafMentionMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.MENTION, target); } + + isTelegrafPhone(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.PHONE, target); + } + + getTelegrafPhoneMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.PHONE, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 66c3371..80f8de8 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -12,4 +12,5 @@ export const DECORATORS = { SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`, ENTITY: `${DECORATORS_PREFIX}/ENTITY`, MENTION: `${DECORATORS_PREFIX}/MENTION`, + PHONE: `${DECORATORS_PREFIX}/PHONE`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index f30fafd..f85f1dd 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -72,6 +72,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafMention(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafPhone(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafPhoneMetadata( + instance[key], + ); + this.handleTelegrafPhone(instance, key, telegraf, metadata); } }, ); @@ -157,4 +162,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.mention(metadata.username, instance[key].bind(instance)); } + + handleTelegrafPhone( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.phone(metadata.phone, instance[key].bind(instance)); + } } From 68690da83e3a8a1764ec89c064ea0f27bceb4714 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:14:34 +0300 Subject: [PATCH 10/23] feat(decorators): TelegrafHashtag added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-hashtag.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 lib/decorators/telegraf-hashtag.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 759e533..8a61847 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -8,3 +8,4 @@ export * from './telegraf-settings.decorator'; export * from './telegraf-entity.decorator'; export * from './telegraf-mention.decorator'; export * from './telegraf-phone.decorator'; +export * from './telegraf-hashtag.decorator'; diff --git a/lib/decorators/telegraf-hashtag.decorator.ts b/lib/decorators/telegraf-hashtag.decorator.ts new file mode 100644 index 0000000..7ab3942 --- /dev/null +++ b/lib/decorators/telegraf-hashtag.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Hashtag handling. + * @param hashtag Hashtag + * + * https://telegraf.js.org/#/?id=hashtag + */ +export function TelegrafHashtag(hashtag: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.HASHTAG, { hashtag }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index df440f7..ba2aa76 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -100,4 +100,15 @@ export class TelegrafMetadataAccessor { getTelegrafPhoneMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.PHONE, target); } + + isTelegrafHashtag(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.HASHTAG, target); + } + + getTelegrafHashtagMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.HASHTAG, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 80f8de8..3617ba7 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -13,4 +13,5 @@ export const DECORATORS = { ENTITY: `${DECORATORS_PREFIX}/ENTITY`, MENTION: `${DECORATORS_PREFIX}/MENTION`, PHONE: `${DECORATORS_PREFIX}/PHONE`, + HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index f85f1dd..30cd359 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -77,6 +77,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafPhone(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafHashtag(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafHashtagMetadata( + instance[key], + ); + this.handleTelegrafHashtag(instance, key, telegraf, metadata); } }, ); @@ -172,4 +177,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.phone(metadata.phone, instance[key].bind(instance)); } + + handleTelegrafHashtag( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); + } } From 7d3e266f0f5e429199bf057e6585df9e2450be84 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:16:48 +0300 Subject: [PATCH 11/23] feat(decorators): TelegrafCashtag added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-cashtag.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 lib/decorators/telegraf-cashtag.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 8a61847..071c1a8 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -9,3 +9,4 @@ export * from './telegraf-entity.decorator'; export * from './telegraf-mention.decorator'; export * from './telegraf-phone.decorator'; export * from './telegraf-hashtag.decorator'; +export * from './telegraf-cashtag.decorator'; diff --git a/lib/decorators/telegraf-cashtag.decorator.ts b/lib/decorators/telegraf-cashtag.decorator.ts new file mode 100644 index 0000000..40a6038 --- /dev/null +++ b/lib/decorators/telegraf-cashtag.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Cashtag handling. + * @param cashtag Cashtag + * + * https://telegraf.js.org/#/?id=cashtag + */ +export function TelegrafCashtag(cashtag: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.CASHTAG, { cashtag }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index ba2aa76..b3c25a8 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -111,4 +111,15 @@ export class TelegrafMetadataAccessor { getTelegrafHashtagMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.HASHTAG, target); } + + isTelegrafCashtag(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.CASHTAG, target); + } + + getTelegrafCashtagMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.CASHTAG, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 3617ba7..118123c 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -14,4 +14,5 @@ export const DECORATORS = { MENTION: `${DECORATORS_PREFIX}/MENTION`, PHONE: `${DECORATORS_PREFIX}/PHONE`, HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, + CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 30cd359..bd60a95 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -82,6 +82,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafHashtag(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafCashtag(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafCashtagMetadata( + instance[key], + ); + this.handleTelegrafCashtag(instance, key, telegraf, metadata); } }, ); @@ -187,4 +192,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.hashtag(metadata.hashtag, instance[key].bind(instance)); } + + handleTelegrafCashtag( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); + } } From 511f9ebd77be085c2e4f5c92bb622e54ca69a92d Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:20:13 +0300 Subject: [PATCH 12/23] feat(decorators): TelegrafAction added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-action.decorator.ts | 13 +++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 15 +++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 lib/decorators/telegraf-action.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 071c1a8..fce4328 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -10,3 +10,4 @@ export * from './telegraf-mention.decorator'; export * from './telegraf-phone.decorator'; export * from './telegraf-hashtag.decorator'; export * from './telegraf-cashtag.decorator'; +export * from './telegraf-action.decorator'; diff --git a/lib/decorators/telegraf-action.decorator.ts b/lib/decorators/telegraf-action.decorator.ts new file mode 100644 index 0000000..ed58872 --- /dev/null +++ b/lib/decorators/telegraf-action.decorator.ts @@ -0,0 +1,13 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; +import { HearsTriggers } from 'telegraf'; + +/** + * 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 { + return SetMetadata(DECORATORS.ACTION, { triggers }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index b3c25a8..634f2e9 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -122,4 +122,15 @@ export class TelegrafMetadataAccessor { getTelegrafCashtagMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.CASHTAG, target); } + + isTelegrafAction(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.ACTION, target); + } + + getTelegrafActionMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.ACTION, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 118123c..cd8d91b 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -15,4 +15,5 @@ export const DECORATORS = { PHONE: `${DECORATORS_PREFIX}/PHONE`, HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, + ACTION: `${DECORATORS_PREFIX}/ACTION`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index bd60a95..635a69c 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -87,6 +87,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafCashtag(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafAction(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafActionMetadata( + instance[key], + ); + this.handleTelegrafAction(instance, key, telegraf, metadata); } }, ); @@ -202,4 +207,14 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.cashtag(metadata.cashtag, instance[key].bind(instance)); } + + handleTelegrafAction( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.cashtag(metadata.triggers, instance[key].bind(instance)); + } } From 76e0eee3adc92f8da6685a6e452dab86739b7b49 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:20:59 +0300 Subject: [PATCH 13/23] fix(decorators): TelegrafAction method call --- lib/telegraf.explorer.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 635a69c..830cc9c 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -214,7 +214,6 @@ export class TelegrafExplorer implements OnModuleInit { telegraf: Telegraf, metadata: any, ) { - // @ts-ignore - telegraf.cashtag(metadata.triggers, instance[key].bind(instance)); + telegraf.action(metadata.triggers, instance[key].bind(instance)); } } From f65905ed0dc556a2f848e8f86d9aa33e6b71b04d Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:25:47 +0300 Subject: [PATCH 14/23] feat(decorators): TelegrafInlineQuery added --- lib/decorators/index.ts | 1 + .../telegraf-inline-query.decorator.ts | 14 ++++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 17 +++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 lib/decorators/telegraf-inline-query.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index fce4328..8bc9e67 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -11,3 +11,4 @@ export * from './telegraf-phone.decorator'; export * from './telegraf-hashtag.decorator'; export * from './telegraf-cashtag.decorator'; export * from './telegraf-action.decorator'; +export * from './telegraf-inline-query.decorator'; diff --git a/lib/decorators/telegraf-inline-query.decorator.ts b/lib/decorators/telegraf-inline-query.decorator.ts new file mode 100644 index 0000000..2f3b6e6 --- /dev/null +++ b/lib/decorators/telegraf-inline-query.decorator.ts @@ -0,0 +1,14 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +export type Triggers = string | string[] | RegExp | RegExp[]; + +/** + * Registers middleware for handling inline_query actions with regular expressions. + * @param triggers Triggers + * + * https://telegraf.js.org/#/?id=inlinequery + */ +export function TelegrafInlineQuery(triggers: Triggers): MethodDecorator { + return SetMetadata(DECORATORS.INLINE_QUERY, { triggers }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 634f2e9..94eb7d9 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -133,4 +133,15 @@ export class TelegrafMetadataAccessor { getTelegrafActionMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.ACTION, target); } + + isTelegrafInlineQuery(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.INLINE_QUERY, target); + } + + getTelegrafInlineQueryMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.INLINE_QUERY, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index cd8d91b..e86ca9a 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -16,4 +16,5 @@ export const DECORATORS = { HASHTAG: `${DECORATORS_PREFIX}/HASHTAG`, CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, ACTION: `${DECORATORS_PREFIX}/ACTION`, + INLINE_QUERY: `${DECORATORS_PREFIX}/INLINE_QUERY`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 830cc9c..2148959 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -92,6 +92,13 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafAction(instance, key, telegraf, metadata); + } else if ( + this.metadataAccessor.isTelegrafInlineQuery(instance[key]) + ) { + const metadata = this.metadataAccessor.getTelegrafInlineQueryMetadata( + instance[key], + ); + this.handleTelegrafInlineQuery(instance, key, telegraf, metadata); } }, ); @@ -216,4 +223,14 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.action(metadata.triggers, instance[key].bind(instance)); } + + handleTelegrafInlineQuery( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + // @ts-ignore + telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance)); + } } From a2eb619e6b3aaadf318d9ffe65e46e1476ed055b Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:28:56 +0300 Subject: [PATCH 15/23] feat(decorators): TelegrafGameQuery added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-game-query.decorator.ts | 11 +++++++++++ lib/telegraf-metadata.accessor.ts | 7 +++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 10 ++++++++++ 5 files changed, 30 insertions(+) create mode 100644 lib/decorators/telegraf-game-query.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 8bc9e67..340866c 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -12,3 +12,4 @@ export * from './telegraf-hashtag.decorator'; export * from './telegraf-cashtag.decorator'; export * from './telegraf-action.decorator'; export * from './telegraf-inline-query.decorator'; +export * from './telegraf-game-query.decorator'; diff --git a/lib/decorators/telegraf-game-query.decorator.ts b/lib/decorators/telegraf-game-query.decorator.ts new file mode 100644 index 0000000..8439345 --- /dev/null +++ b/lib/decorators/telegraf-game-query.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Registers middleware for handling callback_data actions with game query. + * + * https://telegraf.js.org/#/?id=inlinequery + */ +export function TelegrafGameQuery(): MethodDecorator { + return SetMetadata(DECORATORS.GAME_QUERY, {}); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index 94eb7d9..b4da7bc 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -144,4 +144,11 @@ export class TelegrafMetadataAccessor { getTelegrafInlineQueryMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.INLINE_QUERY, target); } + + isTelegrafGameQuery(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.GAME_QUERY, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index e86ca9a..c70b4b9 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -17,4 +17,5 @@ export const DECORATORS = { CASHTAG: `${DECORATORS_PREFIX}/CASHTAG`, ACTION: `${DECORATORS_PREFIX}/ACTION`, INLINE_QUERY: `${DECORATORS_PREFIX}/INLINE_QUERY`, + GAME_QUERY: `${DECORATORS_PREFIX}/GAME_QUERY`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 2148959..26af964 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -99,6 +99,8 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafInlineQuery(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafGameQuery(instance[key])) { + this.handleTelegrafGameQuery(instance, key, telegraf); } }, ); @@ -233,4 +235,12 @@ export class TelegrafExplorer implements OnModuleInit { // @ts-ignore telegraf.inlineQuery(metadata.triggers, instance[key].bind(instance)); } + + handleTelegrafGameQuery( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.gameQuery(instance[key].bind(instance)); + } } From d552931a1f38b20195a236a309ec29506457dc33 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:41:23 +0300 Subject: [PATCH 16/23] chore(deps): lodash removed --- package-lock.json | 5 ----- package.json | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5f55a11..a73b6d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -902,11 +902,6 @@ "p-locate": "^4.1.0" } }, - "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" - }, "log-symbols": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-3.0.0.tgz", diff --git a/package.json b/package.json index af35665..91e30e4 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,7 @@ "prepublish:npm": "npm run build", "publish:npm": "npm publish --access public" }, - "dependencies": { - "lodash": "4.17.15" - }, + "dependencies": {}, "devDependencies": { "@nestjs/common": "6.11.11", "@nestjs/core": "6.11.11", From 527dd29025e618c7b7823aee6db7f885400b1235 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:48:40 +0300 Subject: [PATCH 17/23] chore(deps): add telegraf package as dependency --- package-lock.json | 31 +++++++++++-------------------- package.json | 8 ++++---- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index a73b6d1..e971db8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -109,10 +109,9 @@ "dev": true }, "@types/node": { - "version": "13.1.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.1.6.tgz", - "integrity": "sha512-Jg1F+bmxcpENHP23sVKkNuU3uaxPnsBMW0cLjleiikFKomJQbsn0Cqk2yDvQArqzZN6ABfBkZ0To7pQ8sLdWDg==", - "dev": true + "version": "13.9.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.9.2.tgz", + "integrity": "sha512-bnoqK579sAYrQbp73wwglccjJ4sfRdKU7WNEZ5FW4K2U6Kc0/eZ5kvXG0JKsEKFB50zrFmfFt52/cvBbZa7eXg==" }, "@types/parse-json": { "version": "4.0.0", @@ -970,16 +969,14 @@ "dev": true }, "minimist": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", - "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", - "dev": true + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "module-alias": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/module-alias/-/module-alias-2.2.2.tgz", - "integrity": "sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==", - "dev": true + "integrity": "sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==" }, "ms": { "version": "2.0.0", @@ -996,8 +993,7 @@ "node-fetch": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", - "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==", - "dev": true + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" }, "normalize-path": { "version": "3.0.0", @@ -1234,8 +1230,7 @@ "sandwich-stream": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/sandwich-stream/-/sandwich-stream-2.0.2.tgz", - "integrity": "sha512-jLYV0DORrzY3xaz/S9ydJL6Iz7essZeAfnAavsJ+zsJGZ1MOnsS52yRjU3uF3pJa/lla7+wisp//fxOwOH8SKQ==", - "dev": true + "integrity": "sha512-jLYV0DORrzY3xaz/S9ydJL6Iz7essZeAfnAavsJ+zsJGZ1MOnsS52yRjU3uF3pJa/lla7+wisp//fxOwOH8SKQ==" }, "semver-compare": { "version": "1.0.0", @@ -1344,7 +1339,6 @@ "version": "3.36.0", "resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.36.0.tgz", "integrity": "sha512-9o6AJKRiTm5vMWYI6WpTfBHzu4FMpWBNKxvnMxRds/cbMY9RnsVVjdi8i4bFFlfd+xbi73EbrnI3dybayryICA==", - "dev": true, "requires": { "@types/node": "^13.1.0", "debug": "^4.0.1", @@ -1359,7 +1353,6 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "dev": true, "requires": { "ms": "^2.1.1" } @@ -1367,16 +1360,14 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" } } }, "telegram-typings": { "version": "3.6.1", "resolved": "https://registry.npmjs.org/telegram-typings/-/telegram-typings-3.6.1.tgz", - "integrity": "sha512-njVv1EAhIZnmQVLocZEADYUyqA1WIXuVcDYlsp+mXua/XB0pxx+PKtMSPeZ/EE4wPWTw9h/hA9ASTT6yQelkiw==", - "dev": true + "integrity": "sha512-njVv1EAhIZnmQVLocZEADYUyqA1WIXuVcDYlsp+mXua/XB0pxx+PKtMSPeZ/EE4wPWTw9h/hA9ASTT6yQelkiw==" }, "timers-ext": { "version": "0.1.7", diff --git a/package.json b/package.json index 91e30e4..60346dc 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,9 @@ "prepublish:npm": "npm run build", "publish:npm": "npm publish --access public" }, - "dependencies": {}, + "dependencies": { + "telegraf": "^3.36.0" + }, "devDependencies": { "@nestjs/common": "6.11.11", "@nestjs/core": "6.11.11", @@ -42,14 +44,12 @@ "lint-staged": "10.0.8", "prettier": "1.19.1", "reflect-metadata": "0.1.13", - "telegraf": "3.36.0", "typescript": "3.8.3" }, "peerDependencies": { "@nestjs/common": "^6.7.0", "@nestjs/core": "^6.7.0", - "reflect-metadata": "^0.1.13", - "telegraf": "^3.35.0" + "reflect-metadata": "^0.1.13" }, "husky": { "hooks": { From dd59da7737d568ff681442bec8de9118b7c46ca7 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 17:53:24 +0300 Subject: [PATCH 18/23] chore(deps): lodash typings removed --- package-lock.json | 6 ------ package.json | 1 - 2 files changed, 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index e971db8..2049bc0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -102,12 +102,6 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, - "@types/lodash": { - "version": "4.14.149", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.149.tgz", - "integrity": "sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ==", - "dev": true - }, "@types/node": { "version": "13.9.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.9.2.tgz", diff --git a/package.json b/package.json index 60346dc..92ebae7 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "devDependencies": { "@nestjs/common": "6.11.11", "@nestjs/core": "6.11.11", - "@types/lodash": "4.14.149", "husky": "4.2.3", "lint-staged": "10.0.8", "prettier": "1.19.1", From 25710b790855f3b071e2d96bfd620e9f70f4be4b Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 18:20:07 +0300 Subject: [PATCH 19/23] feat(decorators): metadata typings added --- lib/decorators/telegraf-action.decorator.ts | 10 +++- lib/decorators/telegraf-cashtag.decorator.ts | 10 +++- lib/decorators/telegraf-command.decorator.ts | 10 +++- lib/decorators/telegraf-entity.decorator.ts | 13 ++++- lib/decorators/telegraf-hashtag.decorator.ts | 10 +++- lib/decorators/telegraf-hears.decorator.ts | 10 +++- .../telegraf-inline-query.decorator.ts | 10 +++- lib/decorators/telegraf-mention.decorator.ts | 10 +++- lib/decorators/telegraf-on.decorator.ts | 12 ++++- lib/decorators/telegraf-phone.decorator.ts | 8 ++- lib/telegraf-metadata.accessor.ts | 54 +++++++++++++++---- lib/telegraf.explorer.ts | 32 +++++++---- lib/telegraf.provider.ts | 7 ++- 13 files changed, 162 insertions(+), 34 deletions(-) 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(); + } } From ee0ad03af83e1e8aad9d5f2e75b124739a042efb Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 19:23:51 +0300 Subject: [PATCH 20/23] fix: forRoot method working --- lib/telegraf-core.module.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index 0e45913..49b2ee4 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -19,10 +19,18 @@ import { TelegrafProvider } from './telegraf.provider'; }) export class TelegrafCoreModule { public static forRoot(options: TelegrafModuleOptions): DynamicModule { + const telegrafProvider = { + provide: TELEGRAF_PROVIDER, + useClass: TelegrafProvider, + inject: [TELEGRAF_MODULE_OPTIONS], + }; return { module: TelegrafCoreModule, - providers: [], - exports: [], + providers: [ + { provide: TELEGRAF_MODULE_OPTIONS, useValue: options }, + telegrafProvider, + ], + exports: [telegrafProvider], }; } From 2b05c5cc170638c093b0de60f1e9067f2a46ec3d Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 20:50:04 +0300 Subject: [PATCH 21/23] docs: update --- README.md | 245 ++++++++++++++++++------------------------------------ 1 file changed, 79 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index a62f10c..57651fd 100644 --- a/README.md +++ b/README.md @@ -27,213 +27,126 @@ ## Installation ```bash -$ npm i nestjs-telegraf telegraf +$ npm i nestjs-telegraf ``` -## Usage - -### An example of package usage +Once the installation process is complete, we can import the TelegrafModule into the root AppModule. ```typescript -/* bot.module.ts */ +/* app.module.ts */ -import { Module, OnModuleInit, Logger } from '@nestjs/common' -import { ModuleRef } from '@nestjs/core' -import { ConfigModule } from '@nestjs/config' -import { TelegrafModule, TelegrafService } from 'nestjs-telegraf' -import botConfig from './bot.config' -import { TelegrafConfigService } from './telegraf-config.service' -import { BotService } from './bot.service' +import { Module } from '@nestjs/common'; +import { TelegrafModule } from 'nestjs-telegraf'; @Module({ imports: [ - TelegrafModule.fromFactory({ - imports: [ConfigModule.forFeature(botConfig)], - useClass: TelegrafConfigService, - }), + TelegrafModule.forRoot({ + token: 'TELEGRAM_BOT_TOKEN', + }) ], - exports: [TelegrafModule], - providers: [BotService], }) -export class BotModule implements OnModuleInit { - constructor( - private readonly moduleRef: ModuleRef, - private readonly telegrafService: TelegrafService - ) {} - - onModuleInit() { - this.telegrafService.init(this.moduleRef) - this.telegrafService.startPolling() - } -} +export class AppModule {} ``` -```typescript -/* telegraf-config.service.ts */ +The `forRoot()` method accepts the same configuration object as Telegraf class constructor from the Telegraf package, as described [here](https://telegraf.js.org/#/?id=constructor). -import { Injectable } from '@nestjs/common' -import { TelegrafOptionsFactory, TelegrafModuleOptions } from 'nestjs-telegraf' -import { ConfigService } from '@nestjs/config' +## Telegraf methods + +Each Telegraf instance method described [here](https://telegraf.js.org/#/?id=telegraf) has own decorator in `nestjs-telegraf` package. The name of the decorator corresponds to the name of the Telegraf method and starts with `Telegraf`. For example [`@TelegrafHears`](https://telegraf.js.org/#/?id=hears), [`@TelegrafOn`](https://telegraf.js.org/#/?id=on), [`@TelegrafAction`](https://telegraf.js.org/#/?id=action) and so on. + +Now let's try to repeat the example from the Telegraf [documentation page](https://telegraf.js.org/#/?id=example). + +```typescript +/* app.service.ts */ + +import { Injectable } from '@nestjs/common'; +import { + TelegrafStart, + TelegrafHelp, + TelegrafOn, + TelegrafHears, + ContextMessageUpdate, +} from 'nestjs-telegraf'; @Injectable() -export class TelegrafConfigService implements TelegrafOptionsFactory { - constructor(private readonly configService: ConfigService) {} +export class AppService { + @TelegrafStart() + start(ctx: ContextMessageUpdate) { + ctx.reply('Welcome'); + } - createTelegrafOptions(): TelegrafModuleOptions { - return { - token: this.configService.get('bot.token'), - } + @TelegrafHelp() + help(ctx: ContextMessageUpdate) { + ctx.reply('Send me a sticker'); + } + + @TelegrafOn('sticker') + on(ctx: ContextMessageUpdate) { + ctx.reply('👍'); + } + + @TelegrafHears('hi') + hears(ctx: ContextMessageUpdate) { + ctx.reply('Hey there'); } } ``` -```typescript -/* bot.config.ts */ +## Async configuration +When you need to pass module options asynchronously instead of statically, use the forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration. -import { registerAs } from '@nestjs/config' - -interface Config { - token: string -} - -export default registerAs( - 'bot', - (): Config => ({ - token: process.env.TELEGRAM_BOT_TOKEN, - }) -) -``` - -### Telegraf - -#### Telegraf methods usage -You can decorate any `Telegraf` method with `@TelegramActionHandler` decorator. +One technique is to use a factory function: ```typescript -/* bot.service.ts */ - -import { Injectable } from '@nestjs/common' -import { TelegrafTelegramService } from 'nestjs-telegraf' -import { ContextMessageUpdate } from 'telegraf' - -@Injectable() -export class BotService { - /* This decorator handle /start command */ - @TelegramActionHandler({ onStart: true }) - async onStart(ctx: ContextMessageUpdate) { - await ctx.reply('/start command reply') - } -} -``` - -##### Today available actions for decorator: - -- [`onStart`](https://telegraf.js.org/#/?id=start) Handler for /start command. - -- [`command`](https://telegraf.js.org/#/?id=command) Command handling. - -- [`message`](https://telegraf.js.org/#/?id=hears) Registers middleware for handling text messages. - -- [`action`](https://telegraf.js.org/#/?id=action) Registers middleware for handling `callback_data` actions with regular expressions. - -#### Telegraf middlewares usage - -See https://github.com/bukhalo/nestjs-telegraf/issues/7#issuecomment-577582322 - -#### Telegraf proxy usage - -```typescript - -/* bot.config.ts */ - -import { registerAs } from '@nestjs/config' - -interface Config { - token: string - socksHost: string - socksPort: string | number - socksUser: string - socksPassword: string -} - -export default registerAs( - 'bot', - (): Config => ({ - token: process.env.TELEGRAM_BOT_TOKEN, - socksHost: process.env.TELEGRAM_BOT_SOCKS_HOST, - socksPort: process.env.TELEGRAM_BOT_SOCKS_PORT, - socksUser: process.env.TELEGRAM_BOT_SOCKS_USER, - socksPassword: process.env.TELEGRAM_BOT_SOCKS_PASS, +TelegrafModule.forRootAsync({ + useFactory: () => ({ + token: 'TELEGRAM_BOT_TOKEN', }), -); - +}); ``` +Like other [factory providers](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory), our factory function can be async and can inject dependencies through inject. + ```typescript +TelegrafModule.forRootAsync({ + imports: [ConfigModule], + useFactory: async (configService: ConfigService) => ({ + token: configService.getString('TELEGRAM_BOT_TOKEN'), + }), + inject: [ConfigService], +}); +``` -/* telegraf-config.service.ts */ +Alternatively, you can configure the TelegrafModule using a class instead of a factory, as shown below: -import { Injectable } from '@nestjs/common' -import { ConfigService } from '@nestjs/config' -import { TelegrafModuleOptions, TelegrafOptionsFactory } from 'nestjs-telegraf' -import { SocksProxyAgent } from 'socks-proxy-agent' +```typescript +TelegrafModule.forRootAsync({ + useClass: TelegrafConfigService, +}); +``` +The construction above instantiates `TelegrafConfigService` inside `TelegrafModule`, using it to create the required options object. Note that in this example, the `TelegrafConfigService` has to implement the `TelegrafOptionsFactory` interface, as shown below. The `TelegrafModule` will call the `createTelegrafOptions()` method on the instantiated object of the supplied class. + +```typescript @Injectable() -export class TelegrafConfigService implements TelegrafOptionsFactory { - private agent - - constructor(private readonly configService: ConfigService) {} - - createTelegrafOptions(): TelegrafModuleOptions { - const proxyConfig = { - host: this.configService.get('bot.socksHost'), - port: this.configService.get('bot.socksPort'), - userId: this.configService.get('bot.socksUser'), - password: this.configService.get('bot.socksPassword'), - } - this.agent = new SocksProxyAgent(proxyConfig) - +class TelegrafConfigService implements TelegrafOptionsFactory { + createMongooseOptions(): TelegrafModuleOptions { return { - token: this.configService.get('bot.token'), - telegrafOptions: { telegram: { agent: this.agent } }, + token: 'TELEGRAM_BOT_TOKEN', }; } } - ``` -### Telegram - -#### Telegram methods usage - -Inject `TelegrafTelegramService` from `nestjs-telegraf` package for use [Telegram instance](https://telegraf.js.org/#/?id=telegram) from `telegraf` package. +If you want to reuse an existing options provider instead of creating a private copy inside the `TelegrafModule`, use the `useExisting` syntax. ```typescript -/* bot.service.ts */ - -import { Injectable } from '@nestjs/common' -import { TelegrafTelegramService, TelegramActionHandler } from 'nestjs-telegraf' -import { ContextMessageUpdate } from 'telegraf' - -@Injectable() -export class BotService { - constructor( - private readonly telegrafTelegramService: TelegrafTelegramService - ) {} - - @TelegramActionHandler({ onStart: true }) - async start(ctx: ContextMessageUpdate) { - const me = await this.telegrafTelegramService.getMe() - console.log(me) - } -} +TelegrafModule.forRootAsync({ + imports: [ConfigModule], + useExisting: ConfigService, +}); ``` -## Examples - -You can see the basic use of the package in this repository: -https://github.com/bukhalo/nestjs-telegraf-sample - ## Support Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). From 2f48d7a848ff6dc8adeb0988756541894eba7b74 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 20:50:44 +0300 Subject: [PATCH 22/23] feat: export ContextMessageUpdate type from telegraf package --- lib/interfaces/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 23e7d9f..08c29f5 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -1 +1,2 @@ +export { ContextMessageUpdate } from 'telegraf'; export * from './telegraf-options.interface'; From 0ef899395e07378449d21c120a1e2429b8b266c9 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 22:00:21 +0300 Subject: [PATCH 23/23] chore(release): v1.0.0-alpha.1 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2049bc0..0ab762f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nestjs-telegraf", - "version": "0.7.3", + "version": "1.0.0-alpha.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 92ebae7..92e9b66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nestjs-telegraf", - "version": "0.7.3", + "version": "1.0.0-alpha.1", "description": "Telegraf module for Nest framework", "keywords": [ "nest",