Spaces:
Sleeping
Sleeping
Update command_handler.py
Browse files- command_handler.py +21 -6
command_handler.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# command_handler.py (
|
| 2 |
import pandas as pd
|
| 3 |
from linebot.v3.messaging import TextMessage, ImageMessage
|
| 4 |
|
|
@@ -7,10 +7,11 @@ from cwa_service import fetch_cwa_alarm_list, fetch_significant_earthquakes, fet
|
|
| 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 |
from config import CURRENT_YEAR, MCP_SERVER_URL
|
| 12 |
|
| 13 |
def get_help_message() -> TextMessage:
|
|
|
|
| 14 |
text = (
|
| 15 |
"📖 指令列表 (輸入數字即可)\n\n"
|
| 16 |
"【地震資訊】\n"
|
|
@@ -24,11 +25,14 @@ def get_help_message() -> TextMessage:
|
|
| 24 |
"• 7 <問題> - 與 AI 助理對話\n\n"
|
| 25 |
"【基本指令】\n"
|
| 26 |
"• 8 - 關於此機器人\n"
|
| 27 |
-
"• 9 -
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
return TextMessage(text=text)
|
| 30 |
|
| 31 |
def get_info_message() -> TextMessage:
|
|
|
|
| 32 |
text = (
|
| 33 |
"🤖 關於我\n\n"
|
| 34 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
|
@@ -39,6 +43,7 @@ def get_info_message() -> TextMessage:
|
|
| 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)
|
|
@@ -58,6 +63,7 @@ def get_taiwan_earthquake_list() -> TextMessage:
|
|
| 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,12 +93,18 @@ def get_latest_earthquake_reply() -> list:
|
|
| 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',
|
|
@@ -102,6 +114,7 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
| 102 |
command = ""
|
| 103 |
arg = ""
|
| 104 |
|
|
|
|
| 105 |
parts = user_message.split(' ', 1)
|
| 106 |
cmd_key = parts[0].lower()
|
| 107 |
|
|
@@ -109,25 +122,27 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
|
|
| 109 |
command = cmd_map[cmd_key]
|
| 110 |
if len(parts) > 1:
|
| 111 |
arg = parts[1].strip()
|
| 112 |
-
#
|
| 113 |
elif user_message.startswith('/') and cmd_key in cmd_map.values():
|
| 114 |
command = cmd_key
|
| 115 |
if len(parts) > 1:
|
| 116 |
arg = parts[1].strip()
|
| 117 |
|
|
|
|
| 118 |
if command:
|
| 119 |
if command == '/help': return [get_help_message()]
|
| 120 |
if command == '/info': return [get_info_message()]
|
| 121 |
if command == '/latest': return get_latest_earthquake_reply()
|
| 122 |
if command == '/global': return [TextMessage(text=fetch_global_last24h_text())]
|
| 123 |
if command == '/taiwan': return [get_taiwan_earthquake_list()]
|
| 124 |
-
# [修正] 將 HF_SPACE_URL 改為 MCP_SERVER_URL
|
| 125 |
if command == '/map': return [TextMessage(text=f"🗺️ 外部地震查詢服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]
|
| 126 |
if command == '/alert': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 127 |
if command == '/significant': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
|
|
|
| 128 |
if command == '/ai':
|
| 129 |
prompt = arg
|
| 130 |
if not prompt: return [TextMessage(text="請輸入問題,例如:7 台灣最高的山是哪座?")]
|
| 131 |
return [TextMessage(text=generate_ai_text(prompt))]
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
| 1 |
+
# command_handler.py (已整合 PWS 查詢功能)
|
| 2 |
import pandas as pd
|
| 3 |
from linebot.v3.messaging import TextMessage, ImageMessage
|
| 4 |
|
|
|
|
| 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 pws_service import fetch_latest_pws_info # 匯入新的 PWS 服務
|
| 11 |
from config import CURRENT_YEAR, MCP_SERVER_URL
|
| 12 |
|
| 13 |
def get_help_message() -> TextMessage:
|
| 14 |
+
"""產生並回傳包含所有指令的說明文字。"""
|
| 15 |
text = (
|
| 16 |
"📖 指令列表 (輸入數字即可)\n\n"
|
| 17 |
"【地震資訊】\n"
|
|
|
|
| 25 |
"• 7 <問題> - 與 AI 助理對話\n\n"
|
| 26 |
"【基本指令】\n"
|
| 27 |
"• 8 - 關於此機器人\n"
|
| 28 |
+
"• 9 - 顯示此說明\n"
|
| 29 |
+
"【天氣與警報】\n"
|
| 30 |
+
"• 10 - 最新 PWS 發布情形\n\n"
|
| 31 |
)
|
| 32 |
return TextMessage(text=text)
|
| 33 |
|
| 34 |
def get_info_message() -> TextMessage:
|
| 35 |
+
"""產生並回傳關於此機器人的資訊。"""
|
| 36 |
text = (
|
| 37 |
"🤖 關於我\n\n"
|
| 38 |
"我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
|
|
|
|
| 43 |
return TextMessage(text=text)
|
| 44 |
|
| 45 |
def get_taiwan_earthquake_list() -> TextMessage:
|
| 46 |
+
"""從 USGS 取得今年台灣的地震列表並格式化回覆。"""
|
| 47 |
result = fetch_taiwan_df_this_year()
|
| 48 |
if isinstance(result, pd.DataFrame):
|
| 49 |
count = len(result)
|
|
|
|
| 63 |
return TextMessage(text=reply_text)
|
| 64 |
|
| 65 |
def get_latest_earthquake_reply() -> list:
|
| 66 |
+
"""取得 CWA 最新一筆地震,並組合文字與圖片訊息。"""
|
| 67 |
try:
|
| 68 |
latest_eq = fetch_latest_significant_earthquake()
|
| 69 |
if not latest_eq:
|
|
|
|
| 93 |
return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
|
| 94 |
|
| 95 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 96 |
+
"""
|
| 97 |
+
處理使用者輸入的主函式。
|
| 98 |
+
解析訊息、對應指令並呼叫相應的服務函式。
|
| 99 |
+
"""
|
| 100 |
user_message = (user_message_raw or "").strip()
|
| 101 |
|
| 102 |
+
# 指令與快捷鍵的映射字典
|
| 103 |
cmd_map = {
|
| 104 |
'1': '/latest', '2': '/global', '3': '/taiwan',
|
| 105 |
'4': '/map', '5': '/alert', '6': '/significant',
|
| 106 |
'7': '/ai', '8': '/info', '9': '/help',
|
| 107 |
+
'10': '/pws', # 新增 PWS 指令
|
| 108 |
'地震': '/global', 'quake': '/global', '幫助': '/help',
|
| 109 |
'台灣地震': '/taiwan', '臺灣地震': '/taiwan',
|
| 110 |
'台灣地震畫圖': '/map', '臺灣地震畫圖': '/map',
|
|
|
|
| 114 |
command = ""
|
| 115 |
arg = ""
|
| 116 |
|
| 117 |
+
# 解析使用者輸入以分離指令和參數
|
| 118 |
parts = user_message.split(' ', 1)
|
| 119 |
cmd_key = parts[0].lower()
|
| 120 |
|
|
|
|
| 122 |
command = cmd_map[cmd_key]
|
| 123 |
if len(parts) > 1:
|
| 124 |
arg = parts[1].strip()
|
| 125 |
+
# 允許使用者仍然可以使用 /command 格式
|
| 126 |
elif user_message.startswith('/') and cmd_key in cmd_map.values():
|
| 127 |
command = cmd_key
|
| 128 |
if len(parts) > 1:
|
| 129 |
arg = parts[1].strip()
|
| 130 |
|
| 131 |
+
# 根據解析出的指令執行對應的動作
|
| 132 |
if command:
|
| 133 |
if command == '/help': return [get_help_message()]
|
| 134 |
if command == '/info': return [get_info_message()]
|
| 135 |
if command == '/latest': return get_latest_earthquake_reply()
|
| 136 |
if command == '/global': return [TextMessage(text=fetch_global_last24h_text())]
|
| 137 |
if command == '/taiwan': return [get_taiwan_earthquake_list()]
|
|
|
|
| 138 |
if command == '/map': return [TextMessage(text=f"🗺️ 外部地震查詢服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]
|
| 139 |
if command == '/alert': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 140 |
if command == '/significant': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 141 |
+
if command == '/pws': return [TextMessage(text=fetch_latest_pws_info())] # 新增 PWS 處理邏輯
|
| 142 |
if command == '/ai':
|
| 143 |
prompt = arg
|
| 144 |
if not prompt: return [TextMessage(text="請輸入問題,例如:7 台灣最高的山是哪座?")]
|
| 145 |
return [TextMessage(text=generate_ai_text(prompt))]
|
| 146 |
|
| 147 |
+
# 如果輸入的不是已知指令,則交由 AI 處理
|
| 148 |
+
return [TextMessage(text=generate_ai_text(user_message))]
|