Spaces:
Sleeping
Sleeping
Update command_handler.py
Browse files- command_handler.py +38 -30
command_handler.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# command_handler.py (
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
from linebot.v3.messaging import TextMessage, ImageMessage
|
|
@@ -10,20 +10,33 @@ from ai_service import generate_ai_text
|
|
| 10 |
from config import CURRENT_YEAR, MCP_SERVER_URL
|
| 11 |
|
| 12 |
def get_help_message() -> TextMessage:
|
| 13 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 14 |
text = (
|
| 15 |
-
"📖 指令列表 (
|
| 16 |
-
"
|
|
|
|
| 17 |
"• 1 - 最新一筆顯著地震 (含圖)\n"
|
| 18 |
-
"
|
| 19 |
-
"•
|
|
|
|
|
|
|
|
|
|
| 20 |
"• 4 - CWA 地震目錄查詢 (外部連結)\n"
|
|
|
|
| 21 |
"• 5 - CWA 最新地震預警\n"
|
| 22 |
-
"
|
| 23 |
-
"
|
|
|
|
|
|
|
| 24 |
"• 7 <問題> - 與 AI 助理對話\n"
|
| 25 |
-
"
|
| 26 |
-
"
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
return TextMessage(text=text)
|
| 29 |
|
|
@@ -32,7 +45,7 @@ def get_info_message() -> TextMessage:
|
|
| 32 |
text = (
|
| 33 |
"🤖 關於我\n\n"
|
| 34 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
| 35 |
-
"• 版本: 5.
|
| 36 |
"• 資料來源: CWA, USGS, Google Gemini\n"
|
| 37 |
"• 開發者: dayichen"
|
| 38 |
)
|
|
@@ -79,31 +92,26 @@ def get_latest_earthquake_reply() -> list:
|
|
| 79 |
except Exception as e: return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
|
| 80 |
|
| 81 |
def preprocess_ai_prompt(prompt: str) -> str:
|
| 82 |
-
"""
|
| 83 |
-
[新增] 預處理使用者發給 AI 的問題。
|
| 84 |
-
如果偵測到特定關鍵字組合,就將問題改寫為更明確的指令。
|
| 85 |
-
"""
|
| 86 |
-
# 尋找 "YYYY年" 的格式
|
| 87 |
year_match = re.search(r"(\d{4})年", prompt)
|
| 88 |
-
|
| 89 |
-
# 如果同時找到年份和 "最大地震"
|
| 90 |
if year_match and "最大地震" in prompt:
|
| 91 |
year = year_match.group(1)
|
| 92 |
-
# 改寫成一個非常明確的指令
|
| 93 |
new_prompt = f"請幫我呼叫地震搜尋工具,找出{year}年1月1日至{year}年12月31日之間,規模最大的地震。"
|
| 94 |
print(f"--- AI Prompt Rewritten ---\nOriginal: {prompt}\nNew: {new_prompt}\n---------------------------")
|
| 95 |
return new_prompt
|
| 96 |
-
|
| 97 |
-
# 如果沒有匹配,回傳原始問題
|
| 98 |
return prompt
|
| 99 |
|
| 100 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 101 |
"""處理使用者輸入的主函式。"""
|
| 102 |
user_message = (user_message_raw or "").strip()
|
|
|
|
|
|
|
|
|
|
| 103 |
parts = user_message.split(' ', 1)
|
| 104 |
command_key = parts[0]
|
| 105 |
arg = parts[1].strip() if len(parts) > 1 else ""
|
| 106 |
|
|
|
|
| 107 |
if command_key == '1': return get_latest_earthquake_reply()
|
| 108 |
if command_key == '2': return [TextMessage(text=fetch_global_last24h_text())]
|
| 109 |
if command_key == '3': return [get_taiwan_earthquake_list()]
|
|
@@ -113,14 +121,14 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
| 113 |
if command_key == '8': return [get_info_message()]
|
| 114 |
if command_key == '9': return [get_help_message()]
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
# 如果是 AI 指令 (7) 或其他無法識別的指令,先經過預處理
|
| 118 |
-
final_prompt = user_message
|
| 119 |
if command_key == '7':
|
| 120 |
-
if not arg:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
| 1 |
+
# command_handler.py (Help message now includes English translations)
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
from linebot.v3.messaging import TextMessage, ImageMessage
|
|
|
|
| 10 |
from config import CURRENT_YEAR, MCP_SERVER_URL
|
| 11 |
|
| 12 |
def get_help_message() -> TextMessage:
|
| 13 |
+
"""
|
| 14 |
+
[核心修改]
|
| 15 |
+
產生並回傳包含所有指令的說明文字,並附上英文翻譯。
|
| 16 |
+
"""
|
| 17 |
text = (
|
| 18 |
+
"📖 指令列表 (Command List)\n"
|
| 19 |
+
" (請直接輸入數字 / Please enter a number)\n\n"
|
| 20 |
+
"【地震資訊 (Earthquake Info)】\n"
|
| 21 |
"• 1 - 最新一筆顯著地震 (含圖)\n"
|
| 22 |
+
" (Latest Significant Earthquake w/ Map)\n"
|
| 23 |
+
"• 2 - 全球近24小時顯著地震 (USGS)\n"
|
| 24 |
+
" (Global Quakes in 24h - USGS)\n"
|
| 25 |
+
"• 3 - 今年台灣顯著地震列表 (USGS)\n"
|
| 26 |
+
" (Taiwan Quakes This Year - USGS)\n"
|
| 27 |
"• 4 - CWA 地震目錄查詢 (外部連結)\n"
|
| 28 |
+
" (CWA Catalog Search - External Link)\n"
|
| 29 |
"• 5 - CWA 最新地震預警\n"
|
| 30 |
+
" (Latest CWA Earthquake Alerts)\n"
|
| 31 |
+
"• 6 - CWA 最近7天顯著有感地震\n"
|
| 32 |
+
" (Significant Quakes in 7 Days - CWA)\n\n"
|
| 33 |
+
"【AI 與工具 (AI & Tools)】\n"
|
| 34 |
"• 7 <問題> - 與 AI 助理對話\n"
|
| 35 |
+
" (Chat with AI Assistant)\n"
|
| 36 |
+
" (e.g., 7 2023年臺灣最大地震)\n\n"
|
| 37 |
+
"【基本指令 (Basic Commands)】\n"
|
| 38 |
+
"• 8 - 關於此機器人 (About this Bot)\n"
|
| 39 |
+
"• 9 - 顯示此說明 (Show this Help Message)"
|
| 40 |
)
|
| 41 |
return TextMessage(text=text)
|
| 42 |
|
|
|
|
| 45 |
text = (
|
| 46 |
"🤖 關於我\n\n"
|
| 47 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
| 48 |
+
"• 版本: 5.3 (Gemini Command-Only Edition)\n"
|
| 49 |
"• 資料來源: CWA, USGS, Google Gemini\n"
|
| 50 |
"• 開發者: dayichen"
|
| 51 |
)
|
|
|
|
| 92 |
except Exception as e: return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
|
| 93 |
|
| 94 |
def preprocess_ai_prompt(prompt: str) -> str:
|
| 95 |
+
"""預處理使用者發給 AI 的問題。"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
year_match = re.search(r"(\d{4})年", prompt)
|
|
|
|
|
|
|
| 97 |
if year_match and "最大地震" in prompt:
|
| 98 |
year = year_match.group(1)
|
|
|
|
| 99 |
new_prompt = f"請幫我呼叫地震搜尋工具,找出{year}年1月1日至{year}年12月31日之間,規模最大的地震。"
|
| 100 |
print(f"--- AI Prompt Rewritten ---\nOriginal: {prompt}\nNew: {new_prompt}\n---------------------------")
|
| 101 |
return new_prompt
|
|
|
|
|
|
|
| 102 |
return prompt
|
| 103 |
|
| 104 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 105 |
"""處理使用者輸入的主函式。"""
|
| 106 |
user_message = (user_message_raw or "").strip()
|
| 107 |
+
if not user_message:
|
| 108 |
+
return [] # 如果是空訊息,不回應
|
| 109 |
+
|
| 110 |
parts = user_message.split(' ', 1)
|
| 111 |
command_key = parts[0]
|
| 112 |
arg = parts[1].strip() if len(parts) > 1 else ""
|
| 113 |
|
| 114 |
+
# 處理固定的數字指令
|
| 115 |
if command_key == '1': return get_latest_earthquake_reply()
|
| 116 |
if command_key == '2': return [TextMessage(text=fetch_global_last24h_text())]
|
| 117 |
if command_key == '3': return [get_taiwan_earthquake_list()]
|
|
|
|
| 121 |
if command_key == '8': return [get_info_message()]
|
| 122 |
if command_key == '9': return [get_help_message()]
|
| 123 |
|
| 124 |
+
# 只有當指令是 '7' 時,才啟用 AI
|
|
|
|
|
|
|
| 125 |
if command_key == '7':
|
| 126 |
+
if not arg:
|
| 127 |
+
return [TextMessage(text="請輸入問題��例如:7 台灣最高的山是哪座?")]
|
| 128 |
+
|
| 129 |
+
# 將問題進行預處理,然後交給 AI
|
| 130 |
+
processed_prompt = preprocess_ai_prompt(arg)
|
| 131 |
+
return [TextMessage(text=generate_ai_text(processed_prompt))]
|
| 132 |
|
| 133 |
+
# 對於所有其他無法識別的訊息 (一般對話),回傳說明指令
|
| 134 |
+
return [get_help_message()]
|