From aebb4c0fe0499c5918fb05b37535579a156feb81 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Sun, 3 May 2020 12:14:17 +0300 Subject: [PATCH] docs: add info about injectbot decorator --- README.md | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bed6f1e..97ef3fd 100644 --- a/README.md +++ b/README.md @@ -68,33 +68,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 +125,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 +158,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 +179,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 +193,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).