@@ -1012,11 +1012,17 @@ def sanitize_html(content):
10121012 message = bot_reply ,
10131013 )
10141014
1015- await context .bot .send_message (
1016- chat_id = chat_id ,
1017- text = escaped_reply ,
1018- parse_mode = ParseMode .HTML
1019- )
1015+ # # send the response
1016+ # await context.bot.send_message(
1017+ # chat_id=chat_id,
1018+ # text=escaped_reply,
1019+ # parse_mode=ParseMode.HTML
1020+ # )
1021+
1022+ message_parts = split_message (escaped_reply )
1023+
1024+ for part in message_parts :
1025+ await context .bot .send_message (chat_id = chat_id , text = part , parse_mode = ParseMode .HTML )
10201026
10211027 stop_typing_event .set ()
10221028 context .user_data .pop ('active_translation' , None )
@@ -1242,6 +1248,38 @@ async def make_api_request(bot, chat_history, timeout=30):
12421248 bot .logger .error (f"An error occurred while making the API request: { str (e )} " )
12431249 raise e
12441250
1251+ # split long messages
1252+ def split_message (message , max_length = 4000 ):
1253+ """
1254+ Split a long message into multiple smaller messages.
1255+ Args:
1256+ message (str): The message to split.
1257+ max_length (int): Maximum length of each part.
1258+ Returns:
1259+ list: A list containing message parts.
1260+ """
1261+ # List to store split messages
1262+ message_parts = []
1263+
1264+ # While there is still text to split
1265+ while len (message ) > max_length :
1266+ # Split at the nearest period or line break before the max_length
1267+ split_index = message .rfind ('\n ' , 0 , max_length )
1268+ if split_index == - 1 :
1269+ split_index = message .rfind ('. ' , 0 , max_length )
1270+ if split_index == - 1 :
1271+ split_index = max_length # Split at max length if no better point is found
1272+ # Add the message part
1273+ message_parts .append (message [:split_index ].strip ())
1274+ # Update the remaining message
1275+ message = message [split_index :].strip ()
1276+
1277+ # Add the last part
1278+ if message :
1279+ message_parts .append (message )
1280+
1281+ return message_parts
1282+
12451283# # // (old request type)
12461284# async def make_api_request(bot, chat_history, timeout=30):
12471285# # Prepare the payload for the API request with updated chat history
0 commit comments