Forums

Exception on / [POST] telegram bot

good afternoon, I ran into a problem: I launch the bot through a webhook on a flask, it works fine for 20 minutes, after which it stops responding to updates. In the logs I find the following error:

 2022-10-20 07:41:29,076: Exception on / [POST]
Traceback (most recent call last):
  File "/home/ikadetov/venv/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ikadetov/venv/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ikadetov/venv/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ikadetov/venv/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/ikadetov/trigash2.0/main.py", line 19, in webhook
    Bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
  File "/home/ikadetov/venv/lib/python3.10/site-packages/telebot/__init__.py", line 692, in process_new_updates
    self.process_new_messages(new_messages)
  File "/home/ikadetov/venv/lib/python3.10/site-packages/telebot/__init__.py", line 727, in process_new_messages
    self._notify_command_handlers(self.message_handlers, new_messages, 'message')
  File "/home/ikadetov/venv/lib/python3.10/site-packages/telebot/__init__.py", line 5991, in _notify_command_handlers
    self._exec_task(message_handler['function'], message, pass_bot=message_handler['pass_bot'], task_type='handler')
  File "/home/ikadetov/venv/lib/python3.10/site-packages/telebot/__init__.py", line 1108, in _exec_task
    raise e
  File "/home/ikadetov/venv/lib/python3.10/site-packages/telebot/__init__.py", line 1101, in _exec_task
    task(*args, **kwargs)
  File "/home/ikadetov/trigash2.0/handlers/general.py", line 61, in help
    if BotDB.chat_exist(message.chat.id):
  File "/home/ikadetov/trigash2.0/DB.py", line 23, in chat_exist
    self.cursor.execute('SELECT chat_id FROM chats WHERE chat_id = %s', (chat_id,))
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/cursors.py", line 148, in execute
    result = self._query(query)
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/cursors.py", line 310, in _query
    conn.query(q)
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/connections.py", line 775, in _read_query_result
    result.read()
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/connections.py", line 692, in _read_packet
    packet_header = self._read_bytes(4)
  File "/home/ikadetov/venv/lib/python3.10/site-packages/pymysql/connections.py", line 748, in _read_bytes
    raise err.OperationalError(
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

what could be the reason for this and how to make the bot work on a permanent basis?

See https://help.pythonanywhere.com/pages/ManagingDatabaseConnections/

thank you very much!