mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 15:04:38 +03:00
docs: add info about injectbot decorator
This commit is contained in:
parent
62bdbd77e5
commit
aebb4c0fe0
30
README.md
30
README.md
@ -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).
|
||||||
|
Loading…
Reference in New Issue
Block a user