NestJS Telega
nestjs-telega integrates telegraf-hardened with NestJS. It discovers decorated providers, connects their handlers to a Telegraf bot, and preserves NestJS guards, pipes, interceptors and exception filters.
It supports ordinary update handlers, scenes and wizards, default reply options, callback/inline-query results, and multiple isolated bot instances.
Installation
The default package line uses telegraf-hardened and requires Node.js 18 or newer:
npm install nestjs-telega telegraf-hardenedyarn add nestjs-telega telegraf-hardenedThe legacy Telegraf-based line lives in the telegraf branch and is installed explicitly with the telegraf dist-tag:
npm install nestjs-telega@telegraf telegrafyarn add nestjs-telega@telegraf telegrafQuick start
Import TelegrafModule once in the root module. Keep the token in an environment variable or configuration provider rather than source code.
import { Module } from '@nestjs/common';
import { TelegrafModule } from 'nestjs-telega';
import { BotUpdate } from './bot.update';
@Module({
imports: [
TelegrafModule.forRoot({
token: process.env.TELEGRAM_BOT_TOKEN!,
}),
],
providers: [BotUpdate],
})
export class AppModule {}Create an update provider. A returned string is sent as a reply; use @Ctx() when the handler needs the full Telegraf context.
import { Command, Ctx, Start, Update } from 'nestjs-telega';
import type { Context } from 'telegraf-hardened';
@Update()
export class BotUpdate {
@Start()
onStart(): string {
return 'Welcome!';
}
@Command('chatid')
onChatId(@Ctx() ctx: Context): string {
return `Your chat id is ${ctx.chat?.id}`;
}
}Continue with receiving updates, decorators and listener results, or asynchronous configuration.