Spaces:
Sleeping
Sleeping
chen commited on
Update command_handler.py
Browse files1 unmodified line23456781 unmodified line101112131415161718192021222324252627282930313233343536373839404142432 unmodified lines46474849505152535414 unmodified lines697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161 unmodified lineimport pandas as pdfrom linebot.v3.messaging import TextMessage, ImageMessage# 匯入服務函式from cwa_service import fetch_cwa_alarm_list, fetch_significant_earthquakesfrom usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_yearfrom plotting_service import create_and_save_map1 unmodified linefrom weather_service import fetch_forecast_by_locationfrom config import CURRENT_YEAR, HF_SPACE_URLdef get_help_message() -> TextMessage:"""回傳包含所有可用指令的說明訊息。"""text = ("📖 指令列表\n\n""【地震資訊】\n""• /global - 查詢全球近24小時顯著地震\n""• /taiwan - 顯示今年台灣顯著地震列表\n""• /map - 繪製台灣顯著地震地圖\n""• /alert - 獲取 CWA 最新地震預警\n""• /significant - CWA 最新顯著有感地震\n\n""【AI 與工具】\n""• /ai <問題> - 與 AI 助理對話\n""• /weather <地點> - 查詢天氣預報\n\n""【基本指令】\n""• /info - 關於此機器人\n""• /help - 顯示此說明")return TextMessage(text=text)def get_info_message() -> TextMessage:"""回傳機器人資訊。"""text = ("🤖 關於我\n\n""我是一個多功能助理機器人,提供地震查詢、天氣資訊與 AI 對話功能。\n\n""• 版本: 2.0\n""• 資料來源: CWA, USGS, Hugging Face\n""• 開發者: dayichen")return TextMessage(text=text)def get_taiwan_earthquake_list() -> TextMessage:2 unmodified linesif isinstance(result, pd.DataFrame):count = len(result)lines = [f"🇹🇼 今年 ({CURRENT_YEAR} 年) 台灣區域顯著地震 (M≥5.0),共 {count} 筆:", "-" * 20]for _, row in result.head(15).iterrows():t = row["time_utc"].strftime("%Y-%m-%d %H:%M")lines.append(f"震級: {row['magnitude']:.1f} | 日期時間: {t} (UTC)\n地點: {row['place']}")if count > 15:lines.append(f"... (還有 {count - 15} 筆,請用 /map 查看全部)")reply_text = "\n\n".join(lines)14 unmodified lineselse:return [TextMessage(text=result)]def process_message(user_message_raw: str, request_base_url: str) -> list:"""處理使用者的文字訊息並回傳一個包含回覆訊息的列表。"""# [修正] 加上這一行來定義 user_message 變數user_message = (user_message_raw or "").strip()# 將舊指令對應到新指令,提供向下相容if user_message.lower() in ["地震", "quake"]:user_message = "/global"elif user_message.lower() in ["台灣地震", "臺灣地震"]:user_message = "/taiwan"elif user_message.lower() in ["台灣地震畫圖", "臺灣地震畫圖"]:user_message = "/map"elif user_message.lower() == "地震預警":user_message = "/alert"if user_message.startswith('/'):parts = user_message.split(' ', 1)command = parts[0].lower()arg = parts[1] if len(parts) > 1 else ""if command == '/help':return [get_help_message()]elif command == '/info':return [get_info_message()]elif command == '/global':return [TextMessage(text=fetch_global_last24h_text())]elif command == '/taiwan':return [get_taiwan_earthquake_list()]elif command == '/map':return get_taiwan_earthquake_map(request_base_url)elif command == '/alert':return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]elif command == '/significant':return [TextMessage(text=fetch_significant_earthquakes(limit=5))]elif command == '/weather':if not arg:return [TextMessage(text="請輸入地點,例如:/weather 台北")]return [TextMessage(text=fetch_forecast_by_location(arg))]elif command == '/ai':if not arg:return [TextMessage(text="請輸入問題,例如:/ai 台灣最高的山是哪座?")]return [TextMessage(text=generate_ai_text(arg))]# 若非指令,則預設交給 AI 處理 (作為備援)return [TextMessage(text=generate_ai_text(user_message))]1 unmodified line23456781 unmodified line101112131415161718192021222 unmodified lines2526272829303132333435363738394014 unmodified lines555657585960611 unmodified lineimport pandas as pdfrom linebot.v3.messaging import TextMessage, ImageMessage# ... (匯入服務函式部分不變) ...from cwa_service import fetch_cwa_alarm_list, fetch_significant_earthquakesfrom usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_yearfrom plotting_service import create_and_save_map1 unmodified linefrom weather_service import fetch_forecast_by_locationfrom config import CURRENT_YEAR, HF_SPACE_URL# ... (get_help_message, get_info_message 函式不變) ...def get_help_message():#...return TextMessage(text=text)def get_info_message():#...return TextMessage(text=text)def get_taiwan_earthquake_list() -> TextMessage:2 unmodified linesif isinstance(result, pd.DataFrame):count = len(result)lines = [f"🇹🇼 今年 ({CURRENT_YEAR} 年) 台灣區域顯著地震 (M≥5.0),共 {count} 筆:", "-" * 20]# [修改] 從 row 中讀取 'url' 並加入訊息中for _, row in result.head(15).iterrows():t = row["time_utc"].strftime("%Y-%m-%d %H:%M")lines.append(f"震級: {row['magnitude']:.1f} | 日期時間: {t} (UTC)\n"f"地點: {row['place']}\n"f"報告連結: {row.get('url', '無')}")if count > 15:lines.append(f"... (還有 {count - 15} 筆,請用 /map 查看全部)")reply_text = "\n\n".join(lines)14 unmodified lineselse:return [TextMessage(text=result)]# ... (process_message 函式不變) ...def process_message(user_message_raw: str, request_base_url: str) -> list:# ...return [TextMessage(text=generate_ai_text(user_message))]
command_handler.py+16-71