During the development of a Telegram bot for listening to messages from a specific channel, I encountered a strange issue:

After the program ran for a while, it suddenly stopped receiving new messages from the channel, without any obvious error messages.
This left me quite puzzled and delayed the real-time monitoring tasks. In this article, I will go over the troubleshooting process and how I solved it by implementing a "keep-alive" mechanism and an "auto-reconnect" mechanism. I hope this can help those of you who are also developing TG bots.

🚩 Issue Reproduction: Listening Stops Without Errors

At first, I was using a very simple Telethon script to listen to the channel:

from telethon import TelegramClient, events

client = TelegramClient("session_name", TG_APPID, TG_APPHASH)

@client.on(events.NewMessage(chats=CHANNEL_ID))
async def message_handler(event):
    print("New message received:", event.raw_text)

client.start()
client.run_until_disconnected()

Everything worked fine, but after a few hours, the messages suddenly stopped printing. The key issue—no errors! The program was still running but seemed to have "lost connection."

🔍 Initial Troubleshooting: Blocking? Deadlock? Exception?

I suspected that the issue might have been caused by an exception in the message_handler function, so I added a try-except block:

@client.on(events.NewMessage(chats=CHANNEL_ID))
async def message_handler(event):
    try:
        ...
    except Exception as e:
        print("Error:", e)

After a few hours, I finally captured the following error message:

ConnectionError: Timeout

This indicated that the problem lay in the connection between the client and Telegram's servers.

📡 In-depth Analysis: Why the Timeout?

By consulting the documentation, I discovered that Telegram uses its proprietary MTProto protocol for communication, supporting TCP, HTTPS, and other transmission methods. The client and server typically establish a long-term connection via TCP and rely on periodic heartbeat packets to maintain the connection.

However, if:

  • The client doesn't send requests for a long time
  • There are no active tasks (such as message uploads or downloads)
  • Network fluctuations or device sleep

The server might classify the connection as "inactive" and close it.
Reference: Telegram Protocol Documentation, Shunix's Weblog

Solution: Keep-Alive + Auto-Reconnect

To maintain the connection with Telegram, we can employ two strategies:

  1. Send Heartbeats Periodically (Keep-Alive)
    Call a method like client.get_me() at regular intervals to show the client is still active.

  2. Auto-Reconnect Mechanism
    If the connection is lost, restart the connection using a while True loop and exception handling.

💻 Final Code Implementation (Simplified)

async def keep_alive():
    while True:
        try:
            await client.get_me()  # Heartbeat
        except Exception as e:
            print(f"[Keep-Alive Failed]: {e}")
        await asyncio.sleep(300)  # Execute every 5 minutes

async def main():
    while True:
        try:
            await client.start()
            asyncio.create_task(keep_alive())  # Start keep-alive
            await client.run_until_disconnected()
        except Exception as e:
            print(f"[Reconnection Failed]: {e}")
        await asyncio.sleep(5)  # Wait 5 seconds before reconnecting

if __name__ == "__main__":
    asyncio.run(main())

🔚 Summary

Although the process of solving this issue was somewhat "tedious," it taught me several valuable lessons:

  • Communication with Telegram requires connection maintenance.
  • Exception handling is important and can help identify hidden issues.
  • Long-running bots need self-recovery capabilities (like keep-alive and auto-reconnect).