Skip to content

Commit

Permalink
v0.7503 - msg filtering, error catching
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingFathead committed Oct 5, 2024
1 parent 99660fa commit a83e236
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ If you run into any issues, consult the logs or reach out on the repository's [I
---

# Changelog
- v0.7503 - improved message formatting & error catching
- v0.7502 - added `docker_setup.sh` for easier Docker-based deployment
- v0.7501 - `Dockerfile` and better error catching when receiving `401 Unauthorized`
- v0.75 **Major refactoring** _(5. Oct 2024)_ 👀💦🌀
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# https://github.com/FlyingFathead/TelegramBot-OpenAI-API
#
# version of this program
version_number = "0.7501"
version_number = "0.7503"

# Add the project root directory to Python's path
import sys
Expand Down
48 changes: 43 additions & 5 deletions src/text_message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,17 @@ def sanitize_html(content):
message=bot_reply,
)

await context.bot.send_message(
chat_id=chat_id,
text=escaped_reply,
parse_mode=ParseMode.HTML
)
# # send the response
# await context.bot.send_message(
# chat_id=chat_id,
# text=escaped_reply,
# parse_mode=ParseMode.HTML
# )

message_parts = split_message(escaped_reply)

for part in message_parts:
await context.bot.send_message(chat_id=chat_id, text=part, parse_mode=ParseMode.HTML)

stop_typing_event.set()
context.user_data.pop('active_translation', None)
Expand Down Expand Up @@ -1242,6 +1248,38 @@ async def make_api_request(bot, chat_history, timeout=30):
bot.logger.error(f"An error occurred while making the API request: {str(e)}")
raise e

# split long messages
def split_message(message, max_length=4000):
"""
Split a long message into multiple smaller messages.
Args:
message (str): The message to split.
max_length (int): Maximum length of each part.
Returns:
list: A list containing message parts.
"""
# List to store split messages
message_parts = []

# While there is still text to split
while len(message) > max_length:
# Split at the nearest period or line break before the max_length
split_index = message.rfind('\n', 0, max_length)
if split_index == -1:
split_index = message.rfind('. ', 0, max_length)
if split_index == -1:
split_index = max_length # Split at max length if no better point is found
# Add the message part
message_parts.append(message[:split_index].strip())
# Update the remaining message
message = message[split_index:].strip()

# Add the last part
if message:
message_parts.append(message)

return message_parts

# # // (old request type)
# async def make_api_request(bot, chat_history, timeout=30):
# # Prepare the payload for the API request with updated chat history
Expand Down

0 comments on commit a83e236

Please sign in to comment.