Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segment long messages sent to Telegram to avoid API length limitation #15

Open
dmcallejo opened this issue Dec 19, 2022 · 0 comments
Open

Comments

@dmcallejo
Copy link
Owner

dmcallejo commented Dec 19, 2022

When the bot has a long message to send to the user it may reach the API limit leaving the user without response. See stacktrace below.

To avoid this, a good solution might be process all sent messages and segment them in several chained messages (each message as a reply of the one it came before).

2022-12-19 10:25:33,001 (__init__.py:986 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message is too long"
2022-12-19 10:25:33,001 (__init__.py:988 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
  File "/bot/bot.py", line 190, in reply_to
    bot.reply_to(message, text, parse_mode="html", **kwargs)
  File "/usr/local/lib/python3.10/site-packages/telebot/__init__.py", line 4074, in reply_to
    return self.send_message(message.chat.id, text, reply_to_message_id=message.message_id, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/telebot/__init__.py", line 1451, in send_message
    apihelper.send_message(
  File "/usr/local/lib/python3.10/site-packages/telebot/apihelper.py", line 276, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/usr/local/lib/python3.10/site-packages/telebot/apihelper.py", line 161, in _make_request
    json_result = _check_result(method_name, result)
  File "/usr/local/lib/python3.10/site-packages/telebot/apihelper.py", line 188, in _check_result
    raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message is too long

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/telebot/__init__.py", line 977, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "/usr/local/lib/python3.10/site-packages/telebot/util.py", line 154, in raise_exceptions
    raise self.exception_info
  File "/usr/local/lib/python3.10/site-packages/telebot/util.py", line 98, in run
    task(*args, **kwargs)
  File "/bot/bot.py", line 169, in command_handler
    reply_to(message, response)
  File "/bot/bot.py", line 192, in reply_to
    bot.reply_to(message, "There was a Telegram error sending the message:\n" + text +
  File "/usr/local/lib/python3.10/site-packages/telebot/__init__.py", line 4074, in reply_to
    return self.send_message(message.chat.id, text, reply_to_message_id=message.message_id, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/telebot/__init__.py", line 1451, in send_message
    apihelper.send_message(
  File "/usr/local/lib/python3.10/site-packages/telebot/apihelper.py", line 276, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/usr/local/lib/python3.10/site-packages/telebot/apihelper.py", line 161, in _make_request
    json_result = _check_result(method_name, result)
  File "/usr/local/lib/python3.10/site-packages/telebot/apihelper.py", line 188, in _check_result
    raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message is too long
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant