-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Please answer these questions before submitting your issue. Thanks!
-
What version of pyTelegramBotAPI are you using?
pyTelegramBotAPI=4.15.5
fastapi=0.97.0
uvicorn=0.22.0 -
What OS are you using?
debian -
What version of python are you using?
3.11
Full stacktrace
INFO: 127.0.0.6:34285 - "GET /debug/healthz HTTP/1.1" 200 OK
2024-03-27 10:45:51,183 (async_telebot.py:576 MainThread) INFO - TeleBot: "Received 1 new updates"
2024-03-27 10:45:51,183 (request_logger.py:17 MainThread) INFO - TeleBot: "Message received: {'message_id': 10, 'from': {'id': 1087968824, 'is_bot': True, 'first_name': 'Group', 'username': 'GroupAnonymousBot'}, 'sender_chat': {'id': -1001951399479, 'title': 'чат KAP-META «KMF»', 'username': 'cap_meta_chat', 'type': 'supergroup'}, 'chat': {'id': -1001951399479, 'title': 'чат KAP-META «KMF»', 'username': 'cap_meta_chat', 'type': 'supergroup'}, 'date': 1711487943, 'left_chat_participant': {'id': MY_BOT_CHAT_ID, 'is_bot': True, 'first_name': 'MY_BOT_FIRST_NAME', 'username': 'MY_BOT_USERNAME'}, 'left_chat_member': {'id': MY_BOT_CHAT_ID, 'is_bot': True, 'first_name': 'MY_BOT_FIRST_NAME', 'username': 'MY_BOT_USERNAME'}}"
INFO: 127.0.0.6:50489 - "POST /process HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 435, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 282, in __call__
await super().__call__(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
raise exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
raise e
File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 241, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 167, in run_endpoint_function
return await dependant.call(**values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/bot/main.py", line 144, in process_webhook
await bot.process_new_updates([update])
File "/usr/local/lib/python3.11/site-packages/telebot/async_telebot.py", line 656, in process_new_updates
await self.process_new_messages(new_messages)
File "/usr/local/lib/python3.11/site-packages/telebot/async_telebot.py", line 695, in process_new_messages
await self._process_updates(self.message_handlers, new_messages, 'message')
File "/usr/local/lib/python3.11/site-packages/telebot/async_telebot.py", line 483, in _process_updates
await asyncio.gather(*tasks)
File "/usr/local/lib/python3.11/site-packages/telebot/async_telebot.py", line 509, in _run_middlewares_and_handlers
middleware_result = await middleware.pre_process(message, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/bot/middleware/user.py", line 22, in pre_process
await save_telegram_chat(self.app, chat.id)
File "/app/bot/interactors/users.py", line 59, in save_telegram_chat
chat = await app.bot.get_chat(chat_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/telebot/async_telebot.py", line 2486, in get_chat
result = await asyncio_helper.get_chat(self.token, chat_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/telebot/asyncio_helper.py", line 332, in get_chat
return await _process_request(token, method_url, params=payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/telebot/asyncio_helper.py", line 99, in _process_request
raise e
File "/usr/local/lib/python3.11/site-packages/telebot/asyncio_helper.py", line 95, in _process_request
json_result = await _check_result(url, resp)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/telebot/asyncio_helper.py", line 274, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.asyncio_helper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 403. Description: Forbidden: bot was kicked from the supergroup chat
INFO: 127.0.0.6:44853 - "GET /debug/healthz HTTP/1.1" 200 OKReceived pprinted message:
{
'message_id': 10,
'from': {
'id': 1087968824,
'is_bot': True,
'first_name': 'Group',
'username': 'GroupAnonymousBot'
},
'sender_chat': {
'id': -1001951399479,
'title': 'чат KAP-META «KMF»',
'username': 'cap_meta_chat',
'type': 'supergroup'
},
'chat': {
'id': -1001951399479,
'title': 'чат KAP-META «KMF»',
'username': 'cap_meta_chat',
'type': 'supergroup'
},
'date': 1711487943,
'left_chat_participant': {
'id': MY_BOT_CHAT_ID,
'is_bot': True,
'first_name': 'MY_BOT_NAME',
'username': 'MY_BOT_USERNAME'
},
'left_chat_member': {
'id': MY_BOT_CHAT_ID,
'is_bot': True,
'first_name': 'MY_BOT_NAME,
'username': 'MY_BOT_USERNAME'
}
}
Context
Bot uses webhooks, see above the method POST /process.
On every received message bot trying to get information about sender chat with method chat = await app.bot.get_chat(chat_id) and save it in save_telegram_chat function. Somebody deleted my bot from supergroup and after that calling method bot.get_chat(chat_id) raises ApiTelegramException. The strange thing is: ApiTelegramException produces 500 Internal Server Error and then telegram didn't receive 200 status and repeat sending the same message (with 'message_id': 10) again and again to bot. Also bot has ExceptionHandler and in this case exception handler doesn't work:
class BotExceptionHandler(ExceptionHandler):
def __init__(self, app: Application):
self.app = app
async def handle(self, exception: Exception):
error_str = repr(exception)
logger.error(error_str)
logger.error("Exception traceback:\n%s", traceback.format_exc())
message = f'Bot exception occurred:\n{error_str}'
await self.app.notifier.notify(message)
return True
bot = AsyncTeleBot(
token=settings.telegram.bot_token.get_secret_value(),
exception_handler=BotExceptionHandler(app),
)Error was fixed with adding try-except in save_telegram_chat function:
from telebot.asyncio_helper import ApiTelegramException
try:
chat = await app.bot.get_chat(chat_id)
except ApiTelegramException as err:
logger.warning(
'Unable to get chat info for chat_id=%s when saving telegram chat',
chat_id,
)
logger.error('Exception: %s', err)
return NoneBut this fix seems as not a proper way, because the error looks like a bug somewhere in pyTelegramBotAPI.
Logs after fixing with try-except:
2024-03-27 11:10:34,697 (async_telebot.py:576 MainThread) INFO - TeleBot: "Received 1 new updates"
2024-03-27 11:10:34,697 (request_logger.py:17 MainThread) INFO - TeleBot: "Message received: {'message_id': 10, 'from': {'id': 1087968824, 'is_bot': True, 'first_name': 'Group', 'username': 'GroupAnonymousBot'}, 'sender_chat': {'id': -1001951399479, 'title': 'чат KAP-META «KMF»', 'username': 'cap_meta_chat', 'type': 'supergroup'}, 'chat': {'id': -1001951399479, 'title': 'чат KAP-META «KMF»', 'username': 'cap_meta_chat', 'type': 'supergroup'}, 'date': 1711487943, 'left_chat_participant': {'id': MY_BOT_CHAT_ID, 'is_bot': True, 'first_name': 'MY_BOT_NAME', 'username': 'MY_BOT_USERNAME'}, 'left_chat_member': {'id': MY_BOT_CHAT_ID, 'is_bot': True, 'first_name': 'MY_BOT_NAME', 'username': 'MY_BOT_USERNAME'}}"
2024-03-27 11:10:34,856 (users.py:62 MainThread) WARNING - TeleBot: "Unable to get chat info for chat_id=-1001951399479 when saving telegram chat"
2024-03-27 11:10:34,856 (users.py:66 MainThread) ERROR - TeleBot: "Exception: A request to the Telegram API was unsuccessful. Error code: 403. Description: Forbidden: bot was kicked from the supergroup chat"
2024-03-27 11:10:34,856 (request_logger.py:25 MainThread) INFO - TeleBot: "Message processing finished: 10"
INFO: 127.0.0.6:48801 - "POST /process HTTP/1.1" 200 OK