2021-01-05 17:18:22 +03:00
# NestJS Telegraf ![npm](https://img.shields.io/npm/dm/nestjs-telegraf) ![GitHub last commit](https://img.shields.io/github/last-commit/bukhalo/nestjs-telegraf) ![NPM](https://img.shields.io/npm/l/nestjs-telegraf)
2020-03-31 18:37:42 +03:00
2021-01-05 17:18:22 +03:00
< img align = "right" width = "95" height = "148" title = "NestJS logotype"
src="https://nestjs.com/img/logo-small.svg">
2020-01-12 14:54:23 +03:00
2021-01-05 17:18:22 +03:00
NestJS Telegraf – powerful solution for creating Telegram bots.
This package uses the best of the NodeJS world under the hood. [Telegraf ](https://github.com/telegraf/telegraf ) is the most powerful library for creating bots and [NestJS ](https://github.com/nestjs ) is a progressive framework for creating well-architectured applications. This module provides fast and easy way for creating Telegram bots and deep integration with your NestJS application.
**Features**
- Simple. Easy to use.
- Ton of decorators available out of the box for handling bot actions.
- Ability to create custom decorators.
- Scenes support.
- Telegraf plugins and custom plugins support.
- Ability to run multiple bots simultaneously.
2021-02-14 23:59:26 +03:00
- Full support of NestJS guards, interceptors, filters and pipes!
2020-01-12 14:54:23 +03:00
2020-09-10 00:19:02 +03:00
## Documentation
2020-09-10 00:26:12 +03:00
If you want to dive fully into NestJS Telegraf then don't waste your time in this dump, check out the [documentation site ](https://nestjs-telegraf.vercel.app ).
2020-09-10 00:19:02 +03:00
2020-01-12 14:54:23 +03:00
## Installation
```bash
2021-01-11 16:17:32 +03:00
$ npm i nestjs-telegraf telegraf
2019-07-16 18:56:02 +03:00
```
2021-01-05 17:18:22 +03:00
## Usage
2020-09-10 00:19:02 +03:00
Once the installation process is complete, we can import the `TelegrafModule` into the root `AppModule` :
2020-01-24 10:31:15 +03:00
```typescript
2020-03-19 20:50:04 +03:00
import { Module } from '@nestjs/common';
import { TelegrafModule } from 'nestjs-telegraf';
2022-06-24 00:34:02 +03:00
import { AppUpdate } from './app.update';
2020-01-24 10:31:15 +03:00
@Module ({
imports: [
2020-03-19 20:50:04 +03:00
TelegrafModule.forRoot({
token: 'TELEGRAM_BOT_TOKEN',
2022-05-24 13:10:04 +03:00
}),
2020-01-24 10:31:15 +03:00
],
2022-06-24 00:34:02 +03:00
providers: [AppUpdate],
2020-01-24 10:31:15 +03:00
})
2020-03-19 20:50:04 +03:00
export class AppModule {}
2020-01-24 10:31:15 +03:00
```
2021-01-05 17:18:22 +03:00
Then create `app.update.ts` file and add some decorators for handling Telegram bot API updates:
2020-01-24 10:31:15 +03:00
```typescript
2020-03-19 20:50:04 +03:00
import {
2021-01-05 17:18:22 +03:00
Update,
2020-08-14 12:42:57 +03:00
Start,
Help,
On,
Hears,
2020-05-03 12:14:17 +03:00
Context,
2020-03-19 20:50:04 +03:00
} from 'nestjs-telegraf';
2021-01-05 17:18:22 +03:00
import { AppService } from './app.service';
import { Context } from './context.interface';
@Update ()
export class AppUpdate {
constructor(private readonly appService: AppService)
2020-01-24 10:31:15 +03:00
2020-08-14 12:42:57 +03:00
@Start ()
2021-01-05 17:18:22 +03:00
async startCommand(ctx: Context) {
await ctx.reply('Welcome');
2020-03-19 20:50:04 +03:00
}
2020-01-24 10:31:15 +03:00
2020-08-14 12:42:57 +03:00
@Help ()
2021-01-05 17:18:22 +03:00
async helpCommand(ctx: Context) {
await ctx.reply('Send me a sticker');
2020-03-19 20:50:04 +03:00
}
2020-01-24 10:31:15 +03:00
2020-08-14 12:42:57 +03:00
@On ('sticker')
2021-01-05 17:18:22 +03:00
async onSticker(ctx: Context) {
await ctx.reply('👍');
2020-03-19 20:50:04 +03:00
}
2020-01-24 10:31:15 +03:00
2020-08-14 12:42:57 +03:00
@Hears ('hi')
2021-01-05 17:18:22 +03:00
async hearsHi(ctx: Context) {
await ctx.reply('Hey there');
2020-01-24 10:31:15 +03:00
}
}
```
2021-08-05 12:45:35 +03:00
## Telegraf instance access
If you want to use `Telegraf` instance directly, you can use `@InjectBot` for that.
```typescript
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 > ) {}
...
}
```
2022-05-24 13:10:04 +03:00
See more on a docs page: https://nestjs-telegraf.vercel.app/extras/bot-injection