diff --git a/README.md b/README.md index 03a6c42..a62f10c 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,68 @@ export class BotService { See https://github.com/bukhalo/nestjs-telegraf/issues/7#issuecomment-577582322 +#### Telegraf proxy usage + +```typescript + +/* 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, + }), +); + +``` + +```typescript + +/* 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 diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index 14338bb..64b70a6 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -1,8 +1,10 @@ import { ModuleMetadata, Type } from '@nestjs/common/interfaces' +import { TelegrafOptions } from 'telegraf' export interface TelegrafModuleOptions { token: string sitePublicUrl?: string + telegrafOptions?: TelegrafOptions } export interface TelegrafOptionsFactory { diff --git a/lib/telegraf.service.ts b/lib/telegraf.service.ts index 48a27a5..f927acb 100644 --- a/lib/telegraf.service.ts +++ b/lib/telegraf.service.ts @@ -1,17 +1,16 @@ -import { Injectable, Inject, Logger } from '@nestjs/common' +import { Inject, Injectable, Logger } from '@nestjs/common' import { ModuleRef } from '@nestjs/core' -import Telegraf, { ContextMessageUpdate } from 'telegraf' import { flatten, head } from 'lodash' - -import { TelegramCatch, TelegramActionHandler } from './decorators' -import { TokenInjectionToken } from './telegraf.constants' +import Telegraf, { ContextMessageUpdate } from 'telegraf' +import { TelegramActionHandler, TelegramCatch } from './decorators' +import { InvalidConfigurationException } from './exeptions' import { + ContextTransformer, + Handler, TelegrafOptionsFactory, TelegramErrorHandler, - Handler, - ContextTransformer, } from './interfaces' -import { InvalidConfigurationException } from './exeptions' +import { TokenInjectionToken } from './telegraf.constants' @Injectable() export class TelegrafService { @@ -23,9 +22,13 @@ export class TelegrafService { public constructor( @Inject(TokenInjectionToken) options: TelegrafOptionsFactory ) { - const { token, sitePublicUrl } = options.createTelegrafOptions() + const { + token, + sitePublicUrl, + telegrafOptions, + } = options.createTelegrafOptions() this.sitePublicUrl = sitePublicUrl - this.bot = new Telegraf(token) + this.bot = new Telegraf(token, telegrafOptions) } public init(ref: ModuleRef, devMode: boolean = false) {