From 8ef0fa2ac0533bcad1eb553ccaa9ccebbd344daf Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 8 Oct 2025 15:18:30 +0000 Subject: [PATCH] Fix messages command to handle Unicode characters safely Co-authored-by: SpudGunMan <12676665+SpudGunMan@users.noreply.github.com> --- mesh_bot.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/mesh_bot.py b/mesh_bot.py index db5ce0c..c25ca8e 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -1087,6 +1087,13 @@ def handle_messages(message, deviceID, channel_number, msg_history, publicChanne return message.split("?")[0].title() + " command returns the last " + str(storeFlimit) + " messages sent on a channel." else: response = "" + header = "📨Messages:" + # Calculate safe byte limit (account for header and some overhead) + # Meshtastic has ~237 byte limit, use conservative 200 bytes for message content + max_bytes = 200 + header_bytes = len(header.encode('utf-8')) + available_bytes = max_bytes - header_bytes + # Reverse the message history to show most recent first for msgH in reversed(msg_history): # number of messages to return +1 for the header line @@ -1095,9 +1102,25 @@ def handle_messages(message, deviceID, channel_number, msg_history, publicChanne # if the message is for this deviceID and channel or publicChannel if msgH[4] == deviceID: if msgH[2] == channel_number or msgH[2] == publicChannel: - response += f"\n{msgH[0]}: {msgH[1]}" + new_line = f"\n{msgH[0]}: {msgH[1]}" + # Check if adding this line would exceed byte limit + test_response = response + new_line + if len(test_response.encode('utf-8')) > available_bytes: + # Try to add truncated version of the message + msg_text = msgH[1] + truncated = False + while len(msg_text) > 0 and len((response + f"\n{msgH[0]}: {msg_text}").encode('utf-8')) > available_bytes: + # Remove one character at a time from the end + msg_text = msg_text[:-1] + truncated = True + if len(msg_text) > 10: # Only add if we have at least 10 chars left + response += f"\n{msgH[0]}: {msg_text}" + ("..." if truncated else "") + break # Stop adding more messages + else: + response += new_line + if len(response) > 0: - return "📨Messages:" + response + return header + response else: return "No 📭messages in history"