Creating a Telegram bot using Telethon and facing TimeoutError

I developed a basic Telegram bot utilizing Telethon and encountered an issue when trying to run it. Here’s my code:

from telethon import TelegramClient, events
from settings import *

bot_client = TelegramClient('bot_session', api_id=api_id, api_hash=api_hash)

@bot_client.on(events.NewMessage(pattern=r'/start'))
async def on_start(event):
    await bot_client.send_message(entity=event.chat_id, message='Hello')

bot_client.start()
bot_client.run_until_disconnected()

However, I keep receiving a TimeoutError after executing the bot. I’ve tried using both VPS and VPN as well as a proxy, but the problem persists. Can someone provide assistance who has experience with this issue?

Error messages include:

Connection attempt 1 failed: TimeoutError:
Connection attempt 2 failed: TimeoutError:
... (up to attempt 6)
ConnectionError: Unable to connect to Telegram after multiple attempts.

check ur api_id and api_hash. Make sure they’re correct and authorized for the bot. Also, when using Telethon, ensure ur internet connection is stable and not too slow, as even slight packet loss could cause timeouts. Last resort, try updating pip and reinstalling Telethon to see if it helps!

From my experience, a TimeoutError in Telethon could sometimes be linked to network DNS issues. Verify that your DNS settings on your server are correctly configured, opting for a reliable DNS like Google’s (8.8.8.8 and 8.8.4.4). Also, check network stability and ensure ports required for Telegram to operate (such as 443 for HTTPS) are open and not blocked. Another option is to try connecting with a different location or network and see if the issue still persists. Switching API development keys, as sometimes these keys might be having issues, may also help resolve this problem.