mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
chore(release): v2.0.0
This commit is contained in:
36
website/docs/extras/bot-injection.md
Normal file
36
website/docs/extras/bot-injection.md
Normal file
@@ -0,0 +1,36 @@
|
||||
---
|
||||
id: bot-injection
|
||||
title: Bot injection
|
||||
sidebar_label: Bot injection
|
||||
slug: /extras/bot-injection
|
||||
---
|
||||
|
||||
At times you may need to access the native `Telegraf` instance. You can inject the Telegraf by using the `@InjectBot()` decorator as follows:
|
||||
|
||||
```typescript {8} title="src/echo/echo.service.ts"
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectBot } from 'nestjs-telegraf';
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';
|
||||
|
||||
@Injectable()
|
||||
export class EchoService {
|
||||
constructor(@InjectBot() private bot: Telegraf<TelegrafContext>) {}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
If you run [multiple bots](/docs/extras/multiple-bots) in the same application, explicitly specify the bot name:
|
||||
|
||||
```typescript {8} title="src/echo/echo.service.ts"
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectBot } from 'nestjs-telegraf';
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';
|
||||
|
||||
@Injectable()
|
||||
export class EchoService {
|
||||
constructor(@InjectBot('cats') private bot: Telegraf<TelegrafContext>) {}
|
||||
...
|
||||
}
|
||||
```
|
13
website/docs/extras/middlewares.md
Normal file
13
website/docs/extras/middlewares.md
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: middlewares
|
||||
title: Middlewares
|
||||
sidebar_label: Middlewares
|
||||
slug: /extras/middlewares
|
||||
---
|
||||
|
||||
`nestjs-telegraf` has support of the Telegraf middleware packages. To use an existing middleware package, simply import it and add it to the middlewares array:
|
||||
```typescript
|
||||
TelegrafModule.forRoot({
|
||||
middlewares: [session()],
|
||||
}),
|
||||
```
|
74
website/docs/extras/multiple-bots.md
Normal file
74
website/docs/extras/multiple-bots.md
Normal file
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: multiple-bots
|
||||
title: Multiple bots
|
||||
sidebar_label: Multiple bots
|
||||
slug: /extras/multiple-bots
|
||||
---
|
||||
|
||||
In some cases, you may need to run multiple bots at the same time. This can also be achieved with this module. To work with multiple bots, first create the bots. In this case, bot naming becomes mandatory.
|
||||
```typescript
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { TelegrafModule } from 'nestjs-telegraf';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
TelegrafModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
botName: 'cat',
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
token: configService.get<string>('CAT_BOT_TOKEN'),
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
TelegrafModule.forRootAsync({
|
||||
imports: [ConfigModule.forFeature(telegrafModuleConfig)],
|
||||
botName: 'dog',
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
token: configService.get<string>('DOG_BOT_TOKEN'),
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
```
|
||||
|
||||
:::caution
|
||||
Please note that you shouldn't have multiple bots without a name, or with the same name, otherwise they will get overridden.
|
||||
:::
|
||||
|
||||
You can also inject the `Bot` for a given bot:
|
||||
```typescript
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectBot, Telegraf, Context } from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class EchoService {
|
||||
constructor(@InjectBot('cat') private catBot: Telegraf<Context>) {}
|
||||
}
|
||||
```
|
||||
To inject a given `Bot` to a custom provider (for example, factory provider), use the `getBotToken()` function passing the name of the bot as an argument.
|
||||
```typescript
|
||||
{
|
||||
provide: CatsService,
|
||||
useFactory: (catBot: Telegraf<Context>) => {
|
||||
return new CatsService(catBot);
|
||||
},
|
||||
inject: [getBotToken('cat')],
|
||||
}
|
||||
```
|
||||
Another useful feature of the `nestjs-telegraf` module is the ability to choose which modules should handle updates for each launched bot. By default, module searches for handlers throughout the whole app. To limit this scan to only a subset of modules, use the include property.
|
||||
|
||||
```typescript
|
||||
TelegrafModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
botName: 'cat',
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
token: configService.get<string>('CAT_BOT_TOKEN'),
|
||||
include: [CatsModule],
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
```
|
Reference in New Issue
Block a user