mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 00:27:09 +08:00
fix: remove command prefix from messages
This commit is contained in:
parent
b66c9ac136
commit
d0494a9372
@ -115,6 +115,29 @@ def extract_prompt(message: str, bot_name: str) -> str:
|
||||
return message.strip()
|
||||
|
||||
|
||||
def remove_prompt_prefix(message: str) -> str:
|
||||
"""
|
||||
Remove "/cmd" or "/cmd@bot_name" or "cmd:"
|
||||
"""
|
||||
message += " "
|
||||
# Explanation of the regex pattern:
|
||||
# ^ - Match the start of the string
|
||||
# ( - Start of the group
|
||||
# / - Literal forward slash
|
||||
# [a-zA-Z] - Any letter (start of the command)
|
||||
# [a-zA-Z0-9_]* - Any number of letters, digits, or underscores
|
||||
# (@\w+)? - Optionally match @ followed by one or more word characters (for bot name)
|
||||
# \s - A single whitespace character (space or newline)
|
||||
# | - OR
|
||||
# [a-zA-Z] - Any letter (start of the command)
|
||||
# [a-zA-Z0-9_]* - Any number of letters, digits, or underscores
|
||||
# :\s - Colon followed by a single whitespace character
|
||||
# ) - End of the group
|
||||
pattern = r"^(/[a-zA-Z][a-zA-Z0-9_]*(@\w+)?\s|[a-zA-Z][a-zA-Z0-9_]*:\s)"
|
||||
|
||||
return re.sub(pattern, "", message).strip()
|
||||
|
||||
|
||||
def wrap_handler(handler: T, bot: TeleBot) -> T:
|
||||
def wrapper(message: Message, *args: Any, **kwargs: Any) -> None:
|
||||
try:
|
||||
@ -478,7 +501,7 @@ class TelegraphAPI:
|
||||
__all__ = [
|
||||
"bot_reply_first",
|
||||
"bot_reply_markdown",
|
||||
"extract_prompt",
|
||||
"remove_prompt_prefix",
|
||||
"enrich_text_with_urls",
|
||||
"image_to_data_uri",
|
||||
"TelegraphAPI",
|
||||
|
@ -256,16 +256,15 @@ def answer_it_handler(message: Message, bot: TeleBot) -> None:
|
||||
with open(local_image_path, "wb") as temp_file:
|
||||
temp_file.write(downloaded_file)
|
||||
|
||||
m = original_m = extract_prompt(
|
||||
latest_message.caption.strip(), bot.get_me().username
|
||||
)
|
||||
m = original_m = remove_prompt_prefix(latest_message.caption.strip())
|
||||
ph_image_url = ph.upload_image(local_image_path)
|
||||
full_answer += f"\n\n"
|
||||
else:
|
||||
# remove command from the reply message if present
|
||||
m = original_m = extract_prompt(
|
||||
latest_message.text.strip(), bot.get_me().username
|
||||
)
|
||||
m = original_m = remove_prompt_prefix(latest_message.text.strip())
|
||||
|
||||
if not m:
|
||||
bot.reply_to(message, "The message was retrieved, but the prompt is empty.")
|
||||
return
|
||||
|
||||
m = enrich_text_with_urls(m)
|
||||
full_answer += f"Question:\n{m}\n" if len(m) < 300 else ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user