Spaces:
Sleeping
Sleeping
chen commited on
Update command_handler.py
Browse files12342 unmodified lines789101112131415161718192 unmodified lines2223242526272829303132332 unmodified lines3637383940414252 unmodified lines9596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148# command_handler.py (已整合 PWS 查詢功能)import pandas as pdfrom linebot.v3.messaging import TextMessage, ImageMessage2 unmodified linesfrom usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_yearfrom plotting_service import create_and_save_mapfrom ai_service import generate_ai_textfrom pws_service import fetch_latest_pws_info # 匯入新的 PWS 服務from config import CURRENT_YEAR, MCP_SERVER_URLdef get_help_message() -> TextMessage:"""產生並回傳包含所有指令的說明文字。"""text = ("📖 指令列表 (輸入數字即可)\n\n""【地震資訊】\n""• 1 - 最新一筆顯著地震 (含圖)\n""• 2 - 全球近24小時顯著地震(USGS)\n"2 unmodified lines"• 5 - CWA 最新地震預警\n""• 6 - CWA 最近7天顯著有感地震\n\n""【AI 與工具】\n""• 7 <問題> - 與 AI 助理對話\n\n""【基本指令】\n""• 8 - 關於此機器人\n""• 9 - 顯示此說明\n\n""【天氣與警報】\n""• 10 - 最新 PWS 發布情形\n\n")return TextMessage(text=text)2 unmodified linestext = ("🤖 關於我\n\n""我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n""• 版本: 5.0 (Gemini Edition) 搭配搜尋地震目錄的MCP功能(可用資料 : 1973-01-01 至 2025-07-06)\n""• 資料來源: CWA, USGS, Google Gemini\n""• 開發者: dayichen")52 unmodified linesdef process_message(user_message_raw: str, request_base_url: str) -> list:"""處理使用者輸入的主函式。解析訊息、對應指令並呼叫相應的服務函式。"""user_message = (user_message_raw or "").strip()# 指令與快捷鍵的映射字典cmd_map = {'1': '/latest', '2': '/global', '3': '/taiwan','4': '/map', '5': '/alert', '6': '/significant','7': '/ai', '8': '/info', '9': '/help','10': '/pws', # 新增 PWS 指令'地震': '/global', 'quake': '/global', '幫助': '/help','台灣地震': '/taiwan', '臺灣地震': '/taiwan','台灣地震畫圖': '/map', '臺灣地震畫圖': '/map','地震預警': '/alert',}command = ""arg = ""# 解析使用者輸入以分離指令和參數parts = user_message.split(' ', 1)cmd_key = parts[0].lower()if cmd_key in cmd_map:command = cmd_map[cmd_key]if len(parts) > 1:arg = parts[1].strip()# 允許使用者仍然可以使用 /command 格式elif user_message.startswith('/') and cmd_key in cmd_map.values():command = cmd_keyif len(parts) > 1:arg = parts[1].strip()# 根據解析出的指令執行對應的動作if command:if command == '/help': return [get_help_message()]if command == '/info': return [get_info_message()]if command == '/latest': return get_latest_earthquake_reply()if command == '/global': return [TextMessage(text=fetch_global_last24h_text())]if command == '/taiwan': return [get_taiwan_earthquake_list()]if command == '/map': return [TextMessage(text=f"🗺️ 外部地震查詢服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]if command == '/alert': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]if command == '/significant': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]if command == '/pws': return [TextMessage(text=fetch_latest_pws_info())] # 新增 PWS 處理邏輯if command == '/ai':prompt = argif not prompt: return [TextMessage(text="請輸入問題,例如:7 台灣最高的山是哪座?")]return [TextMessage(text=generate_ai_text(prompt))]# 如果輸入的不是已知指令,則交由 AI 處理return [TextMessage(text=generate_ai_text(user_message))]12342 unmodified lines789101112131415161718192 unmodified lines22232425262728293031322 unmodified lines3536373839404152 unmodified lines949596979899100101102103104105106107108109110111112113114115116117118119120# command_handler.py (已簡化為僅支援數字指令)import pandas as pdfrom linebot.v3.messaging import TextMessage, ImageMessage2 unmodified linesfrom usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_yearfrom plotting_service import create_and_save_mapfrom ai_service import generate_ai_text# (pws_service 的匯入已被移除)from config import CURRENT_YEAR, MCP_SERVER_URLdef get_help_message() -> TextMessage:"""產生並回傳包含所有指令的說明文字。"""text = ("📖 指令列表 (請直接輸入數字)\n\n""【地震資訊】\n""• 1 - 最新一筆顯著地震 (含圖)\n""• 2 - 全球近24小時顯著地震(USGS)\n"2 unmodified lines"• 5 - CWA 最新地震預警\n""• 6 - CWA 最近7天顯著有感地震\n\n""【AI 與工具】\n""• 7 <問題> - 與 AI 助理對話\n"" (範例: 7 昨天台北有地震嗎?)\n\n""【基本指令】\n""• 8 - 關於此機器人\n""• 9 - 顯示此說明")return TextMessage(text=text)2 unmodified linestext = ("🤖 關於我\n\n""我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n""• 版本: 5.1 (Gemini Simplified Edition)\n""• 資料來源: CWA, USGS, Google Gemini\n""• 開發者: dayichen")52 unmodified linesdef process_message(user_message_raw: str, request_base_url: str) -> list:"""處理使用者輸入的主函式。僅解析數字指令,若非指令則交由 AI 處理。"""user_message = (user_message_raw or "").strip()parts = user_message.split(' ', 1)command_key = parts[0]arg = parts[1].strip() if len(parts) > 1 else ""# 根據數字指令執行對應的動作if command_key == '1': return get_latest_earthquake_reply()if command_key == '2': return [TextMessage(text=fetch_global_last24h_text())]if command_key == '3': return [get_taiwan_earthquake_list()]if command_key == '4': return [TextMessage(text=f"🗺️ 外部地震查���服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]if command_key == '5': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]if command_key == '6': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]if command_key == '7':prompt = argif not prompt: return [TextMessage(text="請輸入問題,例如:7 台灣最高的山是哪座?")]return [TextMessage(text=generate_ai_text(prompt))]if command_key == '8': return [get_info_message()]if command_key == '9': return [get_help_message()]# 如果輸入的不是 1-9 的指令,則將完整訊息交由 AI 處理return [TextMessage(text=generate_ai_text(user_message))]
command_handler.py+25-53