yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.17k stars 1.5k forks source link

node-telegram-bot-api in nodejs not working with socks5 proxy #1072

Open mshamilkhan opened 1 year ago

mshamilkhan commented 1 year ago

I am making the bot of telegram through nodejs by using node-telegram-bot-api but the thing is that telegram is banned in Pakistan and we have to use proxy in out nodejs program. I used the socks5 proxy and after the whole program I see a problem saying --->

error: [polling_error] {"code":"EFATAL","message":"EFATAL: Error: connect ECONNREFUSED ::1:1080"}

I have checked my firewall and allowed all the ports from inbound and outbounds to all my ports. I have chosen 2 different methods of proxy 1 is of request and the other one is simple with polling.

in my .env file I have other port like 3000 or 3002 but even then it tries to connect with 1080 which is a socks5 port and it does not exist in computer may be.

` METHOD 1: const express = require('express'); const app = express(); const telegramBot = require('node-telegram-bot-api'); const Agent = require('socks5-https-client/lib/Agent'); const TOKEN = "6110349259:AAFDIH6oy6AAp52jU6Z8yINc_qROC-CE0Pg"; const bot = new telegramBot(TOKEN, { polling: true, request: { agentClass: Agent, agentOptions: { socksHost: process.env.PROXY_SOCKS5_HOST, socksPort: parseInt(process.env.PROXY_SOCKS5_PORT), // If authorization is needed: socksUsername: process.env.PROXY_SOCKS5_USERNAME, socksPassword: process.env.PROXY_SOCKS5_PASSWORD } } })

** METHOD 2:**

const request = require('request') const express = require('express'); const app = express(); const TelegramBot = require('node-telegram-bot-api'); const Agent = require('socks5-https-client/lib/Agent'); const TOKEN = "6110349259:AAFDIH6oy6AAp52jU6Z8yINc_qROC-CE0Pg"; var options = { url: "https://api.telegram.org/bot1111111...11/getMe", agentClass: Agent, agentOptions: { socksHost: process.env.PROXY_SOCKS5_HOST, socksPort: parseInt(process.env.PROXY_SOCKS5_PORT), // If authorization is needed: socksUsername: process.env.PROXY_SOCKS5_USERNAME, socksPassword: process.env.PROXY_SOCKS5_PASSWORD } } request(options, function (error, res, html) { if (!error && res.statusCode == 200) { console.log(html) } else { console.log(error) } })

const bot = new TelegramBot(TOKEN, { polling: true, request: { agentClass: Agent,your text agentOptions: { socksHost: process.env.PROXY_SOCKS5_HOST, socksPort: parseInt(process.env.PROXY_SOCKS5_PORT), // If authorization is needed: socksUsername: process.env.PROXY_SOCKS5_USERNAME, socksPassword: process.env.PROXY_SOCKS5_PASSWORD } } });`

Anmours commented 1 year ago

Have you solved them

prashant5025 commented 1 year ago

Based on the information you have provided, it appears that you are encountering an error with your Node.js program while attempting to use the Telegram Bot API with a SOCKS5 proxy server. Specifically, you are receiving an error message that indicates that the connection to the proxy server is being refused.

To troubleshoot this issue, there are several steps that you can take. First, you should verify that the SOCKS5 proxy server is running and configured correctly. It is also important to double-check your SOCKS5 proxy settings to ensure that the values in your environment variables are correct and that you are using the correct host and port.

If these steps do not resolve the issue, you may want to consider trying a different SOCKS5 proxy server or using a different method of connecting to Telegram, such as the MTProto protocol. Additionally, disabling your firewall temporarily may help to determine if it is causing the issue.

As a software engineer, I would also recommend checking your code to ensure that you are not using the wrong environment variables or hardcoded proxy server settings. It is important to ensure that your code is using the correct environment variables and not hardcoded values that may be incorrect.

I hope this information is helpful to you in resolving your issue. If you have any further questions or concerns, please do not hesitate to let me know.

Anmours commented 1 year ago

I am facing the same problem as you. If you solve it, please let me know. Thank you