dmcallejo / ASFBot

Control your ArchiSteamFarm with Telegram
GNU General Public License v3.0
43 stars 10 forks source link

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

Open dmcallejo opened 1 year ago

dmcallejo commented 1 year ago

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