Introduction
- This article guides you through changing
python-telegram-bothttpxtoaiohttp.httpxandaiohttpare libraries used to make http requests. - Before we get start, make sure you have Python and python-telegram-bot library installed on your environment. Check the python-telegram-bot instalation command below.
Changing libraries
Installing
- First, you will need to install a library that offers an class to handle the requests maded to Telegram. Let’s install
ptbcontrib, use the command below:
Changing httpx to aiohttp
- Next, we need to import
AiohttpRequestin the file where we will instantiate our bot client.
- This class will handle all requests to telegram instead of default
httpx. AiohttpRequestwill be used in the PTB client instance like in the example below:
Why do this change?
- Changing the library httpx to aiohttp brings some benefits with it.
- Performance: aiohttp is faster than httpx.
- Mitigating Errors: The httpx will raise too much network errors like ReadError and other NetworkErrors due to it’s configurations and performance.
Did you like this article?
- We created this content with great care to offer the best help possible. If this article helped you in any way, support our work! Leave your review! it helps us understand what matters most to you.

