PiratProxies

Automation and testing

Sending python-telegram-bot traffic through a proxy

python-telegram-bot is a framework for writing Telegram bots in Python. The proxy is set on the application builder.

The right proxy type for python-telegram-bot

Any plan in a country where Telegram is reachable. A bot sends little traffic, so the smallest plan lasts a long time.

The values to enter in python-telegram-bot

Every PiratProxies plan comes down to four values: host, port, username and password. Here they are for Residential; your own username and password are on your plan page.

Hostresi.piratproxies.com
HTTP port8080
SOCKS5 port1080
UsernameUSERNAME
Passwordshown on your plan page

Written as a single address:

http://USERNAME:PASSWORD@resi.piratproxies.com:8080

There is no separate country or session field: both are part of the username. Your plan page has a generator that writes it, and Targeting and sessions describes each piece.

Setting it up in python-telegram-bot

  1. Call .proxy(...) on the builder with the proxy address.
  2. Set .get_updates_proxy(...) to the same address if the bot uses polling.

Code: python-telegram-bot through a proxy

Python

from telegram.ext import ApplicationBuilder

app = (
    ApplicationBuilder()
    .token("BOT_TOKEN")
    .proxy("http://USERNAME:PASSWORD@resi.piratproxies.com:8080")
    .get_updates_proxy("http://USERNAME:PASSWORD@resi.piratproxies.com:8080")
    .build()
)

Put your own login from the plan page in place of USERNAME and PASSWORD. The example calls an IP-check service, so what it prints is the address your request came from.

Should python-telegram-bot rotate IPs or keep one?

When python-telegram-bot plays one visitor across several steps, a sticky session keeps the IP address unchanged from start to finish. When every run stands alone, drop the session and each run begins from a new address.

Here is what a sticky username looks like:

USERNAME-country-us-time-600-session-a1b2c3d4

When python-telegram-bot will not connect

  • Versions before 20.7 call these proxy_url and get_updates_proxy_url. If the builder has no proxy method, your version uses the older names.
  • Error 407 means the login was refused. Copy the username and password from your plan page once more, and make sure no space crept in and the username was not cut short.
  • An IP in the wrong country nearly always comes from the username, since the country code lives there. Other fixes.

python-telegram-bot and proxies: questions

Which PiratProxies plan suits python-telegram-bot best?

Any plan in a country where Telegram is reachable. A bot sends little traffic, so the smallest plan lasts a long time.

Words used in this guide: headless browser, rotating proxy, sticky session, proxy authentication.

python-telegram-bot belongs to its own developers, and PiratProxies has no connection with them; we name it only to say what this guide covers. Follow the rules of the sites you visit and our acceptable use policy.