🤖 Powerful Nest module for easy and fast creation Telegram bots
Go to file
renovate[bot] 4f686992e7
chore(deps): update dependency lint-staged to v10.0.5 (#21)
Co-authored-by: WhiteSource Renovate <renovatebot@gmail.com>
2020-01-30 11:18:04 +03:00
lib feat(lib/telegraf.service.ts): set bot public 2020-01-23 21:10:53 +03:00
.gitignore improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00
.npmignore improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00
.prettierrc chore: publish 0.7.0 release (#6) 2020-01-15 17:30:47 +03:00
.travis.yml ci: setup travis 2019-02-28 10:30:51 +02:00
CHANGELOG.md chore(release): 0.5.1 2019-07-26 10:50:49 +03:00
index.d.ts improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00
index.js improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00
index.ts improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00
LICENSE.md chore(meta): update package info & readme 2020-01-12 14:54:23 +03:00
package-lock.json chore(deps): update dependency lint-staged to v10.0.5 (#21) 2020-01-30 11:18:04 +03:00
package.json chore(deps): update dependency lint-staged to v10.0.5 (#21) 2020-01-30 11:18:04 +03:00
README.md docs(readme.md): basic information 2020-01-24 10:31:15 +03:00
renovate.json chore(renovate.json): configure renovate bot 2020-01-23 21:28:11 +03:00
tsconfig.json improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00
tslint.json improvement(workflow): rewrite all workflow 2020-01-12 14:18:48 +03:00

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Telegraf module for Nest.

Installation

$ npm i nestjs-telegraf telegraf

Usage

An example of package usage

/* bot.module.ts */

import { Module, OnModuleInit, Logger } from '@nestjs/common'
import { ModuleRef } from '@nestjs/core'
import { ConfigModule } from '@nestjs/config'
import { TelegrafModule, TelegrafService } from 'nestjs-telegraf'
import botConfig from './bot.config'
import { TelegrafConfigService } from './telegraf-config.service'
import { BotService } from './bot.service'

@Module({
  imports: [
    TelegrafModule.fromFactory({
      imports: [ConfigModule.forFeature(botConfig)],
      useClass: TelegrafConfigService,
    }),
  ],
  exports: [TelegrafModule],
  providers: [BotService],
})
export class BotModule implements OnModuleInit {
  constructor(
    private readonly moduleRef: ModuleRef,
    private readonly telegrafService: TelegrafService
  ) {}

  onModuleInit() {
    this.telegrafService.init(this.moduleRef)
    this.telegrafService.startPolling()
  }
}
/* telegraf-config.service.ts */

import { Injectable } from '@nestjs/common'
import { TelegrafOptionsFactory, TelegrafModuleOptions } from 'nestjs-telegraf'
import { ConfigService } from '@nestjs/config'

@Injectable()
export class TelegrafConfigService implements TelegrafOptionsFactory {
  constructor(private readonly configService: ConfigService) {}

  createTelegrafOptions(): TelegrafModuleOptions {
    return {
      token: this.configService.get('bot.token'),
    }
  }
}
/* bot.config.ts */

import { registerAs } from '@nestjs/config'

interface Config {
  token: string
}

export default registerAs(
  'bot',
  (): Config => ({
    token: process.env.TELEGRAM_BOT_TOKEN,
  })
)

Telegraf

Telegraf methods usage

You can decorate any Telegraf method with @TelegramActionHandler decorator.

/* bot.service.ts */

import { Injectable } from '@nestjs/common'
import { TelegrafTelegramService } from 'nestjs-telegraf'
import { ContextMessageUpdate } from 'telegraf'

@Injectable()
export class BotService {
  /* This decorator handle /start command */
  @TelegramActionHandler({ onStart: true })
  async onStart(ctx: ContextMessageUpdate) {
    await ctx.reply('/start command reply')
  }
}
Today available actions for decorator:
  • onStart Handler for /start command.

  • command Command handling.

  • message Registers middleware for handling text messages.

  • action Registers middleware for handling callback_data actions with regular expressions.

Telegraf middlewares usage

See https://github.com/bukhalo/nestjs-telegraf/issues/7#issuecomment-577582322

Telegram

Telegram methods usage

Inject TelegrafTelegramService from nestjs-telegraf package for use Telegram instance from telegraf package.

/* bot.service.ts */

import { Injectable } from '@nestjs/common'
import { TelegrafTelegramService, TelegramActionHandler } from 'nestjs-telegraf'
import { ContextMessageUpdate } from 'telegraf'

@Injectable()
export class BotService {
  constructor(
    private readonly telegrafTelegramService: TelegrafTelegramService
  ) {}

  @TelegramActionHandler({ onStart: true })
  async start(ctx: ContextMessageUpdate) {
    const me = await this.telegrafTelegramService.getMe()
    console.log(me)
  }
}

Examples

You can see the basic use of the package in this repository: https://github.com/bukhalo/nestjs-telegraf-sample

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

People

License

Nest is MIT licensed.