mirror of
				https://github.com/cdryzun/tg_bot_collections.git
				synced 2025-11-04 16:56:43 +08:00 
			
		
		
		
	fix: remove command prefix from messages
This commit is contained in:
		@ -115,6 +115,29 @@ def extract_prompt(message: str, bot_name: str) -> str:
 | 
				
			|||||||
    return message.strip()
 | 
					    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 wrap_handler(handler: T, bot: TeleBot) -> T:
 | 
				
			||||||
    def wrapper(message: Message, *args: Any, **kwargs: Any) -> None:
 | 
					    def wrapper(message: Message, *args: Any, **kwargs: Any) -> None:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
@ -478,7 +501,7 @@ class TelegraphAPI:
 | 
				
			|||||||
__all__ = [
 | 
					__all__ = [
 | 
				
			||||||
    "bot_reply_first",
 | 
					    "bot_reply_first",
 | 
				
			||||||
    "bot_reply_markdown",
 | 
					    "bot_reply_markdown",
 | 
				
			||||||
    "extract_prompt",
 | 
					    "remove_prompt_prefix",
 | 
				
			||||||
    "enrich_text_with_urls",
 | 
					    "enrich_text_with_urls",
 | 
				
			||||||
    "image_to_data_uri",
 | 
					    "image_to_data_uri",
 | 
				
			||||||
    "TelegraphAPI",
 | 
					    "TelegraphAPI",
 | 
				
			||||||
 | 
				
			|||||||
@ -256,16 +256,15 @@ def answer_it_handler(message: Message, bot: TeleBot) -> None:
 | 
				
			|||||||
        with open(local_image_path, "wb") as temp_file:
 | 
					        with open(local_image_path, "wb") as temp_file:
 | 
				
			||||||
            temp_file.write(downloaded_file)
 | 
					            temp_file.write(downloaded_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        m = original_m = extract_prompt(
 | 
					        m = original_m = remove_prompt_prefix(latest_message.caption.strip())
 | 
				
			||||||
            latest_message.caption.strip(), bot.get_me().username
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        ph_image_url = ph.upload_image(local_image_path)
 | 
					        ph_image_url = ph.upload_image(local_image_path)
 | 
				
			||||||
        full_answer += f"\n\n"
 | 
					        full_answer += f"\n\n"
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        # remove command from the reply message if present
 | 
					        m = original_m = remove_prompt_prefix(latest_message.text.strip())
 | 
				
			||||||
        m = original_m = extract_prompt(
 | 
					
 | 
				
			||||||
            latest_message.text.strip(), bot.get_me().username
 | 
					    if not m:
 | 
				
			||||||
        )
 | 
					        bot.reply_to(message, "The message was retrieved, but the prompt is empty.")
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    m = enrich_text_with_urls(m)
 | 
					    m = enrich_text_with_urls(m)
 | 
				
			||||||
    full_answer += f"Question:\n{m}\n" if len(m) < 300 else ""
 | 
					    full_answer += f"Question:\n{m}\n" if len(m) < 300 else ""
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user