I’m working on a monitoring system for my server that tracks system temperature and sends alerts through a telegram bot. The bot checks the temperature every minute and should send a warning message when it goes above a certain limit. To avoid spam, I want it to wait 15 minutes before sending another alert. However, I keep getting timeout errors when trying to send the second message after the waiting period. Here’s my current implementation:
telegram_bot = telepot.Bot(API_KEY)
telegram_bot.message_loop(message_handler)
while True:
if ((check_system_temp() > 35.0) and should_send_alert()):
alert_text = "System temp: " + str(check_system_temp()) + " degrees"
telegram_bot.sendMessage(chat_ids[0], alert_text)
time.sleep(60)
The should_send_alert() function manages the 15-minute cooldown period. I’m getting a ReadTimeoutError from urllib3 when the bot tries to send the message after waiting. Has anyone encountered this before?
I’ve hit this same timeout issue with my telegram monitoring scripts. The HTTP connection stays open too long between requests - when your bot waits 15 minutes, the connection goes stale or the server drops it. Add a timeout parameter when you initialize your bot and catch the exception to retry. I used telegram_bot = telepot.Bot(API_KEY, timeout=30) and it worked. Also wrap your sendMessage in try-except to catch ReadTimeoutError and retry once. Those long gaps between API calls are what’s breaking it.
Telepot has connection pooling issues when sitting idle for long periods. I ran into the same thing with my server monitoring setup. Fixed it by creating a new bot instance before each send instead of reusing a global one. Just do bot = telepot.Bot(API_KEY) right before bot.sendMessage() and you’ll get a fresh connection every time. The overhead’s tiny since you’re only sending occasional alerts. Works great for my temperature monitoring that runs on similar intervals.
yea, telepot does this constantly. switch to python-telegram-bot - telepot’s basically dead and full of connection bugs. use application.bot.send_message() instead. it handles connection pooling way better for periodic messages.