docs: add info about injectbot decorator

This commit is contained in:
Aleksandr Bukhalo 2020-05-03 12:14:17 +03:00
parent 62bdbd77e5
commit aebb4c0fe0

View File

@ -68,33 +68,46 @@ import {
TelegrafHelp, TelegrafHelp,
TelegrafOn, TelegrafOn,
TelegrafHears, TelegrafHears,
ContextMessageUpdate, Context,
} from 'nestjs-telegraf'; } from 'nestjs-telegraf';
@Injectable() @Injectable()
export class AppService { export class AppService {
@TelegrafStart() @TelegrafStart()
start(ctx: ContextMessageUpdate) { start(ctx: Context) {
ctx.reply('Welcome'); ctx.reply('Welcome');
} }
@TelegrafHelp() @TelegrafHelp()
help(ctx: ContextMessageUpdate) { help(ctx: Context) {
ctx.reply('Send me a sticker'); ctx.reply('Send me a sticker');
} }
@TelegrafOn('sticker') @TelegrafOn('sticker')
on(ctx: ContextMessageUpdate) { on(ctx: Context) {
ctx.reply('👍'); ctx.reply('👍');
} }
@TelegrafHears('hi') @TelegrafHears('hi')
hears(ctx: ContextMessageUpdate) { hears(ctx: Context) {
ctx.reply('Hey there'); 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 ## 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. 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 ```typescript
TelegrafModule.forRootAsync({ TelegrafModule.forRootAsync({
imports: [ConfigModule], imports: [ConfigModule.forFeature(telegrafModuleConfig)],
useFactory: async (configService: ConfigService) => ({ useFactory: async (configService: ConfigService) => ({
token: configService.get<string>('TELEGRAM_BOT_TOKEN'), token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
}), }),
@ -145,7 +158,7 @@ If you want to reuse an existing options provider instead of creating a private
```typescript ```typescript
TelegrafModule.forRootAsync({ TelegrafModule.forRootAsync({
imports: [ConfigModule], imports: [ConfigModule.forFeature(telegrafModuleConfig)],
useExisting: ConfigService, useExisting: ConfigService,
}); });
``` ```
@ -166,7 +179,7 @@ app.use(telegrafProvider.webhookCallback('/secret-path'));
The last step is to specify launchOptions in `forRoot` method: The last step is to specify launchOptions in `forRoot` method:
```typescript ```typescript
TelegrafModule.forRootAsync({ TelegrafModule.forRootAsync({
imports: [ConfigModule], imports: [ConfigModule.forFeature(telegrafModuleConfig)],
useFactory: async (configService: ConfigService) => ({ useFactory: async (configService: ConfigService) => ({
token: configService.get<string>('TELEGRAM_BOT_TOKEN'), token: configService.get<string>('TELEGRAM_BOT_TOKEN'),
launchOptions: { launchOptions: {
@ -180,6 +193,7 @@ TelegrafModule.forRootAsync({
}); });
``` ```
## Support ## 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). 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).