skip to Main Content

I have created a telegram bot that sends screenshots of the browser webpage. I have used python-selenium package in order to connect browser with telegram-bot but each time my bot stops working after 5min, here is the full log

ERROR:telegram.ext.dispatcher:No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3connectionpool.py", line 402, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3connectionpool.py", line 398, in _make_request
    httplib_response = conn.getresponse()
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libhttpclient.py", line 1374, in getresponse
    response.begin()
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libhttpclient.py", line 318, in begin
    version, status, reason = self._read_status()
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libhttpclient.py", line 279, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsocket.py", line 705, in readinto
    return self._sock.recv_into(b)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libssl.py", line 1273, in recv_into
    return self.read(nbytes, buffer)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libssl.py", line 1129, in read
    return self._sslobj.read(len, buffer)
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramutilsrequest.py", line 259, in _request_wrapper
    resp = self._con_pool.request(*args, **kwargs)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3request.py", line 68, in request
    return self.request_encode_body(method, url, fields=fields,
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3poolmanager.py", line 244, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3connectionpool.py", line 665, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3utilretry.py", line 347, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3packagessix.py", line 686, in reraise
    raise value
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3connectionpool.py", line 614, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3connectionpool.py", line 404, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout,
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramvendorptb_urllib3urllib3connectionpool.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=5.0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramextutilspromise.py", line 96, in run
    self._result = self.pooled_function(*self.args, **self.kwargs)
  File "C:UsersEvilrebornDesktoptmptest.py", line 53, in status
    context.bot.send_chat_action(
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegrambot.py", line 130, in decorator
    result = func(*args, **kwargs)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegrambot.py", line 2023, in send_chat_action
    result = self._post('sendChatAction', data, timeout=timeout, api_kwargs=api_kwargs)
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegrambot.py", line 295, in _post
    return self.request.post(
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramutilsrequest.py", line 356, in post
    result = self._request_wrapper(
  File "C:UsersEvilrebornAppDataLocalProgramsPythonPython310libsite-packagestelegramutilsrequest.py", line 261, in _request_wrapper
    raise TimedOut() from error
telegram.error.TimedOut: Timed out

variable USER_ID contains my userid so that no one else can use the bot.

My program:

from selenium import webdriver
import os
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram import ChatAction

BOT_TOKEN = 504XXXXXX:AAGXXXXXXXXXXXXXXXXXXXXXXX
USER_ID = 123456

options = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=options)

updater = Updater(token=BOT_TOKEN, use_context=True)
dp = updater.dispatcher


def status(update, context):
    user = update.message.from_user
    if user["id"] == int(USER_ID):
        browser.save_screenshot("snapshot.png")
        context.bot.send_chat_action(
            chat_id=USER_ID, action=ChatAction.UPLOAD_PHOTO, timeout=100)
        context.bot.send_photo(
            chat_id=USER_ID, photo=open("snapshot.png", "rb"), timeout=100
        )
        os.remove("snapshot.png")
    else:
        update.message.reply_text(
            "You are not authorized to use this bot"
        )


def main():
    browser.get("https://google.com")
    dp.add_handler(CommandHandler("status", status, run_async=True))

    updater.start_polling(timeout=100)
    #updater.idle()


if __name__ == "__main__":
    main()

Packages used:

python_telegram_bot==13.7
selenium==3.141.0

What I have tried till now:

  • I have tried to increase the timeout but still, the problem persists.
  • I have gone through all preexisting questions on StackOverflow but none of them works for me.
  • Removed if user["id"] == int(USER_ID): this if-else block
  • context.bot.send_chat_action(chat_id=USER_ID, action=ChatAction.UPLOAD_PHOTO, timeout=100) removed the line where the error occured but still facing the same issue
  • There is no problem with respect to internet connection

Bot crashes when I send "/status" (after 5 min from startup). if I keep on sending "/status" in less than 5min intervals it runs smoothly but if there is no activity for 5min it starts sending this error.

The problem is not with respect to the internet connection. Since I thought it might be an issue so created a Virtual Machine/RDP/VPS on Azure platform of 16GB RAM and tested it on but still, it didn’t work. Also, I have tried increasing the timeout of context.bot.send_chat_action but it didn’t work.

One more thing which I have observed is that after getting this error if I send /status it works (even after 5 min) if I send again after 5 min it doesn’t work, so it works in an alternative manner one time it throws the error and the next time it works.

enter image description here

I’m having this issue for the past 8 days, if anyone can help me, please help.

thankyou for giving your valuable time to my issue,
thanking you


Note: This question was forwarded to both the python-telegram-bot user group and the issue tracker

2

Answers


  1. You increased the timeout for the call to context.bot.send_photo, but the traceback indicates that the timeout happens for the call to context.bot.send_chat_action.
    If you have frequent timeout problems, I suggest to try & stabilize your internet connection. Please also have a look at this wiki page on handling network exception.


    Disclaimer: I’m currently the maintainer of python-telegram-bot

    Login or Signup to reply.
  2. Try debugging instead of run project. I don’t know why but when i get that error i try debugging and it works finely. I hope it works for you too

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search