diff --git a/config.template b/config.template index 0da141c..1d528bc 100644 --- a/config.template +++ b/config.template @@ -158,7 +158,7 @@ news_file_path = news.txt responseDelay = 0.7 # delay in seconds for splits in messages to avoid message collision splitDelay = 0.0 -# message chunk size for sending at high success rate +# message chunk size for sending at high success rate, chunkr allows exceeding by 3 characters MESSAGE_CHUNK_SIZE = 160 # Request Acknowledgement of message OTA wantAck = False diff --git a/modules/system.py b/modules/system.py index 7989d18..d7ce971 100644 --- a/modules/system.py +++ b/modules/system.py @@ -427,22 +427,39 @@ def messageChunker(message): else: # split the part into chunks current_chunk = '' - sentences = part.split('. ') + sentences = [] + sentence = '' + for char in part: + sentence += char + if char in '.!?': + sentences.append(sentence.strip()) + sentence = '' + if sentence: + sentences.append(sentence.strip()) + for sentence in sentences: - sentence = sentence.strip() sentence = sentence.replace(' ', ' ') # remove empty sentences if not sentence: continue + # remove junk sentences and append to the previous sentence this may exceed the MESSAGE_CHUNK_SIZE by 3 + if len(sentence) < 4: + if current_chunk: + current_chunk += ' ' + sentence + else: + current_chunk = sentence + continue # if sentence is too long, split it by words if len(current_chunk) + len(sentence) > MESSAGE_CHUNK_SIZE: - message_list.append(current_chunk) + if current_chunk: + message_list.append(current_chunk) current_chunk = sentence else: if current_chunk: - current_chunk += ' ' - current_chunk += sentence + current_chunk += ' ' + sentence + else: + current_chunk = sentence if current_chunk: message_list.append(current_chunk)