Merge pull request #204 from SpudGunMan/copilot/fix-messages-bot-errors

Fix messages command error with Unicode characters (Cyrillic) I like this approach thanks GitHub
This commit is contained in:
Kelly
2025-10-08 09:21:22 -07:00
committed by GitHub
4 changed files with 38 additions and 6 deletions
+1 -1
View File
@@ -351,7 +351,7 @@ surveyRecordLocation=True
responseDelay = 2.2
# delay in seconds for splits in messages to avoid message collision /throttling
splitDelay = 2.5
# message chunk size for sending at high success rate, chunkr allows exceeding by 3 characters
# message chunk size in charcters, chunkr allows exceeding by 3 characters
MESSAGE_CHUNK_SIZE = 160
# Request Acknowledgement of message OTA
wantAck = False
+23 -2
View File
@@ -1087,6 +1087,11 @@ 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)
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 +1100,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"
+12 -2
View File
@@ -96,13 +96,15 @@ def bbs_post_message(subject, message, fromNode):
if str(fromNode) in bbs_ban_list:
logger.warning(f"System: Naughty node {fromNode}, tried to post a message: {subject}, {message} and was dropped.")
return "Message posted. ID is: " + str(messageID)
# validate message length isnt three times the MESSAGE_CHUNK_SIZE
if len(message) > (3 * MESSAGE_CHUNK_SIZE):
return "Message too long, max length is " + str(3 * MESSAGE_CHUNK_SIZE) + " characters."
# validate not a duplicate message
for msg in bbs_messages:
if msg[1].strip().lower() == subject.strip().lower() and msg[2].strip().lower() == message.strip().lower():
messageID = msg[0]
return "Message posted. ID is: " + str(messageID)
# validate its not overlength by keeping in chunker limit
# append the message to the list
bbs_messages.append([messageID, subject, message, fromNode])
logger.info(f"System: NEW Message Posted, subject: {subject}, message: {message} from {fromNode}")
@@ -153,6 +155,14 @@ def bbs_post_dm(toNode, message, fromNode):
if str(fromNode) in bbs_ban_list:
logger.warning(f"System: Naughty node {fromNode}, tried to post a message: {message} and was dropped.")
return "DM Posted for node " + str(toNode)
# validate message length isnt three times the MESSAGE_CHUNK_SIZE
if len(message) > (3 * MESSAGE_CHUNK_SIZE):
return "Message too long, max length is " + str(3 * MESSAGE_CHUNK_SIZE) + " characters."
# validate not a duplicate message
for msg in bbs_dm:
if msg[0] == int(toNode) and msg[1].strip().lower() == message.strip().lower():
return "DM Posted for node " + str(toNode)
# append the message to the list
bbs_dm.append([int(toNode), message, int(fromNode)])
+2 -1
View File
@@ -31,6 +31,7 @@ seenNodes = [] # list to hold the last seen nodes
surveyTracker, tictactoeTracker, hamtestTracker, hangmanTracker, golfTracker, mastermindTracker, vpTracker, blackjackTracker, lemonadeTracker, dwPlayerTracker = ([], [], [], [], [], [], [], [], [], [])
cmdHistory = [] # list to hold the command history for lheard and history commands
msg_history = [] # list to hold the message history for the messages command
max_bytes = 200 # Meshtastic has ~237 byte limit, use conservative 200 bytes for message content
# Read the config file, if it does not exist, create basic config file
config = configparser.ConfigParser()
@@ -384,7 +385,7 @@ try:
# messaging settings
responseDelay = config['messagingSettings'].getfloat('responseDelay', 0.7) # default 0.7
splitDelay = config['messagingSettings'].getfloat('splitDelay', 0) # default 0
MESSAGE_CHUNK_SIZE = config['messagingSettings'].getint('MESSAGE_CHUNK_SIZE', 160) # default 160
MESSAGE_CHUNK_SIZE = config['messagingSettings'].getint('MESSAGE_CHUNK_SIZE', 160) # default 160 chars
wantAck = config['messagingSettings'].getboolean('wantAck', False) # default False
maxBuffer = config['messagingSettings'].getint('maxBuffer', 200) # default 200
enableHopLogs = config['messagingSettings'].getboolean('enableHopLogs', False) # default False