nestjs-telegraf/docs/extras/bot-injection.md

30 lines
1.0 KiB
Markdown
Raw Normal View History

2023-07-24 16:02:21 +03:00
# Bot injection
2021-02-14 23:59:26 +03:00
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>) {}
...
}
```
2021-08-05 13:35:47 +03:00
If you run [multiple bots](/extras/multiple-bots) in the same application, explicitly specify the bot name:
2021-02-14 23:59:26 +03:00
```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>) {}
...
}
```