Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up a proxy for my Telegram bot using Node.js


How do I set up a proxy for my Telegram bot using Node.js


To set up a proxy for your Telegram bot using Node.js, you can utilize the `node-telegram-bot-api` library along with a SOCKS5 proxy or an HTTP proxy. Below are the steps and code examples for both methods.

Using SOCKS5 Proxy

1. Install Required Packages: You need to install `node-telegram-bot-api` and `socks5-https-client`.

bash
   npm install node-telegram-bot-api socks5-https-client
   

2. Set Up Your Bot: Use the following code to configure your bot with a SOCKS5 proxy.

javascript
   const TelegramBot = require('node-telegram-bot-api');
   const SocksAgent = require('socks5-https-client/lib/Agent');

   const TOKEN = 'YOUR_BOT_TOKEN'; // Replace with your bot token
   const socksAgent = new SocksAgent({
       socksHost: 'YOUR_SOCKS_PROXY_HOST', // e.g., '103.206.97.70'
       socksPort: YOUR_SOCKS_PROXY_PORT, // e.g., 4145
       // If your proxy requires authentication
       socksUsername: 'YOUR_USERNAME', // Optional
       socksPassword: 'YOUR_PASSWORD' // Optional
   });

   const bot = new TelegramBot(TOKEN, {
       polling: true,
       request: {
           agentClass: SocksAgent,
           agentOptions: {
               socksHost: 'YOUR_SOCKS_PROXY_HOST',
               socksPort: YOUR_SOCKS_PROXY_PORT,
               socksUsername: 'YOUR_USERNAME', // Optional
               socksPassword: 'YOUR_PASSWORD' // Optional
           }
       }
   });

   bot.onText(/\/start/, (msg) => {
       bot.sendMessage(msg.chat.id, 'Welcome to the bot!');
   });
   

Using HTTP Proxy

1. Install Required Packages: You need to install `node-telegram-bot-api`.

bash
   npm install node-telegram-bot-api
   

2. Set Up Your Bot: Use the following code to configure your bot with an HTTP proxy.

javascript
   const TelegramBot = require('node-telegram-bot-api');

   const TOKEN = 'YOUR_BOT_TOKEN'; // Replace with your bot token
   const bot = new TelegramBot(TOKEN, {
       polling: true,
       request: {
           proxy: 'https://YOUR_HTTP_PROXY_HOST:YOUR_HTTP_PROXY_PORT', // e.g., 'https://proxy.example.com80'
           // If your proxy requires authentication
           // You may need to set up an environment variable or a config file for username and password
       }
   });

   bot.onText(/\/start/, (msg) => {
       bot.sendMessage(msg.chat.id, 'Welcome to the bot!');
   });
   

Important Notes

- Ensure that the proxy server is running and accessible from your network.
- If you are using a SOCKS5 proxy that requires authentication, include the `socksUsername` and `socksPassword` in the options.
- For HTTP proxies, if authentication is required, you may need to handle it differently depending on the proxy server's requirements.

By following these steps, you should be able to successfully set up a proxy for your Telegram bot using Node.js.

Citations:
[1] https://stackoverflow.com/questions/54060545/telegram-bot-ends-unexpectedly
[2] https://mast.github.io/telegram-bot-api/
[3] https://github.com/yagop/node-telegram-bot-api/issues/122
[4] https://github.com/yagop/node-telegram-bot-api/issues/1072
[5] https://www.npmjs.com/package/telegram-bot-api/v/2.0.0
[6] https://community.n8n.io/t/telegram-error-bad-request-please-check-your-parameters/27484
[7] https://www.youtube.com/watch?v=C53l5CSzOGE
[8] https://forum.dfinity.org/t/show-and-tell-a-telegram-bot-on-the-internet-computer/11795