Spaces:
Sleeping
Sleeping
Update command_handler.py
Browse files- command_handler.py +16 -71
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,34 +10,13 @@ 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 |
-
"📖 指令列表\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()
|
| 33 |
-
|
| 34 |
-
text = (
|
| 35 |
-
"🤖 關於我\n\n"
|
| 36 |
-
"我是一個多功能助理機器人,提供地震查詢、天氣資訊與 AI 對話功能。\n\n"
|
| 37 |
-
"• 版本: 2.0\n"
|
| 38 |
-
"• 資料來源: CWA, USGS, Hugging Face\n"
|
| 39 |
-
"• 開發者: dayichen"
|
| 40 |
-
)
|
| 41 |
return TextMessage(text=text)
|
| 42 |
|
| 43 |
def get_taiwan_earthquake_list() -> TextMessage:
|
|
@@ -46,9 +25,16 @@ 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if count > 15:
|
| 53 |
lines.append(f"... (還有 {count - 15} 筆,請用 /map 查看全部)")
|
| 54 |
reply_text = "\n\n".join(lines)
|
|
@@ -69,48 +55,7 @@ def get_taiwan_earthquake_map(base_url: str) -> list:
|
|
| 69 |
else:
|
| 70 |
return [TextMessage(text=result)]
|
| 71 |
|
|
|
|
| 72 |
def process_message(user_message_raw: str, request_base_url: str) -> list:
|
| 73 |
-
|
| 74 |
-
# [修正] 加上這一行來定義 user_message 變數
|
| 75 |
-
user_message = (user_message_raw or "").strip()
|
| 76 |
-
|
| 77 |
-
# 將舊指令對應到新指令,提供向下相容
|
| 78 |
-
if user_message.lower() in ["地震", "quake"]:
|
| 79 |
-
user_message = "/global"
|
| 80 |
-
elif user_message.lower() in ["台灣地震", "臺灣地震"]:
|
| 81 |
-
user_message = "/taiwan"
|
| 82 |
-
elif user_message.lower() in ["台灣地震畫圖", "臺灣地震畫圖"]:
|
| 83 |
-
user_message = "/map"
|
| 84 |
-
elif user_message.lower() == "地震預警":
|
| 85 |
-
user_message = "/alert"
|
| 86 |
-
|
| 87 |
-
if user_message.startswith('/'):
|
| 88 |
-
parts = user_message.split(' ', 1)
|
| 89 |
-
command = parts[0].lower()
|
| 90 |
-
arg = parts[1] if len(parts) > 1 else ""
|
| 91 |
-
|
| 92 |
-
if command == '/help':
|
| 93 |
-
return [get_help_message()]
|
| 94 |
-
elif command == '/info':
|
| 95 |
-
return [get_info_message()]
|
| 96 |
-
elif command == '/global':
|
| 97 |
-
return [TextMessage(text=fetch_global_last24h_text())]
|
| 98 |
-
elif command == '/taiwan':
|
| 99 |
-
return [get_taiwan_earthquake_list()]
|
| 100 |
-
elif command == '/map':
|
| 101 |
-
return get_taiwan_earthquake_map(request_base_url)
|
| 102 |
-
elif command == '/alert':
|
| 103 |
-
return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
|
| 104 |
-
elif command == '/significant':
|
| 105 |
-
return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
|
| 106 |
-
elif command == '/weather':
|
| 107 |
-
if not arg:
|
| 108 |
-
return [TextMessage(text="請輸入地點,例如:/weather 台北")]
|
| 109 |
-
return [TextMessage(text=fetch_forecast_by_location(arg))]
|
| 110 |
-
elif command == '/ai':
|
| 111 |
-
if not arg:
|
| 112 |
-
return [TextMessage(text="請輸入問題,例如:/ai 台灣最高的山是哪座?")]
|
| 113 |
-
return [TextMessage(text=generate_ai_text(arg))]
|
| 114 |
-
|
| 115 |
-
# 若非指令,則預設交給 AI 處理 (作為備援)
|
| 116 |
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 |
+
# ... (get_help_message, get_info_message 函式不變) ...
|
| 14 |
+
def get_help_message():
|
| 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 |
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(
|
| 33 |
+
f"震級: {row['magnitude']:.1f} | 日期時間: {t} (UTC)\n"
|
| 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 |
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))]
|