R
R
Rustem2020-02-18 23:21:59
typescript
Rustem, 2020-02-18 23:21:59

How to release a telegraf-based telegram bot through a proxy server?

Hello!

I am writing a telegram bot using the telegraf library. I found a simple JS example:

const telegraf = require('telegraf');
const SocksAgent = require('socks5-https-client/lib/Agent');
const socksAgent = new SocksAgent({
  socksHost: 'localhost',
  socksPort: 9050
});
const telegrammToken = 'token';
const bot = new telegraf(telegrammToken, {
  telegram: {
    agent: socksAgent
  }
});
bot.start(ctx => ctx.reply('Welcome'));
bot.help(ctx => ctx.reply('Send me a sticker'));
bot.on('sticker', ctx => ctx.reply(''));
bot.hears('hi', ctx => ctx.reply('Hey there'));
bot.launch();

It is working.

But I'm writing in TypeScript and trying to rewrite:
import SocksProxyAgent from 'socks-proxy-agent/dist/agent';
import { Telegraf } from 'telegraf';
import { telegramToken } from './config/botConfig';
const socksAgent = new SocksProxyAgent({
  host: 'localhost',
  port: 9050
});
const bot = new Telegraf(telegramToken, {
  telegram: {
    agent: socksAgent
  }
});
...


At first, I cursed that there were no types.
declared (as best he could)
declare module 'socks5-https-client/lib/Agent' {
  import * as tls from 'tls';
  import * as https from 'https';

  interface IAgentOptions extends https.AgentOptions, tls.ConnectionOptions {
    rejectUnauthorized?: boolean;
    maxCachedSessions?: number;
  }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  class Agent extends https.Agent {
    constructor(options?: IAgentOptions);
    options: IAgentOptions;
    private createConnection(options: {}): any;
  }
}

and now on the line
const bot = new Telegraf(telegramToken, {
  telegram: {
    agent: socksAgent
  }
});

error:
No overload matches this call.
...

tell me what's wrong ...
maybe someone wrote a bot in TypeScript and Telegraf using a proxy.

Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
2
2CHEVSKII, 2020-02-18
@h0kum

Most likely, because of import destructuring (or whatever this {} crap is called). Try import Telegraf instead of import {Telegraf}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question