yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.45k stars 1.53k forks source link

Using proxy in typescript #818

Closed ali-heidari closed 2 years ago

ali-heidari commented 4 years ago

Hi everyone,

I want to create a simple telegram robot using typescript. I need set a proxy, used following code:

const bot = new TelegramBot(config.bot.token, {
    polling: true,
    request: {
        agentClass: Agent,
        agentOptions: {
            socksHost: config.proxy.socksHost,
            socksPort: config.proxy.socksPort,
            // If authorization is needed:
             socksUsername: config.proxy.socksUsername,
             socksPassword: config.proxy.socksPassword
        }
    }
});

but got a compile-time error which says:

 Object literal may only specify known properties, and 'socksHost' does not exist in type 'AgentOptions | AgentOptions'

I read other related issues but it seems there is a lack of type definitions. Then used this one:

  const bot = new TelegramBot(this._token, {
      polling: true,
      request: {
        url: "api.telegram.org",
        proxy: "127.0.0.1:1234",
      },
    });

When i run it, gives this error...

code:"EFATAL"
message:"EFATAL: Error: tunneling socket could not be established, cause=connect EINVAL 0.0.4.210:80 - Local (0.0.0.0:0)"
stack:"RequestError: Error: tunneling socket could not be established, cause=connect EINVAL 0.0.4.210:80 - Local (0.0.0.0:0)

Can anyone suggest to me how to set a proxy with typescript?

ali-heidari commented 4 years ago

My co-worker asked this question in StackOverflow and got a comment which leads us to a workaround. You can check it here: how-to-use-proxy-in-typescript

You can put @ts-ignore on top of the nagging line. With this trick, both codes mentioned in previous comment, work like a charm.

const bot = new TelegramBot(this._token, {
      polling: true,
      // @ts-ignore
      request: {
        // url: "api.telegram.org",
        proxy: "http://127.0.0.1:1234",
      },
    });

As I said, this is just a trick to make it work, But the solution would be adding the type definitions.