Code Monkey home page Code Monkey logo

nestjs-telegraf's Introduction

NestJS Telegraf npm GitHub last commit NPM

NestJS Telegraf โ€“ powerful solution for creating Telegram bots.

This package uses the best of the NodeJS world under the hood. Telegraf is the most powerful library for creating bots and 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.
  • Full support of NestJS guards, interceptors, filters and pipes!

Documentation

If you want to dive fully into NestJS Telegraf then don't waste your time in this dump, check out the documentation site.

Installation

$ npm i nestjs-telegraf telegraf

Usage

Once the installation process is complete, we can import the TelegrafModule into the root AppModule:

import { Module } from '@nestjs/common';
import { TelegrafModule } from 'nestjs-telegraf';

@Module({
  imports: [
    TelegrafModule.forRoot({
      token: 'TELEGRAM_BOT_TOKEN',
    })
  ],
})
export class AppModule {}

Then create app.update.ts file and add some decorators for handling Telegram bot API updates:

import {
  Update,
  Start,
  Help,
  On,
  Hears,
  Context,
} from 'nestjs-telegraf';
import { AppService } from './app.service';
import { Context } from './context.interface';

@Update()
export class AppUpdate {
  constructor(private readonly appService: AppService)

  @Start()
  async startCommand(ctx: Context) {
    await ctx.reply('Welcome');
  }

  @Help()
  async helpCommand(ctx: Context) {
    await ctx.reply('Send me a sticker');
  }

  @On('sticker')
  async onSticker(ctx: Context) {
    await ctx.reply('๐Ÿ‘');
  }

  @Hears('hi')
  async hearsHi(ctx: Context) {
    await ctx.reply('Hey there');
  }
}

Telegraf instance access

If you want to use Telegraf instance directly, you can use @InjectBot for that.

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>) {}
  ...
}

See more on a docs page: https://nestjs-telegraf.vercel.app/extras/bot-injection

nestjs-telegraf's People

Contributors

renovate[bot] avatar evilsprut avatar morb0 avatar dependabot[bot] avatar igorkamyshev avatar renovate-bot avatar allcontributors[bot] avatar alexey-nobody avatar toolstik avatar xtcry avatar gibson avatar galkin avatar lamuertepeluda avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.