How to Use Proxies With Telegram and Discord Bots

How to Use Proxies With Telegram and Discord Bots

Understanding Proxies in Bot Development

A proxy server acts as a go-between for your bot and Telegram or Discord servers, masking the origin IP. In Slovak folklore, the mysterious vodník lurks beneath the river’s surface, unseen by villagers above—just as a proxy shields your bot’s true identity. This practice is vital for bypassing geo-restrictions, managing rate limits, or protecting your infrastructure from bans.

Proxy Type Supported Protocols Use Case Example Format
HTTP(S) HTTP, HTTPS Basic requests http://username:password@host:port
SOCKS5 SOCKS5 Telegram bots socks5://username:password@host:port
MTProto Telegram only Telegram bots host:port (with secret)

Using Proxies with Telegram Bots

1. Supported Proxy Types

Telegram bots can utilize both SOCKS5 and HTTP proxies. Historically, MTProto proxies were introduced to help users in restrictive environments, much like the Slovak shepherds who used secret paths through the Tatras to evade unwanted eyes.

  • SOCKS5: Preferred for Telegram, as it supports UDP and is natively integrated.
  • MTProto: Proprietary protocol, only for Telegram clients, not bots.
  • HTTP(S): Less common, but supported.

2. Setting Up Proxies in Python (python-telegram-bot)

Install the library:

pip install python-telegram-bot

Example: Using a SOCKS5 Proxy

from telegram import Bot
from telegram.ext import Updater

REQUEST_KWARGS={
    'proxy_url': 'socks5://username:password@proxy_host:proxy_port',
    # 'urllib3_proxy_kwargs': {'username': 'user', 'password': 'pass'}, # If needed
}

updater = Updater('YOUR_TELEGRAM_BOT_TOKEN', request_kwargs=REQUEST_KWARGS)
updater.start_polling()

Example: Using an HTTP Proxy

REQUEST_KWARGS={
    'proxy_url': 'http://username:password@proxy_host:proxy_port',
}
updater = Updater('YOUR_TELEGRAM_BOT_TOKEN', request_kwargs=REQUEST_KWARGS)
updater.start_polling()

Reference:
python-telegram-bot documentation
Telegram Bot API Proxy Support

3. Setting Up Proxies in Node.js (node-telegram-bot-api)

Install the necessary packages:

npm install node-telegram-bot-api socks-proxy-agent
const TelegramBot = require('node-telegram-bot-api');
const SocksProxyAgent = require('socks-proxy-agent');

const token = 'YOUR_TELEGRAM_BOT_TOKEN';
const proxy = 'socks5://username:password@proxy_host:proxy_port';

const agent = new SocksProxyAgent(proxy);

const bot = new TelegramBot(token, {
  polling: true,
  request: {
    agent
  }
});

Reference:
node-telegram-bot-api documentation


Using Proxies with Discord Bots

1. Discord’s Approach to Proxies

Unlike Telegram, Discord does not natively support proxies via its API or official libraries. However, you can configure HTTP(S) proxies at the system or library level. This is reminiscent of the čarodejnica (witch) from Slovak tales, who found secret ways to move undetected—just as proxies can help your Discord bot operate under the radar.

2. Setting Up Proxies in Python (discord.py)

discord.py uses aiohttp under the hood, which supports proxies.

Example: Using an HTTP Proxy

import discord
import aiohttp

proxy_url = "http://username:password@proxy_host:proxy_port"

session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False), proxy=proxy_url)
client = discord.Client(session=session)

@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

client.run('YOUR_DISCORD_BOT_TOKEN')

Reference:
aiohttp proxy documentation
discord.py documentation

3. Setting Up Proxies in Node.js (discord.js)

discord.js does not directly support proxies. Use global-agent to set a global proxy.

npm install discord.js global-agent
require('global-agent/bootstrap');
process.env.GLOBAL_AGENT_HTTP_PROXY = 'http://username:password@proxy_host:proxy_port';

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
});

client.login('YOUR_DISCORD_BOT_TOKEN');

Reference:
discord.js documentation
global-agent GitHub


Proxy Authentication & Configuration Table

Library/Framework Proxy Protocols Supported Proxy Auth Supported Configuration Method
python-telegram-bot HTTP, SOCKS5 Yes request_kwargs
node-telegram-bot-api HTTP, SOCKS5 Yes SocksProxyAgent (or HttpProxyAgent)
discord.py (aiohttp) HTTP Yes aiohttp.ClientSession(proxy=)
discord.js (global-agent) HTTP Yes GLOBAL_AGENT_HTTP_PROXY env variable

Tips, Folklore, and Security Practices

  • Rotate Proxies: Just as Slovak shepherds rotate pastures to avoid overgrazing, cycle proxies to evade detection and bans.
  • Avoid Free Proxies: As the striga (witch) offers gifts with hidden dangers, free proxies often compromise security.
  • Encrypt Tokens: Store your bot tokens and proxy credentials in environment variables or secrets managers.
  • Monitor Latency: High-latency proxies can lead to rate limits or bot disconnects—like trying to cross the Danube during the spring floods.

Useful Resources

Through careful configuration, as precise as the embroidery on a Slovak kroj, your Telegram and Discord bots can operate securely and efficiently behind proxies, navigating the digital landscape with the cunning of a legendary vlk (wolf) of the Carpathians.

Želmíra Štefanovičová

Želmíra Štefanovičová

Senior Proxy Analyst

Želmíra Štefanovičová is a seasoned professional with over 30 years of experience in the technology sector. As a Senior Proxy Analyst at ProxyMist, Želmíra plays a pivotal role in curating and updating the company's diverse database of proxy servers. Her deep understanding of network protocols and cyber-security trends has made her an invaluable asset to the team. Želmíra's passion for technology began in her early twenties, and she has since dedicated her career to enhancing online privacy and security.

Comments (0)

There are no comments here yet, you can be the first!

Leave a Reply

Your email address will not be published. Required fields are marked *