Spaces:
Sleeping
Sleeping
Update command_handler.py
Browse files- command_handler.py +24 -53
command_handler.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 1 |
-
# command_handler.py
|
| 2 |
import pandas as pd
|
| 3 |
from linebot.v3.messaging import TextMessage, ImageMessage
|
| 4 |
|
| 5 |
-
#
|
| 6 |
from cwa_service import fetch_cwa_alarm_list, fetch_significant_earthquakes, fetch_latest_significant_earthquake
|
| 7 |
from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
|
| 8 |
from plotting_service import create_and_save_map
|
| 9 |
from ai_service import generate_ai_text
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
def get_help_message() -> TextMessage:
|
| 13 |
-
"""回傳包含所有可用指令的說明訊息。"""
|
| 14 |
text = (
|
| 15 |
"📖 指令列表 (輸入 #數字 即可)\n\n"
|
| 16 |
"【地震資訊】\n"
|
|
@@ -29,18 +29,16 @@ def get_help_message() -> TextMessage:
|
|
| 29 |
return TextMessage(text=text)
|
| 30 |
|
| 31 |
def get_info_message() -> TextMessage:
|
| 32 |
-
"""回傳機器人資訊。"""
|
| 33 |
text = (
|
| 34 |
"🤖 關於我\n\n"
|
| 35 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
| 36 |
-
"• 版本:
|
| 37 |
-
"• 資料來源: CWA, USGS,
|
| 38 |
"• 開發者: dayichen"
|
| 39 |
)
|
| 40 |
return TextMessage(text=text)
|
| 41 |
|
| 42 |
def get_taiwan_earthquake_list() -> TextMessage:
|
| 43 |
-
"""回傳近期的台灣地震文字列表。"""
|
| 44 |
result = fetch_taiwan_df_this_year()
|
| 45 |
if isinstance(result, pd.DataFrame):
|
| 46 |
count = len(result)
|
|
@@ -53,14 +51,13 @@ def get_taiwan_earthquake_list() -> TextMessage:
|
|
| 53 |
f"報告連結: {row.get('url', '無')}"
|
| 54 |
)
|
| 55 |
if count > 15:
|
| 56 |
-
lines.append(f"... (還有 {count} 筆資料)")
|
| 57 |
reply_text = "\n\n".join(lines)
|
| 58 |
else:
|
| 59 |
reply_text = result
|
| 60 |
return TextMessage(text=reply_text)
|
| 61 |
|
| 62 |
def get_latest_earthquake_reply() -> list:
|
| 63 |
-
"""獲取最新地震資訊並組合成 LINE 訊息"""
|
| 64 |
try:
|
| 65 |
latest_eq = fetch_latest_significant_earthquake()
|
| 66 |
if not latest_eq:
|
|
@@ -90,69 +87,43 @@ def get_latest_earthquake_reply() -> list:
|
|
| 90 |
return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
|
| 91 |
|
| 92 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 93 |
-
"""處理使用者的文字訊息並回傳一個包含回覆訊息的列表。"""
|
| 94 |
user_message = (user_message_raw or "").strip()
|
| 95 |
|
| 96 |
-
# --- 指令對應 ---
|
| 97 |
cmd_map = {
|
| 98 |
'#1': '/latest', '#2': '/global', '#3': '/taiwan',
|
| 99 |
'#4': '/map', '#5': '/alert', '#6': '/significant',
|
| 100 |
'#7': '/ai', '#8': '/info', '#9': '/help',
|
| 101 |
-
'地震': '/global', 'quake': '/global',
|
| 102 |
'台灣地震': '/taiwan', '臺灣地震': '/taiwan',
|
| 103 |
'台灣地震畫圖': '/map', '臺灣地震畫圖': '/map',
|
| 104 |
'地震預警': '/alert',
|
| 105 |
-
'/help': '/help'
|
| 106 |
}
|
| 107 |
|
| 108 |
command = ""
|
| 109 |
arg = ""
|
| 110 |
|
| 111 |
-
if user_message.
|
|
|
|
|
|
|
| 112 |
parts = user_message.split(' ', 1)
|
| 113 |
key = parts[0]
|
|
|
|
| 114 |
if key in cmd_map:
|
| 115 |
command = cmd_map[key]
|
| 116 |
-
arg = parts[1] if len(parts) > 1 else ""
|
| 117 |
-
|
| 118 |
-
elif user_message.startswith('/'):
|
| 119 |
-
parts = user_message.split(' ', 1)
|
| 120 |
-
command = parts[0].lower()
|
| 121 |
-
arg = parts[1] if len(parts) > 1 else ""
|
| 122 |
-
|
| 123 |
-
else:
|
| 124 |
-
for key, value in cmd_map.items():
|
| 125 |
-
if user_message.lower() == key:
|
| 126 |
-
command = value
|
| 127 |
-
break
|
| 128 |
|
| 129 |
-
# --- 指令處理 ---
|
| 130 |
if command:
|
| 131 |
-
if command == '/help'
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
"請點擊以下連結進行查詢:\n"
|
| 144 |
-
"https://huggingface.co/spaces/cwadayi/MCP-2"
|
| 145 |
-
)
|
| 146 |
-
return [TextMessage(text=reply_text)]
|
| 147 |
-
elif command == '/alert':
|
| 148 |
-
return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 149 |
-
elif command == '/significant':
|
| 150 |
-
return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 151 |
-
elif command == '/ai':
|
| 152 |
-
prompt = arg or user_message[2:].lstrip()
|
| 153 |
-
if not prompt:
|
| 154 |
-
return [TextMessage(text="請輸入問題,例如:#7 台灣最高的山是哪座?")]
|
| 155 |
return [TextMessage(text=generate_ai_text(prompt))]
|
| 156 |
|
| 157 |
-
# 若非任何指令,則預設交給 AI 處理
|
| 158 |
return [TextMessage(text=generate_ai_text(user_message))]
|
|
|
|
| 1 |
+
# command_handler.py (Corrected Version)
|
| 2 |
import pandas as pd
|
| 3 |
from linebot.v3.messaging import TextMessage, ImageMessage
|
| 4 |
|
| 5 |
+
# 匯入所有服務函式
|
| 6 |
from cwa_service import fetch_cwa_alarm_list, fetch_significant_earthquakes, fetch_latest_significant_earthquake
|
| 7 |
from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
|
| 8 |
from plotting_service import create_and_save_map
|
| 9 |
from ai_service import generate_ai_text
|
| 10 |
+
# [修正] 將 HF_SPACE_URL 改為 MCP_SERVER_URL
|
| 11 |
+
from config import CURRENT_YEAR, MCP_SERVER_URL
|
| 12 |
|
| 13 |
def get_help_message() -> TextMessage:
|
|
|
|
| 14 |
text = (
|
| 15 |
"📖 指令列表 (輸入 #數字 即可)\n\n"
|
| 16 |
"【地震資訊】\n"
|
|
|
|
| 29 |
return TextMessage(text=text)
|
| 30 |
|
| 31 |
def get_info_message() -> TextMessage:
|
|
|
|
| 32 |
text = (
|
| 33 |
"🤖 關於我\n\n"
|
| 34 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
| 35 |
+
"• 版本: 5.0 (Gemini Edition)\n"
|
| 36 |
+
"• 資料來源: CWA, USGS, Google Gemini\n"
|
| 37 |
"• 開發者: dayichen"
|
| 38 |
)
|
| 39 |
return TextMessage(text=text)
|
| 40 |
|
| 41 |
def get_taiwan_earthquake_list() -> TextMessage:
|
|
|
|
| 42 |
result = fetch_taiwan_df_this_year()
|
| 43 |
if isinstance(result, pd.DataFrame):
|
| 44 |
count = len(result)
|
|
|
|
| 51 |
f"報告連結: {row.get('url', '無')}"
|
| 52 |
)
|
| 53 |
if count > 15:
|
| 54 |
+
lines.append(f"... (還有 {count-15} 筆資料)")
|
| 55 |
reply_text = "\n\n".join(lines)
|
| 56 |
else:
|
| 57 |
reply_text = result
|
| 58 |
return TextMessage(text=reply_text)
|
| 59 |
|
| 60 |
def get_latest_earthquake_reply() -> list:
|
|
|
|
| 61 |
try:
|
| 62 |
latest_eq = fetch_latest_significant_earthquake()
|
| 63 |
if not latest_eq:
|
|
|
|
| 87 |
return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
|
| 88 |
|
| 89 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
|
|
| 90 |
user_message = (user_message_raw or "").strip()
|
| 91 |
|
|
|
|
| 92 |
cmd_map = {
|
| 93 |
'#1': '/latest', '#2': '/global', '#3': '/taiwan',
|
| 94 |
'#4': '/map', '#5': '/alert', '#6': '/significant',
|
| 95 |
'#7': '/ai', '#8': '/info', '#9': '/help',
|
| 96 |
+
'地震': '/global', 'quake': '/global', '幫助': '/help',
|
| 97 |
'台灣地震': '/taiwan', '臺灣地震': '/taiwan',
|
| 98 |
'台灣地震畫圖': '/map', '臺灣地震畫圖': '/map',
|
| 99 |
'地震預警': '/alert',
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
command = ""
|
| 103 |
arg = ""
|
| 104 |
|
| 105 |
+
if user_message.lower() in cmd_map:
|
| 106 |
+
command = cmd_map[user_message.lower()]
|
| 107 |
+
elif user_message.startswith(('#', '/')):
|
| 108 |
parts = user_message.split(' ', 1)
|
| 109 |
key = parts[0]
|
| 110 |
+
arg = parts[1].strip() if len(parts) > 1 else ""
|
| 111 |
if key in cmd_map:
|
| 112 |
command = cmd_map[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
|
|
|
| 114 |
if command:
|
| 115 |
+
if command == '/help': return [get_help_message()]
|
| 116 |
+
if command == '/info': return [get_info_message()]
|
| 117 |
+
if command == '/latest': return get_latest_earthquake_reply()
|
| 118 |
+
if command == '/global': return [TextMessage(text=fetch_global_last24h_text())]
|
| 119 |
+
if command == '/taiwan': return [get_taiwan_earthquake_list()]
|
| 120 |
+
# [修正] 將 HF_SPACE_URL 改為 MCP_SERVER_URL
|
| 121 |
+
if command == '/map': return [TextMessage(text=f"🗺️ 外部地震查詢服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]
|
| 122 |
+
if command == '/alert': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 123 |
+
if command == '/significant': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 124 |
+
if command == '/ai':
|
| 125 |
+
prompt = arg
|
| 126 |
+
if not prompt: return [TextMessage(text="請輸入問題,例如:#7 台灣最高的山是哪座?")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
return [TextMessage(text=generate_ai_text(prompt))]
|
| 128 |
|
|
|
|
| 129 |
return [TextMessage(text=generate_ai_text(user_message))]
|