mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 23:14:39 +03:00
Merge pull request #31 from alexeynobody/master
Add support for proxy and other features in TelegrafOptions
This commit is contained in:
commit
af1ef0a201
62
README.md
62
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
|
||||
|
@ -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 {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user