Node-telegram-bot-api: Way to run bot behind proxy

Created on 11 May 2016  路  11Comments  路  Source: yagop/node-telegram-bot-api

I need to use the bot behind a corporate proxy.

I've tried to find where to put so I could make a pull request, but couldn't

question

Most helpful comment

v0.25.0 added the ability to pass request options. Therefore, you may do something like:

const bot = new TelegramBot(token, {
  polling: true,
  request: {
    proxy: "http://127.0.0.1:1234",
  },
});

See request's docs on more information on the proxy!

Otherwise, if you are using webhooks, please proceed to issue #253!

(PS: Join our Telegram channel for updates)

All 11 comments

You need a web server that forwards messages to the Telegram Bot API in any case, so being behind is no problem as long as you control the proxy.

You can see an example in this file of a server listening on 127.0.0.1:8443 and forwarding messages to this API (pintbot in my case).

Then (eg. Nginx) for your reverse proxy something like:

location /yourtelegrampath {
    proxy_pass http://127.0.0.1:8443;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  }

EDIT: This is assuming your reverse proxy is at example.com and your Telegram Bot's webhook is example.com/yourtelegrampath.

I'm actually using Long Polling, and the proxy won't let the requests go out without auth.
I've set up CNTLM and I'm able to make HTTP and HTTPS requests if I can set the module to use my proxy, wich authenticates with the corporate one and redirects the request
What I'm looking for, actually, is something like

request.setProxy('http://127.0.0.1:1234');

v0.25.0 added the ability to pass request options. Therefore, you may do something like:

const bot = new TelegramBot(token, {
  polling: true,
  request: {
    proxy: "http://127.0.0.1:1234",
  },
});

See request's docs on more information on the proxy!

Otherwise, if you are using webhooks, please proceed to issue #253!

(PS: Join our Telegram channel for updates)

Thanks so much!

Hi guys! I'm using

const bot = new TelegramBot(token, {
  polling: true,
  request: {
    proxy: "http://[user]:[pw]@127.0.0.1:1234",
  },
});

When I access the URL on the browser it works, so i know it is not blocked.

But it still fails... The string I used is the same i get from the command

npm config get proxy

And npm works great.

Any thoughts?

Just tested it on basic request an it works...

var request = require('request');
request({
        url: 'http://www.google.com',
        proxy: "http://[user]:[pw]@127.0.0.1:1234"
      } , function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage.
  }
})

We hope you are using the latest version i.e. v0.25.0!

Yup!

$ npm list node-telegram-bot-api
*project*@0.0.1 *pathHere*
`-- [email protected]

You read through the Proxy Documentation?

Yup...

I think i figured it out... node-telegram-bot-api is not reaching the _request method. Here's the stack:

Unhandled rejection RequestError: Error: getaddrinfo EAI_AGAIN api.telegram.org:443
    at new RequestError (*myPath*\node_modules\request-promise-core\lib\errors.js:14:15)
    at Request.plumbing.callback (*myPath*\node_modules\request-promise-core\lib\plumbing.js:87
:29)
    at Request.RP$callback [as _callback] (*myPath*\node_modules\request-promise-core\lib\plumb
ing.js:46:31)
    at self.callback (*myPath*\node_modules\request\request.js:186:22)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.onRequestError (*myPath*\node_modules\request\request.js:845:8)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at TLSSocket.socketErrorListener (_http_client.js:310:9)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at connectErrorNT (net.js:1022:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

As far as I could see, node-telegram-bot-api is not even getting it's _request method (I inserted a console.log() there, it never came out...

Just in case it helps anyone, I solved it by setting

process.env.http_proxy="http://[user]:[pw]@127.0.0.1:1234"
process.env.https_proxy="http://[user]:[pw]@127.0.0.1:1234"

Since the request module always respects env variables, there's no need to set the request option anymore and it probably works with older versions of node-telegram-bot-api as well.

Also, one can skip hardcoding the proxy by using the env :-)

Thanks @GochoMugo for your replies!

Was this page helpful?
0 / 5 - 0 ratings