- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Working Behind a Proxy
PTBs default networking backend HTTPXRequest comes with built-in support for proxies.
Note that the details below only apply to HTTPXRequest.
If you use a different implementation of BaseRequest, you'll have to configure proxies yourself.
PTB will obtain its proxy configuration in the following order (the first to be found will be used):
- Programmatic.
- Using HTTP_PROXYenvironment variable.
- Using HTTPS_PROXYenvironment variable.
- Using ALL_PROXYenvironment variable.
Proxies can be setup like this:
from telegram.ext import ApplicationBuilder
# "USERNAME:PASSWORD@" is optional, if you need authentication:
proxy_url = 'http://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT'  # can also be a https proxy
app = ApplicationBuilder().token("TOKEN").proxy_url(proxy_url).get_updates_proxy_url(proxy_url).build()In the last line, we setup the proxy such that it'll be used both for making requests to the Bot API like  Bot.send_message (proxy_url()) and for fetching updates from Telegram (get_updates_proxy_url). It is not necessary to setup a proxy for both, you can do it for either of them.
This configuration is supported, but requires an optional/extra python package. To install:
pip install python-telegram-bot[socks]from telegram.ext import ApplicationBuilder
proxy_url = "socks5://user:pass@host:port"
app = ApplicationBuilder().token("TOKEN").proxy_url(proxy_url).build()If you're more of an advanced user and would like to customize your proxy setup even further, check out the docs of httpx for more info.
- Wiki of python-telegram-bot© Copyright 2015-2025 – Licensed by Creative Commons
- Architecture Overview
- Builder Pattern for Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests