Forums

Telegram Bot Timeout Issue: Messages Reach Telegram but Code Throws Timeout Error

Hello,

I’ve been using the Telegram API with my bot for the last 6 months without any issues, but recently, I’ve encountered a problem where the code fails with a timeout error, although the message successfully reaches Telegram. This has been happening for the past few days.

To troubleshoot, I increased the timeout to 60 seconds, but the issue persists. Below is the full traceback:

Traceback (most recent call last):  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 402, in _make_request  six.raise_from(e, None)  File "<string>", line 2, in raise_from  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 398, in _make_request  httplib_response = conn.getresponse()  File "/usr/local/lib/python3.9/http/client.py", line 1345, in getresponse  response.begin()  File "/usr/local/lib/python3.9/http/client.py", line 307, in begin  version, status, reason = self._read_status()  File "/usr/local/lib/python3.9/http/client.py", line 268, in _read_status  line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")  File "/usr/local/lib/python3.9/socket.py", line 704, in readinto  return self._sock.recv_into(b)  File "/usr/local/lib/python3.9/ssl.py", line 1241, in recv_into  return self.read(nbytes, buffer)  File "/usr/local/lib/python3.9/ssl.py", line 1099, in read  return self._sslobj.read(len, buffer) socket.timeout: The read operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last):  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/utils/request.py", line 253, in _request_wrapper  resp = self._con_pool.request(*args, **kwargs)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/request.py", line 68, in request  return self.request_encode_body(method, url, fields=fields,  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/request.py", line 148, in request_encode_body  return self.urlopen(method, url, **extra_kw)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/poolmanager.py", line 359, in urlopen  return super(ProxyManager, self).urlopen(method, url, redirect=redirect, **kw)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/poolmanager.py", line 244, in urlopen  response = conn.urlopen(method, u.request_uri, **kw)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 665, in urlopen  retries = retries.increment(method, url, error=e, _pool=self,  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/util/retry.py", line 347, in increment  raise six.reraise(type(error), error, _stacktrace)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/packages/six.py", line 686, in reraise  raise value  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 614, in urlopen  httplib_response = self._make_request(conn, method, url,  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 404, in _make_request  self._raise_timeout(err=e, url=url, timeout_value=read_timeout,  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 321, in _raise_timeout  raise exc_cls(*args) telegram.vendor.ptb_urllib3.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=60) The above exception was the direct cause of the following exception: Traceback (most recent call last):  File "/home/GoldyBot/GBot/GBot.py", line 760, in <module>  makeSignal(nowDateTimeUTC)  File "/home/GoldyBot/GBot/GBot.py", line 505, in makeSignal  messageInfo = bot.send_photo(chat_id = channelID, photo = open(signalImage, "rb"), caption = signalText, parse_mode = "html", timeout = 60)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/bot.py", line 127, in decorator  result = func(*args, **kwargs)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/bot.py", line 661, in send_photo  return self._message( # type: ignore[return-value]  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/bot.py", line 296, in _message  result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/bot.py", line 259, in _post  return self.request.post(  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/utils/request.py", line 348, in post  result = self._request_wrapper('POST', url, fields=data, **urlopen_kwargs)  File "/home/GoldyBot/.local/lib/python3.9/site-packages/telegram/utils/request.py", line 255, in _request_wrapper  raise TimedOut() from error telegram.error.TimedOut: Timed out 

It seems that while the message is successfully sent to Telegram, the code still throws a timeout error. Could anyone provide insights on what could be causing this issue? I haven't made any changes to the code, and the bot was working fine before.

Any help would be greatly appreciated!

Hmm, I can't think of any changes on our side that might cause issues like that. Perhaps there have been some changes on Telegram's side? Are you using a recent version of the Telegram library? When did the problem start happening?

Actually I'm using a fairly old version: python-telegram-bot==13.5

If there have been changes on the Telegram side, then this is the problem... I'll try to run the script on my laptop and if the problem persists, I'll update Telegram.

It's just that this is fairly old code, it worked stably and if I update any of the libraries, I'll have to rewrite a lot. But if the problem is really in the Telegram version, then I'll fix it. I'll let you know when I check this option.

Thank you!

I will join the discussion, gentlemen, I have a similar situation, only my bot is implemented in such a way that it visits the site-downloads the file-uploads the file to the bot-the bot sends it in a chat and my logs show the following: when trying to send for the 1st time, I get an error called Timed Out, and the subsequent sending goes into a sending error. But when I restart the script, everything goes as if there were no problems. My version is as follows: python-telegram-bot 21.11.1

And so, the problem was also on my laptop.

I had to give up the Telegram Bot API and switch to aiogram, unfortunately, I had to rewrite a lot of code to work asynchronously. As a result, I had a lot of problems and errors. I spent a lot of time on the solution and I succeeded. Finally, if it helps anyone, I provide the code on how to configure the proxy and aiogram to work successfully on PythonAnyWhere:

from aiogram import Bot from aiogram.client.session.aiohttp import AiohttpSession proxyURL = "http://proxy.server:3128" timeout = 180 session = AiohttpSession(proxy = proxyURL, timeout = timeout) bot = Bot(token = tokenBot, session = session) 

Keep in mind that the timeout can be from 5 seconds to 1 minute, I don't know why, so I set it to 3 minutes with a reserve

@GoldyBot thank you so much for sharing that! Because it was happening on your laptop as well, it must be the result of some kind of change with Telegram's servers. I'm very glad that you were able to work out a solution, even if it took a while, and hopefully because you've shared your code, @oldgreml1n will be able to get theirs working that way too.

thank you for your reply @giles, and I apologize for my late reply. I don't really want to switch to it due to the fact that it was developed by Ukraine, and in case of something they can change in updates, in order to avoid data leakage, I don't think it would be a good idea to switch to it.

@GoldyBot Is there any way I can contact you outside the forum? I would like to discuss with you a couple of points regarding the codes that we both wrote, and the errors that we encountered, if possible.

@oldgreml1n I don't know, I don't really do programming, I just make scripts to automate some tasks - nothing more. I'm not a programmer.

@GoldyBot, I had a similar situation. I wrote a small script for my work to automate some tasks related to my job. Honestly, I wasn’t too keen on switching to aiogram because I was concerned about its origin (the country of origin) and my inexperience with unfamiliar libraries. Nevertheless, I pulled myself together and rewrote the script using aiogram. I had two issues with the old script: 1) there were network errors (even though everything was fine with the network in my organization), and 2) the file that the script downloaded from the browser wouldn’t upload to the bot (it kept throwing a timeout error). The only fix was restarting the script, which wasn’t very convenient for me. Now, it seems like the script has stopped behaving that way, and it looks like the issue really was with the Telegram library.

Thanks for the update

maybe it will help someone, I don't know, I'll leave it just in case. in general, I had a problem with the fact that the standard settings of the aiogram library were with the following values: total=120, connect=60, sock_connect=60, sock_read=120, and that's why I had an error when sending the file, I changed the values to 300/120/120/300, and the problem disappeared.

That's useful to know -- thanks for posting it!