nestjs-telegraf/website/versioned_docs/version-2.6/extras/bot-injection.md

36 lines
1.1 KiB
Markdown
Raw Normal View History

2023-07-23 15:28:11 +03:00
---
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 '@maks1ms/nestjs-telegraf';
2023-07-23 15:28:11 +03:00
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](/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 '@maks1ms/nestjs-telegraf';
2023-07-23 15:28:11 +03:00
import { Telegraf } from 'telegraf';
import { TelegrafContext } from '../common/interfaces/telegraf-context.interface.ts';
@Injectable()
export class EchoService {
constructor(@InjectBot('cats') private bot: Telegraf<TelegrafContext>) {}
...
}
```