Solved: Python Telegram Bot Hangs


I’ve upgraded my Telegram game bot Word Squad to use the latest V21 Python Telegram Bot(PTB) framework, from a distant V13 which I built this bot with 2 years ago. Along the way I upgraded a few components too, such as Python 3.9 –> 3.12, redis 6 –> 7, etc.

After I made all the necessary changes, and tested locally, I checked in my code and let my automated workflows to build and deploy the latest Word Squad. And it’s deployed. However the new bot didn’t seem to be responsive, at all. I took a look at its logs:

httpx - INFO - HTTP Request: POST https://api.telegram.org/<redacted>/getMe "HTTP/1.1 200 OK"
httpx - INFO - HTTP Request: POST https://api.telegram.org/<redacted>/deleteWebhook "HTTP/1.1 200 OK"
apscheduler.scheduler - INFO - Scheduler started
telegram.ext.Application - INFO - Application started
httpx - INFO - HTTP Request: POST https://api.telegram.org/<redacted>/getUpdates "HTTP/1.1 200 OK"

It usually will keep doing the getUpdates requests but this time it stopped there, more like it got stuck for some reason. Let the troubleshooting begin then.

Between my localhost test environment and production there isn’t really any big difference. I tried a few combinations but still, localhost worked but production didn’t

Then I tried to remove components 1 by 1, guess what, after I removed the environment variable REDIS_HOST and restarted the bot suddenly it went back to live! It was spamming errors but it was no longer stuck.

Then I googled a bit so this issue is real: https://github.com/redis/redis-py/issues/722. I added 2 arguments for the Python redis client, and so far it’s working great.

redis_pool = redis.ConnectionPool(host=os.environ.get("REDIS_HOST", default='127.0.0.1'), port=6379, db=0, socket_keepalive=True, socket_timeout=3)

🙂

,