From 319d8fc8f42bd7c4f4eea8574079840f604cb538 Mon Sep 17 00:00:00 2001 From: Karim Iskakov Date: Wed, 8 Mar 2023 15:50:07 -0600 Subject: [PATCH] Handle long messages --- bot/bot.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 6c220d1..709367b 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -37,6 +37,12 @@ HELP_MESSAGE = """Commands: ⚪ /help – Show help """ + +def split_text_into_chunks(text, chunk_size): + for i in range(0, len(text), chunk_size): + yield text[i:i + chunk_size] + + async def register_user_if_not_exists(update: Update, context: CallbackContext, user: User): if not db.check_if_user_exists(user.id): db.add_new_user( @@ -143,11 +149,13 @@ async def message_handle(update: Update, context: CallbackContext, message=None, text = f"✍️ Note: Your current dialog is too long, so {n_first_dialog_messages_removed} first messages were removed from the context.\n Send /new command to start new dialog" await update.message.reply_text(text, parse_mode=ParseMode.HTML) - try: - await update.message.reply_text(answer, parse_mode=ParseMode.HTML) - except telegram.error.BadRequest: - # answer has invalid characters, so we send it without parse_mode - await update.message.reply_text(answer) + # split answer into multiple messages due to 4096 character limit + for answer_chunk in split_text_into_chunks(answer, 4000): + try: + await update.message.reply_text(answer_chunk, parse_mode=ParseMode.HTML) + except telegram.error.BadRequest: + # answer has invalid characters, so we send it without parse_mode + await update.message.reply_text(answer_chunk) async def voice_message_handle(update: Update, context: CallbackContext): @@ -273,10 +281,12 @@ async def error_handle(update: Update, context: CallbackContext) -> None: ) # split text into multiple messages due to 4096 character limit - message_chunk_size = 4000 - message_chunks = [message[i:i + message_chunk_size] for i in range(0, len(message), message_chunk_size)] - for message_chunk in message_chunks: - await context.bot.send_message(update.effective_chat.id, message_chunk, parse_mode=ParseMode.HTML) + for message_chunk in split_text_into_chunks(message, 4000): + try: + await context.bot.send_message(update.effective_chat.id, message_chunk, parse_mode=ParseMode.HTML) + except telegram.error.BadRequest: + # answer has invalid characters, so we send it without parse_mode + await context.bot.send_message(update.effective_chat.id, message_chunk) except: await context.bot.send_message(update.effective_chat.id, "Some error in error handler") @@ -315,4 +325,4 @@ def run_bot() -> None: if __name__ == "__main__": - run_bot() + run_bot() \ No newline at end of file