1

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:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\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 "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 398, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1374, in getresponse
    response.begin()
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 318, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 279, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1273, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\ssl.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:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\utils\request.py", line 259, in _request_wrapper
    resp = self._con_pool.request(*args, **kwargs)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 68, in request
    return self.request_encode_body(method, url, fields=fields,
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\poolmanager.py", line 244, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 665, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\retry.py", line 347, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\packages\six.py", line 686, in reraise
    raise value
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 614, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\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 "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\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=5.0)

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

Traceback (most recent call last):
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\ext\utils\promise.py", line 96, in run
    self._result = self.pooled_function(*self.args, **self.kwargs)
  File "C:\Users\Evilreborn\Desktop\tmp\test.py", line 53, in status
    context.bot.send_chat_action(
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\bot.py", line 130, in decorator
    result = func(*args, **kwargs)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\bot.py", line 2023, in send_chat_action
    result = self._post('sendChatAction', data, timeout=timeout, api_kwargs=api_kwargs)
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\bot.py", line 295, in _post
    return self.request.post(
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\utils\request.py", line 356, in post
    result = self._request_wrapper(
  File "C:\Users\Evilreborn\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\utils\request.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

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
EvilReboot
  • 152
  • 10
  • A telegram bot gets input from users all over the internet, right? But this will only take a screenshot of the browser instance that you created in this app, which hasn't had anyt activity. So, what's the point here? – Tim Roberts Feb 07 '22 at 05:00
  • @TimRoberts I'm a bug tester, I have to check my admin portal around 10-15 times a day to check the status of my reported bug. I just want to make this repeated task automatic for this purpose I just needed a screenshot of the webpage but before reaching to end product I started getting this error. – EvilReboot Feb 07 '22 at 05:10
  • 1
    So, you're not just fetching "google.com", you have more code that actually brings up your admin portal in this Selenium instance, and you have something to refresh it? – Tim Roberts Feb 07 '22 at 05:16
  • @TimRoberts there are 3 more lines of code, in place of "google.com" there is a portal login url and 3 lines are for passing username, password, and clicking the sign-in button. I have removed those lines since it contains credentials. I filtered the code by removing the credentials and the above code recreates the same error. – EvilReboot Feb 07 '22 at 05:23
  • I haven't implemented the "Refreshing" feature yet but I was thinking to append this login script to the "status" function and after sending the screenshot I will close the browser by using `browser.quit()` – EvilReboot Feb 07 '22 at 05:24
  • `each time my bot stops working after 5min` → exception was raised inside `if user["id"] == int(USER_ID):` Is bot crashes even if you didn't send any message? – rzlvmp Feb 07 '22 at 06:32
  • @rzlvmp No, 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. – EvilReboot Feb 07 '22 at 07:10
  • @EvilReboot sorry can't help anymore, not familiar with Telegram. Maybe you reaching some Telegram limits? – rzlvmp Feb 07 '22 at 07:15
  • @rzlvmp can you please upvote this question? i'm still serching for answer – EvilReboot Feb 15 '22 at 02:29

2 Answers2

0

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

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
  • @CallMeStag The problem is not with respect to the internet connection. Since I thought it might be issue so created a Virtual Machine/RDP/VPS of 16GB RAM and tested on it but still it didn't works. Also i have tried increasing timeout of `context.bot.send_chat_action` but it didn't work. One more thing which I have observed that after getting this error if I send `/status` it works, if i send again after 5 min it don't works, so it works in alternate way one time it throws error and one time it works. – EvilReboot Feb 07 '22 at 07:25
  • @CallMeStag `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 – EvilReboot Feb 07 '22 at 08:07
0

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

mister13
  • 11
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31626162) – innicoder Apr 30 '22 at 02:16