Spaces:
Sleeping
Sleeping
Update command_handler.py
Browse files- command_handler.py +15 -24
command_handler.py
CHANGED
|
@@ -5,7 +5,7 @@ from linebot.v3.messaging import TextMessage, ImageMessage
|
|
| 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 |
from config import CURRENT_YEAR, HF_SPACE_URL
|
| 11 |
|
|
@@ -17,7 +17,7 @@ def get_help_message() -> TextMessage:
|
|
| 17 |
"• #1 - 最新顯著地震 (含圖)\n"
|
| 18 |
"• #2 - 全球近24小時顯著地震\n"
|
| 19 |
"• #3 - 今年台灣顯著地震列表\n"
|
| 20 |
-
"• #4 -
|
| 21 |
"• #5 - CWA 最新地震預警\n"
|
| 22 |
"• #6 - CWA 最新顯著有感地震\n\n"
|
| 23 |
"【AI 與工具】\n"
|
|
@@ -33,7 +33,7 @@ def get_info_message() -> TextMessage:
|
|
| 33 |
text = (
|
| 34 |
"🤖 關於我\n\n"
|
| 35 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
| 36 |
-
"• 版本: 3.
|
| 37 |
"• 資料來源: CWA, USGS, Hugging Face\n"
|
| 38 |
"• 開發者: dayichen"
|
| 39 |
)
|
|
@@ -53,26 +53,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 |
-
|
| 63 |
-
"""產生地震地圖,並回傳包含連結的文字訊息。"""
|
| 64 |
-
result = fetch_taiwan_df_this_year()
|
| 65 |
-
if isinstance(result, pd.DataFrame):
|
| 66 |
-
filename = create_and_save_map(result)
|
| 67 |
-
image_url = f"{(HF_SPACE_URL or base_url)}/static/{filename}"
|
| 68 |
-
reply_text = (
|
| 69 |
-
"🗺️ 已為您繪製地震分佈圖。\n\n"
|
| 70 |
-
"請點擊以下連結查看圖片:\n"
|
| 71 |
-
f"{image_url}"
|
| 72 |
-
)
|
| 73 |
-
return [TextMessage(text=reply_text)]
|
| 74 |
-
else:
|
| 75 |
-
return [TextMessage(text=result)]
|
| 76 |
|
| 77 |
def get_latest_earthquake_reply() -> list:
|
| 78 |
"""獲取最新地震資訊並組合成 LINE 訊息"""
|
|
@@ -123,7 +110,6 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
| 123 |
command = ""
|
| 124 |
arg = ""
|
| 125 |
|
| 126 |
-
# 檢查是否為數字指令 (e.g., #7 你好)
|
| 127 |
if user_message.startswith('#') and len(user_message) > 1:
|
| 128 |
parts = user_message.split(' ', 1)
|
| 129 |
key = parts[0]
|
|
@@ -131,13 +117,11 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
| 131 |
command = cmd_map[key]
|
| 132 |
arg = parts[1] if len(parts) > 1 else ""
|
| 133 |
|
| 134 |
-
# 檢查是否為斜線指令 (e.g., /ai 你好)
|
| 135 |
elif user_message.startswith('/'):
|
| 136 |
parts = user_message.split(' ', 1)
|
| 137 |
command = parts[0].lower()
|
| 138 |
arg = parts[1] if len(parts) > 1 else ""
|
| 139 |
|
| 140 |
-
# 檢查是否為中文關鍵字指令
|
| 141 |
else:
|
| 142 |
for key, value in cmd_map.items():
|
| 143 |
if user_message.lower() == key:
|
|
@@ -155,16 +139,23 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
| 155 |
elif command == '/taiwan':
|
| 156 |
return [get_taiwan_earthquake_list()]
|
| 157 |
elif command == '/map':
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
elif command == '/alert':
|
| 160 |
return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 161 |
elif command == '/significant':
|
| 162 |
return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 163 |
elif command == '/ai':
|
| 164 |
-
prompt = arg or user_message[2:].lstrip()
|
| 165 |
if not prompt:
|
| 166 |
return [TextMessage(text="請輸入問題,例如:#7 台灣最高的山是哪座?")]
|
| 167 |
return [TextMessage(text=generate_ai_text(prompt))]
|
| 168 |
|
| 169 |
-
# 若非任何指令,則預設交給 AI 處理
|
| 170 |
return [TextMessage(text=generate_ai_text(user_message))]
|
|
|
|
| 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 |
from config import CURRENT_YEAR, HF_SPACE_URL
|
| 11 |
|
|
|
|
| 17 |
"• #1 - 最新顯著地震 (含圖)\n"
|
| 18 |
"• #2 - 全球近24小時顯著地震\n"
|
| 19 |
"• #3 - 今年台灣顯著地震列表\n"
|
| 20 |
+
"• #4 - M≥6.0 強震列表 (外部連結)\n" # [修改] 更新指令說明
|
| 21 |
"• #5 - CWA 最新地震預警\n"
|
| 22 |
"• #6 - CWA 最新顯著有感地震\n\n"
|
| 23 |
"【AI 與工具】\n"
|
|
|
|
| 33 |
text = (
|
| 34 |
"🤖 關於我\n\n"
|
| 35 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
| 36 |
+
"• 版本: 3.1\n"
|
| 37 |
"• 資料來源: CWA, USGS, Hugging Face\n"
|
| 38 |
"• 開發者: dayichen"
|
| 39 |
)
|
|
|
|
| 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 |
+
# [移除] get_taiwan_earthquake_map 函式
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def get_latest_earthquake_reply() -> list:
|
| 65 |
"""獲取最新地震資訊並組合成 LINE 訊息"""
|
|
|
|
| 110 |
command = ""
|
| 111 |
arg = ""
|
| 112 |
|
|
|
|
| 113 |
if user_message.startswith('#') and len(user_message) > 1:
|
| 114 |
parts = user_message.split(' ', 1)
|
| 115 |
key = parts[0]
|
|
|
|
| 117 |
command = cmd_map[key]
|
| 118 |
arg = parts[1] if len(parts) > 1 else ""
|
| 119 |
|
|
|
|
| 120 |
elif user_message.startswith('/'):
|
| 121 |
parts = user_message.split(' ', 1)
|
| 122 |
command = parts[0].lower()
|
| 123 |
arg = parts[1] if len(parts) > 1 else ""
|
| 124 |
|
|
|
|
| 125 |
else:
|
| 126 |
for key, value in cmd_map.items():
|
| 127 |
if user_message.lower() == key:
|
|
|
|
| 139 |
elif command == '/taiwan':
|
| 140 |
return [get_taiwan_earthquake_list()]
|
| 141 |
elif command == '/map':
|
| 142 |
+
# [修改] 回傳新的文字訊息和連結
|
| 143 |
+
reply_text = (
|
| 144 |
+
"🗺️ 外部強震查詢服務\n\n"
|
| 145 |
+
"此服務提供全球規模大於 6.0 的地震列表。\n\n"
|
| 146 |
+
"請點擊以下連結進行查詢:\n"
|
| 147 |
+
"https://huggingface.co/spaces/cwadayi/MCP-2"
|
| 148 |
+
)
|
| 149 |
+
return [TextMessage(text=reply_text)]
|
| 150 |
elif command == '/alert':
|
| 151 |
return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 152 |
elif command == '/significant':
|
| 153 |
return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 154 |
elif command == '/ai':
|
| 155 |
+
prompt = arg or user_message[2:].lstrip()
|
| 156 |
if not prompt:
|
| 157 |
return [TextMessage(text="請輸入問題,例如:#7 台灣最高的山是哪座?")]
|
| 158 |
return [TextMessage(text=generate_ai_text(prompt))]
|
| 159 |
|
| 160 |
+
# 若非任何指令,則預設交給 AI 處理
|
| 161 |
return [TextMessage(text=generate_ai_text(user_message))]
|