mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
Split to multiple reply if text length is more than 4000 bytes
This commit is contained in:
parent
0c90e27d45
commit
13090b6a42
@ -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"]
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user