Skip to main content
Version: 2.6

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:

src/echo/echo.service.ts
import { Injectable } from '@nestjs/common';
import { InjectBot } from '@maks1ms/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 in the same application, explicitly specify the bot name:

src/echo/echo.service.ts
import { Injectable } from '@nestjs/common';
import { InjectBot } from '@maks1ms/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>) {}
...
}