Skip to content

Commit a83e236

Browse files
committed
v0.7503 - msg filtering, error catching
1 parent 99660fa commit a83e236

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ If you run into any issues, consult the logs or reach out on the repository's [I
236236
---
237237

238238
# Changelog
239+
- v0.7503 - improved message formatting & error catching
239240
- v0.7502 - added `docker_setup.sh` for easier Docker-based deployment
240241
- v0.7501 - `Dockerfile` and better error catching when receiving `401 Unauthorized`
241242
- v0.75 **Major refactoring** _(5. Oct 2024)_ 👀💦🌀

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# https://github.com/FlyingFathead/TelegramBot-OpenAI-API
66
#
77
# version of this program
8-
version_number = "0.7501"
8+
version_number = "0.7503"
99

1010
# Add the project root directory to Python's path
1111
import sys

src/text_message_handler.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)