lib | ||
.gitignore | ||
.npmignore | ||
.prettierrc | ||
.travis.yml | ||
CHANGELOG.md | ||
index.d.ts | ||
index.js | ||
index.ts | ||
LICENSE.md | ||
package-lock.json | ||
package.json | ||
README.md | ||
renovate.json | ||
tsconfig.json | ||
tslint.json |
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
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 handlingcallback_data
actions with regular expressions.
Telegraf middlewares usage
See https://github.com/bukhalo/nestjs-telegraf/issues/7#issuecomment-577582322
Telegraf proxy usage
/* bot.config.ts */
import { registerAs } from '@nestjs/config'
interface Config {
token: string
socksHost: string
socksPort: string | number
socksUser: string
socksPassword: string
}
export default registerAs(
'bot',
(): Config => ({
token: process.env.TELEGRAM_BOT_TOKEN,
socksHost: process.env.TELEGRAM_BOT_SOCKS_HOST,
socksPort: process.env.TELEGRAM_BOT_SOCKS_PORT,
socksUser: process.env.TELEGRAM_BOT_SOCKS_USER,
socksPassword: process.env.TELEGRAM_BOT_SOCKS_PASS,
}),
);
/* telegraf-config.service.ts */
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { TelegrafModuleOptions, TelegrafOptionsFactory } from 'nestjs-telegraf'
import { SocksProxyAgent } from 'socks-proxy-agent'
@Injectable()
export class TelegrafConfigService implements TelegrafOptionsFactory {
private agent
constructor(private readonly configService: ConfigService) {}
createTelegrafOptions(): TelegrafModuleOptions {
const proxyConfig = {
host: this.configService.get('bot.socksHost'),
port: this.configService.get('bot.socksPort'),
userId: this.configService.get('bot.socksUser'),
password: this.configService.get('bot.socksPassword'),
}
this.agent = new SocksProxyAgent(proxyConfig)
return {
token: this.configService.get('bot.token'),
telegrafOptions: { telegram: { agent: this.agent } },
};
}
}
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
- Authors - Aleksandr Bukhalo & Igor Kamyshev
- Maintainers - Aleksandr Bukhalo
- Website - https://nestjs.com
License
Nest is MIT licensed.