yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.47k stars 1.54k forks source link

Socks Proxy. [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:" #1131

Open milad2golnia opened 1 year ago

milad2golnia commented 1 year ago

Bug Report

I have read:

I am using the latest version of the library.

Expected Behavior

I'm trying to use a socks5 proxy with this module but I faced error. First off, I'm sure that socks proxy is working correctly and I tested it with curl like below:

curl --proxy "socks5://127.0.0.1:9050" https://github.com

I use the socks proxy in my code like this:

// @ts-ignore
import Agent from 'socks5-http-client/lib/Agent.js';

this.bot = new TelegramBot(token, {
      polling: true,
      request: {
        agentClass: Agent,
        agentOptions: {
          socksHost: '127.0.0.1',
          socksPort: 9050,
        },
      } as any,
    });
    console.log( 'test');
    const result = await this.bot.sendMessage(
      chatID,
      'Sending text through proxy is working correctly.'
    );

And it raises following error:

test
error: [polling_error] {"code":"EFATAL","message":"EFATAL: TypeError [ERR_INVALID_PROTOCOL]: Protocol \"https:\" not supported. Expected \"http:\""}
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

FatalError: EFATAL: TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"
    at new RequestError (/home/milad/Projects/backup/node_modules/request-promise-core/lib/errors.js:14:15)
    at plumbing.callback (/home/milad/Projects/backup/node_modules/request-promise-core/lib/plumbing.js:87:29)
    at Request.RP$callback [as _callback] (/home/milad/Projects/backup/node_modules/request-promise-core/lib/plumbing.js:46:31)
    at self.callback (/home/milad/Projects/backup/node_modules/request/request.js:185:22)
    at Request.emit (node:events:514:28)
    at Request.start (/home/milad/Projects/backup/node_modules/request/request.js:753:10)
    at Request.write (/home/milad/Projects/backup/node_modules/request/request.js:1491:10)
    at end (/home/milad/Projects/backup/node_modules/request/request.js:549:18)
    at Immediate._onImmediate (/home/milad/Projects/backup/node_modules/request/request.js:578:7)
    at process.processImmediate (node:internal/timers:478:21)
    at plumbing.init (/home/milad/Projects/backup/node_modules/request-promise-core/lib/plumbing.js:36:28)
    at Request.RP$initInterceptor [as init] (/home/milad/Projects/backup/node_modules/request-promise-core/configure/request2.js:41:27)
    at new Request (/home/milad/Projects/backup/node_modules/request/request.js:127:8)
    at request (/home/milad/Projects/backup/node_modules/request/index.js:53:10)
    at TelegramBot._request (/home/milad/Projects/backup/node_modules/node-telegram-bot-api/src/telegram.js:286:12)
From previous event:
    at TelegramBot._request (/home/milad/Projects/backup/node_modules/node-telegram-bot-api/src/telegram.js:300:15)
    at TelegramBot.sendMessage (/home/milad/Projects/backup/node_modules/node-telegram-bot-api/src/telegram.js:915:17)
    at BotController.<anonymous> (file:///home/milad/Projects/backup/build/lib/transformer/telegram/index.js:70:43)
    at Generator.next (<anonymous>)
    at file:///home/milad/Projects/backup/build/lib/transformer/telegram/index.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (file:///home/milad/Projects/backup/build/lib/transformer/telegram/index.js:3:12)
    at BotController.uploadFile (file:///home/milad/Projects/backup/build/lib/transformer/telegram/index.js:54:16)
    at file:///home/milad/Projects/backup/build/index.js:60:24
    at Generator.next (<anonymous>)
    at file:///home/milad/Projects/backup/build/index.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (file:///home/milad/Projects/backup/build/index.js:3:12)
    at dailyDockerPostgresMinioTelegram (file:///home/milad/Projects/backup/build/index.js:50:12)
    at file:///home/milad/Projects/backup/build/index.js:139:1
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:308:24)
    at async loadESM (node:internal/process/esm_loader:42:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12) {
  code: 'EFATAL'
}

Node.js v20.6.1

Actual Behavior

The actual behavior is returning normally and seeing the message in the channel with given chatID.

Steps to reproduce the Behavior

Setup a socks proxy and use my code.

milad2golnia commented 1 year ago

I tested http://localhost:9080 with following change:

    this.bot = new TelegramBot(token, {
      polling: true,
      request: {
        proxy: 'http://localhost:9080',
      } as any,
    });

But it gives me the same error so it seems the problem is not related to using socks proxy but using proxy in general.