Merge pull request #31 from alexeynobody/master

Add support for proxy and other features in TelegrafOptions
This commit is contained in:
Aleksandr Bukhalo 2020-02-29 20:51:25 +03:00 committed by GitHub
commit af1ef0a201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 10 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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) {