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 import TeleBot
|
||||||
from telebot.types import BotCommand, Message
|
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)
|
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:
|
def extract_prompt(message: str, bot_name: str) -> str:
|
||||||
"""
|
"""
|
||||||
@ -94,3 +130,7 @@ def list_available_commands() -> list[str]:
|
|||||||
continue
|
continue
|
||||||
commands.append(child.stem)
|
commands.append(child.stem)
|
||||||
return commands
|
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 import TeleBot
|
||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
|
|
||||||
|
from . import bot_reply_markdown
|
||||||
|
|
||||||
from telegramify_markdown import convert
|
from telegramify_markdown import convert
|
||||||
from telegramify_markdown.customize import markdown_symbol
|
from telegramify_markdown.customize import markdown_symbol
|
||||||
|
|
||||||
@ -79,18 +81,7 @@ def claude_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
bot_reply_markdown(message, "Claude answer", claude_reply_text, bot)
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def claude_pro_handler(message: Message, bot: TeleBot) -> None:
|
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 google.generativeai.types.generation_types import StopCandidateException
|
||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
from telebot.types import Message
|
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
|
from . import bot_reply_markdown
|
||||||
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
|
|
||||||
|
|
||||||
GOOGLE_GEMINI_KEY = environ.get("GOOGLE_GEMINI_KEY")
|
GOOGLE_GEMINI_KEY = environ.get("GOOGLE_GEMINI_KEY")
|
||||||
|
|
||||||
@ -18,7 +15,7 @@ generation_config = {
|
|||||||
"temperature": 0.7,
|
"temperature": 0.7,
|
||||||
"top_p": 1,
|
"top_p": 1,
|
||||||
"top_k": 1,
|
"top_k": 1,
|
||||||
"max_output_tokens": 2048,
|
"max_output_tokens": 4096,
|
||||||
}
|
}
|
||||||
|
|
||||||
safety_settings = [
|
safety_settings = [
|
||||||
@ -66,6 +63,8 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
try:
|
try:
|
||||||
player.send_message(m)
|
player.send_message(m)
|
||||||
gemini_reply_text = player.last.text.strip()
|
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:
|
except StopCandidateException as e:
|
||||||
match = re.search(r'content\s*{\s*parts\s*{\s*text:\s*"([^"]+)"', str(e))
|
match = re.search(r'content\s*{\s*parts\s*{\s*text:\s*"([^"]+)"', str(e))
|
||||||
if match:
|
if match:
|
||||||
@ -79,18 +78,8 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
# By default markdown
|
||||||
bot.reply_to(
|
bot_reply_markdown(message, "Gemini answer", gemini_reply_text, bot)
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def gemini_photo_handler(message: Message, bot: TeleBot) -> None:
|
def gemini_photo_handler(message: Message, bot: TeleBot) -> None:
|
||||||
|
@ -7,11 +7,7 @@ import requests
|
|||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
|
|
||||||
import telegramify_markdown
|
from . import bot_reply_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
|
|
||||||
|
|
||||||
YI_BASE_URL = environ.get("YI_BASE_URL")
|
YI_BASE_URL = environ.get("YI_BASE_URL")
|
||||||
YI_API_KEY = environ.get("YI_API_KEY")
|
YI_API_KEY = environ.get("YI_API_KEY")
|
||||||
@ -84,18 +80,8 @@ def yi_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.pop()
|
player_message.pop()
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
# reply back as Markdown and fallback to plain text if failed.
|
||||||
bot.reply_to(
|
bot_reply_markdown(message, "yi answer", yi_reply_text, bot)
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _image_to_data_uri(file_path):
|
def _image_to_data_uri(file_path):
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
pyTelegramBotAPI
|
pyTelegramBotAPI>=4.16
|
||||||
cairosvg
|
cairosvg
|
||||||
github_poster
|
github_poster
|
||||||
prettymapp
|
prettymapp
|
||||||
google-generativeai==0.3.1
|
google-generativeai>=0.4
|
||||||
anthropic
|
anthropic
|
||||||
telegramify-markdown
|
telegramify-markdown
|
||||||
openai
|
openai
|
||||||
|
Loading…
x
Reference in New Issue
Block a user