From 13090b6a42a2a5684556ef70277ae6261c1b7ba4 Mon Sep 17 00:00:00 2001 From: "Y1.Zhao" Date: Tue, 19 Mar 2024 04:20:22 -0400 Subject: [PATCH] Split to multiple reply if text length is more than 4000 bytes --- handlers/__init__.py | 40 ++++++++++++++++++++++++++++++++++++++++ handlers/claude.py | 15 +++------------ handlers/gemini.py | 23 ++++++----------------- handlers/yi.py | 20 +++----------------- requirements.txt | 4 ++-- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/handlers/__init__.py b/handlers/__init__.py index 80912de..237d6e6 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -9,9 +9,45 @@ from typing import Any, Callable, TypeVar from telebot import TeleBot from telebot.types import BotCommand, Message +from telebot.util import smart_split +import telegramify_markdown +from telegramify_markdown.customize import markdown_symbol + +markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol +markdown_symbol.link = "🔗" # If you want, Customizing the link symbol T = TypeVar("T", bound=Callable) +BOT_MESSAGE_LENGTH = 4000 + + +def bot_reply_markdown(message: Message, hint: str, text: str, bot: TeleBot) -> None: + """ + reply the Markdown by take care of the message length. + it will fallback to plain text in case of any failure + """ + try: + if len(text.encode("utf-8")) <= BOT_MESSAGE_LENGTH: + bot.reply_to( + message, + f"**{hint}**:\n{telegramify_markdown.convert(text)}", + parse_mode="MarkdownV2", + ) + return + + # Need a split of message + msgs = smart_split(text, BOT_MESSAGE_LENGTH) + for i in range(len(msgs)): + bot.reply_to( + message, + f"**{hint}** \[{i+1}/{len(msgs)}\]:\n{telegramify_markdown.convert(msgs[i])}", + parse_mode="MarkdownV2", + ) + except Exception as e: + print(traceback.format_exc()) + # print(f"wrong markdown format: {text}") + bot.reply_to(message, f"{hint}:\n{text}") + def extract_prompt(message: str, bot_name: str) -> str: """ @@ -94,3 +130,7 @@ def list_available_commands() -> list[str]: continue commands.append(child.stem) return commands + + +# `import *` will give you these +__all__ = ["bot_reply_markdown", "extract_prompt"] diff --git a/handlers/claude.py b/handlers/claude.py index 742a0ae..2f7cf2b 100644 --- a/handlers/claude.py +++ b/handlers/claude.py @@ -6,6 +6,8 @@ from anthropic import Anthropic, APITimeoutError from telebot import TeleBot from telebot.types import Message +from . import bot_reply_markdown + from telegramify_markdown import convert from telegramify_markdown.customize import markdown_symbol @@ -79,18 +81,7 @@ def claude_handler(message: Message, bot: TeleBot) -> None: player_message.clear() return - try: - bot.reply_to( - message, - "Claude answer:\n" + convert(claude_reply_text), - parse_mode="MarkdownV2", - ) - except: - print("wrong markdown format") - bot.reply_to( - message, - "claude answer:\n\n" + claude_reply_text, - ) + bot_reply_markdown(message, "Claude answer", claude_reply_text, bot) def claude_pro_handler(message: Message, bot: TeleBot) -> None: diff --git a/handlers/gemini.py b/handlers/gemini.py index 8c6549c..84dc99c 100644 --- a/handlers/gemini.py +++ b/handlers/gemini.py @@ -5,11 +5,8 @@ import google.generativeai as genai from google.generativeai.types.generation_types import StopCandidateException from telebot import TeleBot from telebot.types import Message -import telegramify_markdown -from telegramify_markdown.customize import markdown_symbol -markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol -markdown_symbol.link = "🔗" # If you want, Customizing the link symbol +from . import bot_reply_markdown GOOGLE_GEMINI_KEY = environ.get("GOOGLE_GEMINI_KEY") @@ -18,7 +15,7 @@ generation_config = { "temperature": 0.7, "top_p": 1, "top_k": 1, - "max_output_tokens": 2048, + "max_output_tokens": 4096, } safety_settings = [ @@ -66,6 +63,8 @@ def gemini_handler(message: Message, bot: TeleBot) -> None: try: player.send_message(m) gemini_reply_text = player.last.text.strip() + # Gemini is often using ':' in **Title** which not work in Telegram Markdown + gemini_reply_text = gemini_reply_text.replace(": **", "**\: ") except StopCandidateException as e: match = re.search(r'content\s*{\s*parts\s*{\s*text:\s*"([^"]+)"', str(e)) if match: @@ -79,18 +78,8 @@ def gemini_handler(message: Message, bot: TeleBot) -> None: ) return - try: - bot.reply_to( - message, - "Gemini answer:\n" + telegramify_markdown.convert(gemini_reply_text), - parse_mode="MarkdownV2", - ) - except: - print("wrong markdown format") - bot.reply_to( - message, - "Gemini answer:\n\n" + gemini_reply_text, - ) + # By default markdown + bot_reply_markdown(message, "Gemini answer", gemini_reply_text, bot) def gemini_photo_handler(message: Message, bot: TeleBot) -> None: diff --git a/handlers/yi.py b/handlers/yi.py index 04a312b..7be7c48 100644 --- a/handlers/yi.py +++ b/handlers/yi.py @@ -7,11 +7,7 @@ import requests from telebot import TeleBot from telebot.types import Message -import telegramify_markdown -from telegramify_markdown.customize import markdown_symbol - -markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol -markdown_symbol.link = "🔗" # If you want, Customizing the link symbol +from . import bot_reply_markdown YI_BASE_URL = environ.get("YI_BASE_URL") YI_API_KEY = environ.get("YI_API_KEY") @@ -84,18 +80,8 @@ def yi_handler(message: Message, bot: TeleBot) -> None: player_message.pop() return - try: - bot.reply_to( - message, - "yi answer:\n" + telegramify_markdown.convert(yi_reply_text), - parse_mode="MarkdownV2", - ) - except: - print("wrong markdown format") - bot.reply_to( - message, - "yi answer:\n\n" + yi_reply_text, - ) + # reply back as Markdown and fallback to plain text if failed. + bot_reply_markdown(message, "yi answer", yi_reply_text, bot) def _image_to_data_uri(file_path): diff --git a/requirements.txt b/requirements.txt index b1e0ae4..51463d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -pyTelegramBotAPI +pyTelegramBotAPI>=4.16 cairosvg github_poster prettymapp -google-generativeai==0.3.1 +google-generativeai>=0.4 anthropic telegramify-markdown openai