python-telegram-bot (PTB) from httpx, the default, to aiohttp. Both are libraries that make HTTP requests. The default httpx backend can raise frequent NetworkError exceptions, such as ReadError, in bots that poll Telegram for long periods, and switching to aiohttp is a common way to make them stop. If your bot runs without these errors, you don’t need this change.
Install ptbcontrib
You need Python and thepython-telegram-bot library:
ptbcontrib, which offers a class to handle the requests made to Telegram. Its aiohttp_request extra also installs aiohttp:
requirements.txt, so Square Cloud installs them during the deploy:
requirements.txt
Switch from httpx to aiohttp
ImportAiohttpRequest in the file where you create your bot client:
httpx. Pass it to the PTB client like in the examples below. Both read the bot token from the TELEGRAM_BOT_TOKEN environment variable, so it never goes into your code:
Run it on Square Cloud
Create asquarecloud.app file at the root of your project. For a bot whose main file is main.py:
squarecloud.app
main.py, requirements.txt and squarecloud.app, without the token.
Then set the token as the TELEGRAM_BOT_TOKEN environment variable: in the upload form’s Advanced configuration, on the application’s Environment Variables page (then restart the bot), or from the CLI:
Next steps
Telegram bot
Create, deploy and test a Telegram bot from scratch.
Environment variables
Manage your bot’s token and other secrets.
App won't start
Fix ModuleNotFoundError and other startup failures.
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! Developers have already rated Square Cloud 4.9/5 across 402 reviews on Google and Trustpilot: leave yours too! It helps us understand what matters most to you.
Google Reviews
Leave your review in Google Reviews.
Trustpilot
Leave your review in Trustpilot.

