mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 08:37:09 +08:00
reply immediately and make users feel bots little resposible
This commit is contained in:
parent
943ab4001a
commit
27444d6fa1
@ -21,32 +21,43 @@ T = TypeVar("T", bound=Callable)
|
|||||||
BOT_MESSAGE_LENGTH = 4000
|
BOT_MESSAGE_LENGTH = 4000
|
||||||
|
|
||||||
|
|
||||||
def bot_reply_markdown(message: Message, hint: str, text: str, bot: TeleBot) -> None:
|
def bot_reply_markdown(reply_id: Message, who: str, text: str, bot: TeleBot) -> None:
|
||||||
"""
|
"""
|
||||||
reply the Markdown by take care of the message length.
|
reply the Markdown by take care of the message length.
|
||||||
it will fallback to plain text in case of any failure
|
it will fallback to plain text in case of any failure
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
if len(text.encode("utf-8")) <= BOT_MESSAGE_LENGTH:
|
if len(text.encode("utf-8")) <= BOT_MESSAGE_LENGTH:
|
||||||
bot.reply_to(
|
bot.edit_message_text(
|
||||||
message,
|
f"**{who}**:\n{telegramify_markdown.convert(text)}",
|
||||||
f"**{hint}**:\n{telegramify_markdown.convert(text)}",
|
chat_id=reply_id.chat.id,
|
||||||
parse_mode="MarkdownV2",
|
message_id=reply_id.message_id,
|
||||||
|
parse_mode="MarkdownV2"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Need a split of message
|
# Need a split of message
|
||||||
msgs = smart_split(text, BOT_MESSAGE_LENGTH)
|
msgs = smart_split(text, BOT_MESSAGE_LENGTH)
|
||||||
for i in range(len(msgs)):
|
bot.edit_message_text(
|
||||||
|
f"**{who}** \[1/{len(msgs)}\]:\n{telegramify_markdown.convert(msgs[0])}",
|
||||||
|
chat_id=reply_id.chat.id,
|
||||||
|
message_id=reply_id.message_id,
|
||||||
|
parse_mode="MarkdownV2"
|
||||||
|
)
|
||||||
|
for i in range(1, len(msgs)):
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
reply_id.reply_to_message,
|
||||||
f"**{hint}** \[{i+1}/{len(msgs)}\]:\n{telegramify_markdown.convert(msgs[i])}",
|
f"**{hint}** \[{i+1}/{len(msgs)}\]:\n{telegramify_markdown.convert(msgs[i])}",
|
||||||
parse_mode="MarkdownV2",
|
parse_mode="MarkdownV2",
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
# print(f"wrong markdown format: {text}")
|
# print(f"wrong markdown format: {text}")
|
||||||
bot.reply_to(message, f"{hint}:\n{text}")
|
bot.edit_message_text(
|
||||||
|
f"**{who}**:\n{text}",
|
||||||
|
chat_id=reply_id.chat.id,
|
||||||
|
message_id=reply_id.message_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def extract_prompt(message: str, bot_name: str) -> str:
|
def extract_prompt(message: str, bot_name: str) -> str:
|
||||||
|
@ -37,7 +37,9 @@ def claude_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
player_message = claude_player_dict[str(message.from_user.id)]
|
player_message = claude_player_dict[str(message.from_user.id)]
|
||||||
if m.strip() == "clear":
|
|
||||||
|
q = m.strip()
|
||||||
|
if q == "clear" or len(q) == 0:
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
message,
|
||||||
"just clear you claude messages history",
|
"just clear you claude messages history",
|
||||||
@ -45,6 +47,12 @@ def claude_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# show something, make it more responsible
|
||||||
|
reply_id = bot.reply_to(message,
|
||||||
|
"**Claude** is __thinking__...",
|
||||||
|
parse_mode="MarkdownV2"
|
||||||
|
)
|
||||||
|
|
||||||
player_message.append({"role": "user", "content": m})
|
player_message.append({"role": "user", "content": m})
|
||||||
# keep the last 5, every has two ask and answer.
|
# keep the last 5, every has two ask and answer.
|
||||||
if len(player_message) > 10:
|
if len(player_message) > 10:
|
||||||
@ -57,7 +65,7 @@ def claude_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
# tricky
|
# tricky
|
||||||
player_message.pop()
|
player_message.pop()
|
||||||
r = client.messages.create(
|
r = client.messages.create(
|
||||||
max_tokens=1024, messages=player_message, model=ANTHROPIC_MODEL
|
max_tokens=4096, messages=player_message, model=ANTHROPIC_MODEL
|
||||||
)
|
)
|
||||||
if not r.content:
|
if not r.content:
|
||||||
claude_reply_text = "Claude did not answer."
|
claude_reply_text = "Claude did not answer."
|
||||||
@ -81,7 +89,7 @@ def claude_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
bot_reply_markdown(message, "Claude answer", claude_reply_text, bot)
|
bot_reply_markdown(reply_id, "Claude", claude_reply_text, bot)
|
||||||
|
|
||||||
|
|
||||||
def claude_pro_handler(message: Message, bot: TeleBot) -> None:
|
def claude_pro_handler(message: Message, bot: TeleBot) -> None:
|
||||||
@ -94,7 +102,8 @@ def claude_pro_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
player_message = claude_pro_player_dict[str(message.from_user.id)]
|
player_message = claude_pro_player_dict[str(message.from_user.id)]
|
||||||
if m.strip() == "clear":
|
q = m.strip()
|
||||||
|
if q == "clear" or len(q) == 0:
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
message,
|
||||||
"just clear you claude opus messages history",
|
"just clear you claude opus messages history",
|
||||||
|
@ -49,13 +49,21 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
gemini_player_dict[str(message.from_user.id)] = player
|
gemini_player_dict[str(message.from_user.id)] = player
|
||||||
else:
|
else:
|
||||||
player = gemini_player_dict[str(message.from_user.id)]
|
player = gemini_player_dict[str(message.from_user.id)]
|
||||||
if m.strip() == "clear":
|
q = m.strip()
|
||||||
|
if q == "clear" or len(q) == 0:
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
message,
|
||||||
"just clear you gemini messages history",
|
"just clear you gemini messages history",
|
||||||
)
|
)
|
||||||
player.history.clear()
|
player.history.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# show something, make it more responsible
|
||||||
|
reply_id = bot.reply_to(message,
|
||||||
|
"**Gemini** is __thinking__...",
|
||||||
|
parse_mode="MarkdownV2"
|
||||||
|
)
|
||||||
|
|
||||||
# keep the last 5, every has two ask and answer.
|
# keep the last 5, every has two ask and answer.
|
||||||
if len(player.history) > 10:
|
if len(player.history) > 10:
|
||||||
player.history = player.history[2:]
|
player.history = player.history[2:]
|
||||||
@ -64,7 +72,7 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
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 is often using ':' in **Title** which not work in Telegram Markdown
|
||||||
gemini_reply_text = gemini_reply_text.replace(": **", "**\: ")
|
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,7 +87,7 @@ def gemini_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# By default markdown
|
# By default markdown
|
||||||
bot_reply_markdown(message, "Gemini answer", gemini_reply_text, bot)
|
bot_reply_markdown(reply_id, "Gemini", gemini_reply_text, bot)
|
||||||
|
|
||||||
|
|
||||||
def gemini_photo_handler(message: Message, bot: TeleBot) -> None:
|
def gemini_photo_handler(message: Message, bot: TeleBot) -> None:
|
||||||
|
@ -35,7 +35,8 @@ def yi_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
player_message = yi_player_dict[str(message.from_user.id)]
|
player_message = yi_player_dict[str(message.from_user.id)]
|
||||||
if m.strip() == "clear":
|
q = m.strip()
|
||||||
|
if q == "clear" or len(q) == 0:
|
||||||
bot.reply_to(
|
bot.reply_to(
|
||||||
message,
|
message,
|
||||||
"just clear your yi messages history",
|
"just clear your yi messages history",
|
||||||
@ -43,6 +44,12 @@ def yi_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message.clear()
|
player_message.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# show something, make it more responsible
|
||||||
|
reply_id = bot.reply_to(message,
|
||||||
|
"**Yi** is __thinking__...",
|
||||||
|
parse_mode="MarkdownV2"
|
||||||
|
)
|
||||||
|
|
||||||
player_message.append({"role": "user", "content": m})
|
player_message.append({"role": "user", "content": m})
|
||||||
# keep the last 5, every has two ask and answer.
|
# keep the last 5, every has two ask and answer.
|
||||||
if len(player_message) > 10:
|
if len(player_message) > 10:
|
||||||
@ -81,7 +88,7 @@ def yi_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# reply back as Markdown and fallback to plain text if failed.
|
# reply back as Markdown and fallback to plain text if failed.
|
||||||
bot_reply_markdown(message, "yi answer", yi_reply_text, bot)
|
bot_reply_markdown(replay_id, "Yi", yi_reply_text, bot)
|
||||||
|
|
||||||
|
|
||||||
def _image_to_data_uri(file_path):
|
def _image_to_data_uri(file_path):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user