Spaces:
Sleeping
Sleeping
Update command_handler.py
Browse files- command_handler.py +69 -11
command_handler.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 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
|
| 7 |
from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
|
| 8 |
from plotting_service import create_and_save_map
|
|
@@ -10,13 +10,34 @@ from ai_service import generate_ai_text
|
|
| 10 |
from weather_service import fetch_forecast_by_location
|
| 11 |
from config import CURRENT_YEAR, HF_SPACE_URL
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return TextMessage(text=text)
|
| 17 |
|
| 18 |
-
def get_info_message():
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return TextMessage(text=text)
|
| 21 |
|
| 22 |
def get_taiwan_earthquake_list() -> TextMessage:
|
|
@@ -25,8 +46,6 @@ def get_taiwan_earthquake_list() -> TextMessage:
|
|
| 25 |
if isinstance(result, pd.DataFrame):
|
| 26 |
count = len(result)
|
| 27 |
lines = [f"🇹🇼 今年 ({CURRENT_YEAR} 年) 台灣區域顯著地震 (M≥5.0),共 {count} 筆:", "-" * 20]
|
| 28 |
-
|
| 29 |
-
# [修改] 從 row 中讀取 'url' 並加入訊息中
|
| 30 |
for _, row in result.head(15).iterrows():
|
| 31 |
t = row["time_utc"].strftime("%Y-%m-%d %H:%M")
|
| 32 |
lines.append(
|
|
@@ -34,7 +53,6 @@ def get_taiwan_earthquake_list() -> TextMessage:
|
|
| 34 |
f"地點: {row['place']}\n"
|
| 35 |
f"報告連結: {row.get('url', '無')}"
|
| 36 |
)
|
| 37 |
-
|
| 38 |
if count > 15:
|
| 39 |
lines.append(f"... (還有 {count - 15} 筆,請用 /map 查看全部)")
|
| 40 |
reply_text = "\n\n".join(lines)
|
|
@@ -55,7 +73,47 @@ def get_taiwan_earthquake_map(base_url: str) -> list:
|
|
| 55 |
else:
|
| 56 |
return [TextMessage(text=result)]
|
| 57 |
|
| 58 |
-
# ... (process_message 函式不變) ...
|
| 59 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
return [TextMessage(text=generate_ai_text(user_message))]
|
|
|
|
| 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
|
| 7 |
from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
|
| 8 |
from plotting_service import create_and_save_map
|
|
|
|
| 10 |
from weather_service import fetch_forecast_by_location
|
| 11 |
from config import CURRENT_YEAR, HF_SPACE_URL
|
| 12 |
|
| 13 |
+
def get_help_message() -> TextMessage:
|
| 14 |
+
"""回傳包含所有可用指令的說明訊息。"""
|
| 15 |
+
text = (
|
| 16 |
+
"📖 指令列表\n\n"
|
| 17 |
+
"【地震資訊】\n"
|
| 18 |
+
"• /global - 查詢全球近24小時顯著地震\n"
|
| 19 |
+
"• /taiwan - 顯示今年台灣顯著地震列表\n"
|
| 20 |
+
"• /map - 繪製台灣顯著地震地圖\n"
|
| 21 |
+
"• /alert - 獲取 CWA 最新地震預警\n"
|
| 22 |
+
"• /significant - CWA 最新顯著有感地震\n\n"
|
| 23 |
+
"【AI 與工具】\n"
|
| 24 |
+
"• /ai <問題> - 與 AI 助理對話\n"
|
| 25 |
+
"• /weather <地點> - 查詢天氣預報\n\n"
|
| 26 |
+
"【基本指令】\n"
|
| 27 |
+
"• /info - 關於此機器人\n"
|
| 28 |
+
"• /help - 顯示此說明"
|
| 29 |
+
)
|
| 30 |
return TextMessage(text=text)
|
| 31 |
|
| 32 |
+
def get_info_message() -> TextMessage:
|
| 33 |
+
"""回傳機器人資訊。"""
|
| 34 |
+
text = (
|
| 35 |
+
"🤖 關於我\n\n"
|
| 36 |
+
"我是一個多功能助理機器人,提供地震查詢、天氣資訊與 AI 對話功能。\n\n"
|
| 37 |
+
"• 版本: 2.1\n"
|
| 38 |
+
"• 資料來源: CWA, USGS, Hugging Face\n"
|
| 39 |
+
"• 開發者: dayichen"
|
| 40 |
+
)
|
| 41 |
return TextMessage(text=text)
|
| 42 |
|
| 43 |
def get_taiwan_earthquake_list() -> TextMessage:
|
|
|
|
| 46 |
if isinstance(result, pd.DataFrame):
|
| 47 |
count = len(result)
|
| 48 |
lines = [f"🇹🇼 今年 ({CURRENT_YEAR} 年) 台灣區域顯著地震 (M≥5.0),共 {count} 筆:", "-" * 20]
|
|
|
|
|
|
|
| 49 |
for _, row in result.head(15).iterrows():
|
| 50 |
t = row["time_utc"].strftime("%Y-%m-%d %H:%M")
|
| 51 |
lines.append(
|
|
|
|
| 53 |
f"地點: {row['place']}\n"
|
| 54 |
f"報告連結: {row.get('url', '無')}"
|
| 55 |
)
|
|
|
|
| 56 |
if count > 15:
|
| 57 |
lines.append(f"... (還有 {count - 15} 筆,請用 /map 查看全部)")
|
| 58 |
reply_text = "\n\n".join(lines)
|
|
|
|
| 73 |
else:
|
| 74 |
return [TextMessage(text=result)]
|
| 75 |
|
|
|
|
| 76 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 77 |
+
"""處理使用者的文字訊息並回傳一個包含回覆訊息的列表。"""
|
| 78 |
+
user_message = (user_message_raw or "").strip()
|
| 79 |
+
|
| 80 |
+
# 將舊指令對應到新指令,提供向下相容
|
| 81 |
+
if user_message.lower() in ["地震", "quake"]:
|
| 82 |
+
user_message = "/global"
|
| 83 |
+
elif user_message.lower() in ["台灣地震", "臺灣地震"]:
|
| 84 |
+
user_message = "/taiwan"
|
| 85 |
+
elif user_message.lower() in ["台灣地震畫圖", "臺灣地震畫圖"]:
|
| 86 |
+
user_message = "/map"
|
| 87 |
+
elif user_message.lower() == "地震預警":
|
| 88 |
+
user_message = "/alert"
|
| 89 |
+
|
| 90 |
+
if user_message.startswith('/'):
|
| 91 |
+
parts = user_message.split(' ', 1)
|
| 92 |
+
command = parts[0].lower()
|
| 93 |
+
arg = parts[1] if len(parts) > 1 else ""
|
| 94 |
+
|
| 95 |
+
if command == '/help':
|
| 96 |
+
return [get_help_message()]
|
| 97 |
+
elif command == '/info':
|
| 98 |
+
return [get_info_message()]
|
| 99 |
+
elif command == '/global':
|
| 100 |
+
return [TextMessage(text=fetch_global_last24h_text())]
|
| 101 |
+
elif command == '/taiwan':
|
| 102 |
+
return [get_taiwan_earthquake_list()]
|
| 103 |
+
elif command == '/map':
|
| 104 |
+
return get_taiwan_earthquake_map(request_base_url)
|
| 105 |
+
elif command == '/alert':
|
| 106 |
+
return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 107 |
+
elif command == '/significant':
|
| 108 |
+
return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 109 |
+
elif command == '/weather':
|
| 110 |
+
if not arg:
|
| 111 |
+
return [TextMessage(text="請輸入地點,例如:/weather 台北")]
|
| 112 |
+
return [TextMessage(text=fetch_forecast_by_location(arg))]
|
| 113 |
+
elif command == '/ai':
|
| 114 |
+
if not arg:
|
| 115 |
+
return [TextMessage(text="請輸入問題,例如:/ai 台灣最高的山是哪座?")]
|
| 116 |
+
return [TextMessage(text=generate_ai_text(arg))]
|
| 117 |
+
|
| 118 |
+
# 若非指令,則預設交給 AI 處理 (作為備援)
|
| 119 |
return [TextMessage(text=generate_ai_text(user_message))]
|