DAGDA SEARCH | Back to results | Support

Working Behind a Proxy · python-telegram-bot/python-telegram-bot Wiki · GitHub

Source: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Working-Behind-a-Proxy

Repository navigation

Working Behind a Proxy

Jump to bottom Edit New page Harshil edited this page May 21, 2024 · 20 revisions

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.

How is a Proxy Server Chosen?

PTB will obtain its proxy configuration in the following order (the first to be found will be used):

  1. Programmatic.
  2. Using HTTP_PROXY environment variable.
  3. Using HTTPS_PROXY environment variable.
  4. Using ALL_PROXY environment variable.

Setting a HTTP(S) Proxy Server Programmatically

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(proxy_url).get_updates_proxy(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()) and for fetching updates from Telegram (get_updates_proxy). It is not necessary to setup a proxy for both, you can do it for either of them.

Working Behind a Socks5 Server

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(proxy_url).get_updates_proxy(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-2026 – Licensed by Creative Commons

Must read

  1. Introduction to the API
  2. Tutorial: Your first bot
  3. FAQ
  4. How to ask good questions
  5. How to write an MWE

Concepts & Important Elements

  1. Architecture Overview
  2. Builder Pattern for Application
  3. Types of Handlers
  4. Working with Files and Media
  5. Exceptions, Warnings and Logging
  6. Concurrency in PTB

Notable Features

  1. Advanced Filters
  2. Storing data
  3. Making your bot persistent
  4. Adding Defaults
  5. Job Queue
  6. Arbitrary callback_data
  7. Avoiding flood limits
  8. Webhooks
  9. Bot API Forward Compatiblity

Code Resources

  1. Frequently requested design patterns
  2. Code snippets
  3. Performance Optimizations
  4. Telegram Passport
  5. Bots built with PTB
  6. Automated Bot Tests

Examples explained

  1. InlineKeyboard Example

Networking

  1. Working Behind a Proxy
  2. Handling network errors

Other resources

  1. Where to host Telegram Bots
  2. How to host your bot
  3. Local API Server
  4. Type Checking with PTB
  5. Press
  6. Notes on GAE
  7. Related Projects
  8. Emoji

Transition Guides

Administration

Clone this wiki locally