diff --git a/.all-contributorsrc b/.all-contributorsrc index 2520561..89a7706 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -11,8 +11,7 @@ "avatar_url": "https://avatars3.githubusercontent.com/u/5383030?v=4", "profile": "https://github.com/Sedjj", "contributions": [ - "bug", - "ideas" + "bug" ] }, { @@ -22,8 +21,7 @@ "profile": "http://www.ismb.it/vito.macchia", "contributions": [ "code", - "bug", - "ideas" + "bug" ] }, { @@ -42,17 +40,9 @@ "avatar_url": "https://avatars2.githubusercontent.com/u/14031838?v=4", "profile": "https://bukhalo.com/", "contributions": [ - "business", "code", "doc", - "example", - "ideas", - "infra", - "maintenance", - "projectManagement", - "question", - "review", - "tutorial" + "review" ] }, { @@ -61,7 +51,6 @@ "avatar_url": "https://avatars3.githubusercontent.com/u/43011265?v=4", "profile": "https://github.com/VyacheslavSaloidWork", "contributions": [ - "question", "bug" ] } diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index 5b74740..0000000 --- a/.prettierignore +++ /dev/null @@ -1 +0,0 @@ -lib/telegraf.provider.ts diff --git a/README.md b/README.md index bed6f1e..0a809e8 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,13 @@

- -[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-) - - Nest Logo + + Nest Logo

-[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 -[circleci-url]: https://circleci.com/gh/nestjs/nest - -

A progressive Node.js framework for building efficient and scalable server-side applications.

-

-NPM Version -Package License -NPM Downloads -CircleCI -Coverage -Discord -Backers on Open Collective -Sponsors on Open Collective - - Support us - -

- -## Description +# NestJS Telegraf +![npm](https://img.shields.io/npm/dm/nestjs-telegraf) +![GitHub last commit](https://img.shields.io/github/last-commit/bukhalo/nestjs-telegraf) +![NPM](https://img.shields.io/npm/l/nestjs-telegraf) [Telegraf](https://github.com/telegraf/telegraf) module for [NestJS](https://github.com/nestjs/nest). @@ -68,33 +52,46 @@ import { TelegrafHelp, TelegrafOn, TelegrafHears, - ContextMessageUpdate, + Context, } from 'nestjs-telegraf'; @Injectable() export class AppService { @TelegrafStart() - start(ctx: ContextMessageUpdate) { + start(ctx: Context) { ctx.reply('Welcome'); } @TelegrafHelp() - help(ctx: ContextMessageUpdate) { + help(ctx: Context) { ctx.reply('Send me a sticker'); } @TelegrafOn('sticker') - on(ctx: ContextMessageUpdate) { + on(ctx: Context) { ctx.reply('👍'); } @TelegrafHears('hi') - hears(ctx: ContextMessageUpdate) { + hears(ctx: Context) { ctx.reply('Hey there'); } } ``` +## Bot injection +At times you may need to access the native `Telegraf` instance. For example, you may want to connect stage middleware. You can inject the Telegraf by using the `@InjectBot()` decorator as follows: +```typescript +import { Injectable } from '@nestjs/common'; +import { InjectBot, TelegrafProvider } from 'nestjs-telegraf'; + +@Injectable() +export class BotSettingsService { + constructor(@InjectBot() private bot: TelegrafProvider) {} +} +``` + + ## 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. @@ -112,7 +109,7 @@ Like other [factory providers](https://docs.nestjs.com/fundamentals/custom-provi ```typescript TelegrafModule.forRootAsync({ - imports: [ConfigModule], + imports: [ConfigModule.forFeature(telegrafModuleConfig)], useFactory: async (configService: ConfigService) => ({ token: configService.get('TELEGRAM_BOT_TOKEN'), }), @@ -145,7 +142,7 @@ If you want to reuse an existing options provider instead of creating a private ```typescript TelegrafModule.forRootAsync({ - imports: [ConfigModule], + imports: [ConfigModule.forFeature(telegrafModuleConfig)], useExisting: ConfigService, }); ``` @@ -166,7 +163,7 @@ app.use(telegrafProvider.webhookCallback('/secret-path')); The last step is to specify launchOptions in `forRoot` method: ```typescript TelegrafModule.forRootAsync({ - imports: [ConfigModule], + imports: [ConfigModule.forFeature(telegrafModuleConfig)], useFactory: async (configService: ConfigService) => ({ token: configService.get('TELEGRAM_BOT_TOKEN'), launchOptions: { @@ -180,6 +177,7 @@ TelegrafModule.forRootAsync({ }); ``` + ## 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). @@ -203,11 +201,11 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d - - + + - - + +

Eldar Salimzebarov

🐛 🤔

Vito Macchia

💻 🐛 🤔

Eldar Salimzebarov

🐛

Vito Macchia

💻 🐛

KITAHARA SETSUNA

💻 🐛

Aleksandr Bukhalo

💼 💻 📖 💡 🤔 🚇 🚧 📆 💬 👀

Vyacheslav Saloid

💬 🐛

Aleksandr Bukhalo

💻 📖 👀

Vyacheslav Saloid

🐛
diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 340866c..37dcea4 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -1,3 +1,4 @@ +export * from './inject-bot.decorator'; export * from './telegraf-use.decorator'; export * from './telegraf-on.decorator'; export * from './telegraf-hears.decorator'; diff --git a/lib/decorators/inject-bot.decorator.ts b/lib/decorators/inject-bot.decorator.ts new file mode 100644 index 0000000..84f1c50 --- /dev/null +++ b/lib/decorators/inject-bot.decorator.ts @@ -0,0 +1,4 @@ +import { Inject } from '@nestjs/common'; +import { TELEGRAF_PROVIDER } from '../telegraf.constants'; + +export const InjectBot = (): ParameterDecorator => Inject(TELEGRAF_PROVIDER); diff --git a/lib/decorators/telegraf-action.decorator.ts b/lib/decorators/telegraf-action.decorator.ts index 2fd6781..8345f33 100644 --- a/lib/decorators/telegraf-action.decorator.ts +++ b/lib/decorators/telegraf-action.decorator.ts @@ -1,8 +1,9 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; -import { HearsTriggers } from 'telegraf'; +import { HearsTriggers } from 'telegraf/typings/composer'; +import { Context } from '../interfaces'; -export type TelegrafActionTriggers = HearsTriggers; +export type TelegrafActionTriggers = HearsTriggers; export interface TelegrafActionMetadata { triggers: TelegrafActionTriggers; diff --git a/lib/decorators/telegraf-hears.decorator.ts b/lib/decorators/telegraf-hears.decorator.ts index 17b8d1b..4c0a280 100644 --- a/lib/decorators/telegraf-hears.decorator.ts +++ b/lib/decorators/telegraf-hears.decorator.ts @@ -1,8 +1,9 @@ import { SetMetadata } from '@nestjs/common'; import { DECORATORS } from '../telegraf.constants'; -import { HearsTriggers } from 'telegraf'; +import { HearsTriggers } from 'telegraf/typings/composer'; +import { Context } from '../interfaces'; -export type TelegrafHearsTriggers = HearsTriggers; +export type TelegrafHearsTriggers = HearsTriggers; export interface TelegrafHearsMetadata { triggers: TelegrafHearsTriggers; diff --git a/lib/index.ts b/lib/index.ts index 6ca2523..deb2848 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,3 +1,11 @@ -export * from './telegraf.module'; -export * from './interfaces'; +export * as Composer from 'telegraf/composer'; +export * as Markup from 'telegraf/markup'; +export * as BaseScene from 'telegraf/scenes/base'; +export * as session from 'telegraf/session'; +export * as Stage from 'telegraf/stage'; +export * as WizardScene from 'telegraf/scenes/wizard'; + export * from './decorators'; +export * from './interfaces'; +export * from './telegraf.module'; +export * from './telegraf.provider'; diff --git a/lib/interfaces/context.interface.ts b/lib/interfaces/context.interface.ts new file mode 100644 index 0000000..418bbac --- /dev/null +++ b/lib/interfaces/context.interface.ts @@ -0,0 +1,11 @@ +import { TelegrafContext } from 'telegraf/typings/context'; + +export interface Context extends TelegrafContext { + [key: string]: any; +} + +/** + * Removed type from Telegraf v3.38.0, added for backward compatibility. + * TODO: remove on next major release + */ +export interface ContextMessageUpdate extends Context {} diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 08c29f5..3b25926 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -1,2 +1,2 @@ -export { ContextMessageUpdate } from 'telegraf'; +export * from './context.interface'; export * from './telegraf-options.interface'; diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index cd4229f..13c729d 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -3,7 +3,7 @@ import { TelegrafOptions, LaunchPollingOptions, LaunchWebhookOptions, -} from 'telegraf'; +} from 'telegraf/typings/telegraf'; export interface TelegrafModuleOptions { token: string; diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index 49b2ee4..9be69ed 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -1,3 +1,4 @@ +import { DiscoveryModule } from '@nestjs/core'; import { Module, DynamicModule, Provider, Type } from '@nestjs/common'; import { TelegrafModuleOptions, @@ -10,7 +11,6 @@ import { } 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({ diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 036679f..49292f7 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -5,7 +5,6 @@ import { MetadataScanner } from '@nestjs/core/metadata-scanner'; import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; import { TelegrafProvider } from './telegraf.provider'; import { TELEGRAF_PROVIDER } from './telegraf.constants'; -import { Telegraf, ContextMessageUpdate } from 'telegraf'; import { TelegrafActionMetadata, TelegrafCashtagMetadata, @@ -41,9 +40,11 @@ export class TelegrafExplorer implements OnModuleInit { return; } - const telegraf = this.moduleRef.get>( + const telegraf: TelegrafProvider = this.moduleRef.get( TELEGRAF_PROVIDER, - { strict: false }, + { + strict: false, + }, ); this.metadataScanner.scanFromPrototype( @@ -118,18 +119,14 @@ export class TelegrafExplorer implements OnModuleInit { }); } - handleTelegrafUse( - instance: object, - key: string, - telegraf: Telegraf, - ) { + handleTelegrafUse(instance: object, key: string, telegraf: TelegrafProvider) { telegraf.use(instance[key].bind(instance)); } handleTelegrafOn( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafOnMetadata, ) { telegraf.on(metadata.updateTypes, instance[key].bind(instance)); @@ -138,7 +135,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafHears( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafHearsMetadata, ) { telegraf.hears(metadata.triggers, instance[key].bind(instance)); @@ -147,7 +144,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafCommand( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafCommandMetadata, ) { telegraf.command(metadata.commands, instance[key].bind(instance)); @@ -156,7 +153,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafStart( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, ) { telegraf.start(instance[key].bind(instance)); } @@ -164,7 +161,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafHelp( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, ) { telegraf.help(instance[key].bind(instance)); } @@ -172,7 +169,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafSettings( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, ) { // @ts-ignore telegraf.settings(instance[key].bind(instance)); @@ -181,7 +178,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafEntity( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafEntityMetadata, ) { // @ts-ignore @@ -191,7 +188,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafMention( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafMentionMetadata, ) { // @ts-ignore @@ -201,7 +198,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafPhone( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafPhoneMetadata, ) { // @ts-ignore @@ -211,7 +208,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafHashtag( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafHashtagMetadata, ) { // @ts-ignore @@ -221,7 +218,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafCashtag( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafCashtagMetadata, ) { // @ts-ignore @@ -231,7 +228,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafAction( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafActionMetadata, ) { telegraf.action(metadata.triggers, instance[key].bind(instance)); @@ -240,7 +237,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafInlineQuery( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, metadata: TelegrafInlineQueryMetadata, ) { // @ts-ignore @@ -250,7 +247,7 @@ export class TelegrafExplorer implements OnModuleInit { handleTelegrafGameQuery( instance: object, key: string, - telegraf: Telegraf, + telegraf: TelegrafProvider, ) { telegraf.gameQuery(instance[key].bind(instance)); } diff --git a/lib/telegraf.module.ts b/lib/telegraf.module.ts index a0c34de..fb3cdeb 100644 --- a/lib/telegraf.module.ts +++ b/lib/telegraf.module.ts @@ -7,10 +7,11 @@ import { @Module({}) export class TelegrafModule { - public static forRoot(options?: TelegrafModuleOptions): DynamicModule { + public static forRoot(options: TelegrafModuleOptions): DynamicModule { return { module: TelegrafModule, imports: [TelegrafCoreModule.forRoot(options)], + exports: [TelegrafCoreModule], }; } @@ -20,6 +21,7 @@ export class TelegrafModule { return { module: TelegrafModule, imports: [TelegrafCoreModule.forRootAsync(options)], + exports: [TelegrafCoreModule], }; } } diff --git a/lib/telegraf.provider.ts b/lib/telegraf.provider.ts index 145d7b5..5b21284 100644 --- a/lib/telegraf.provider.ts +++ b/lib/telegraf.provider.ts @@ -5,32 +5,30 @@ import { Logger, OnApplicationShutdown, } from '@nestjs/common'; -import Telegraf, { ContextMessageUpdate } from 'telegraf'; +import { Telegraf } from 'telegraf'; +import { Context, TelegrafModuleOptions } from './interfaces'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; -import { TelegrafModuleOptions } from './interfaces'; @Injectable() -export class TelegrafProvider - // @ts-ignore - extends Telegraf +export class TelegrafProvider extends Telegraf implements OnApplicationBootstrap, OnApplicationShutdown { private logger = new Logger('Telegraf'); - private launchOptions; + private readonly launchOptions; constructor(@Inject(TELEGRAF_MODULE_OPTIONS) options: TelegrafModuleOptions) { super(options.token, options.options); this.launchOptions = options.launchOptions; } - onApplicationBootstrap() { + async onApplicationBootstrap() { this.catch((e) => { this.logger.error(e); }); - this.launch(this.launchOptions); + await this.launch(this.launchOptions); } - async onApplicationShutdown(signal?: string) { + async onApplicationShutdown() { await this.stop(); } } diff --git a/package-lock.json b/package-lock.json index c942ff5..8f863e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nestjs-telegraf", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2845,9 +2845,9 @@ } }, "telegraf": { - "version": "3.37.0", - "resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.37.0.tgz", - "integrity": "sha512-V3448qwfOolBqkIc87yxjW4zMvR2P6AIF24pPTlX9WhZPwA1TF/x3nQhnWPRLtGh2SJuvDcr83iTkXPXT7Opnw==", + "version": "3.38.0", + "resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.38.0.tgz", + "integrity": "sha512-va4VlrKWp64JrowFoZX/NPzzA6q38kvaIukVXOWFO1V+jR1G8+hCfgJy4TX8Z3rwLJzwaBEet1QhikHDRZWl3A==", "requires": { "debug": "^4.0.1", "minimist": "^1.2.0", diff --git a/package.json b/package.json index 9542fae..e8a0b26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nestjs-telegraf", - "version": "1.1.0", + "version": "1.2.0", "description": "Telegraf module for NestJS", "keywords": [ "nest", @@ -33,7 +33,7 @@ "test": "" }, "dependencies": { - "telegraf": "3.37.0" + "telegraf": "3.38.0" }, "devDependencies": { "@nestjs/common": "7.0.9",