cwadayi commited on
Commit
5da9c98
·
verified ·
1 Parent(s): 2fdc860

Update command_handler.py

Browse files
Files changed (1) hide show
  1. command_handler.py +25 -53
command_handler.py CHANGED
@@ -1,4 +1,4 @@
1
- # command_handler.py (已整合 PWS 查詢功能)
2
  import pandas as pd
3
  from linebot.v3.messaging import TextMessage, ImageMessage
4
 
@@ -7,13 +7,13 @@ 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
- 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"
18
  "• 1 - 最新一筆顯著地震 (含圖)\n"
19
  "• 2 - 全球近24小時顯著地震(USGS)\n"
@@ -22,12 +22,11 @@ def get_help_message() -> TextMessage:
22
  "• 5 - CWA 最新地震預警\n"
23
  "• 6 - CWA 最近7天顯著有感地震\n\n"
24
  "【AI 與工具】\n"
25
- "• 7 <問題> - 與 AI 助理對話\n\n"
 
26
  "【基本指令】\n"
27
  "• 8 - 關於此機器人\n"
28
- "• 9 - 顯示此說明\n\n"
29
- "【天氣與警報】\n"
30
- "• 10 - 最新 PWS 發布情形\n\n"
31
  )
32
  return TextMessage(text=text)
33
 
@@ -36,7 +35,7 @@ def get_info_message() -> TextMessage:
36
  text = (
37
  "🤖 關於我\n\n"
38
  "我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
39
- "• 版本: 5.0 (Gemini Edition) 搭配搜尋地震目錄的MCP功能(可用資料 : 1973-01-01 至 2025-07-06)\n"
40
  "• 資料來源: CWA, USGS, Google Gemini\n"
41
  "• 開發者: dayichen"
42
  )
@@ -95,54 +94,27 @@ def get_latest_earthquake_reply() -> list:
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',
111
- '地震預警': '/alert',
112
- }
113
-
114
- command = ""
115
- arg = ""
116
-
117
- # 解析使用者輸入以分離指令和參數
118
  parts = user_message.split(' ', 1)
119
- cmd_key = parts[0].lower()
120
-
121
- if cmd_key in cmd_map:
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))]
 
1
+ # command_handler.py (已簡化為僅支援數字指令)
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
+ # (pws_service 的匯入已被移除)
11
  from config import CURRENT_YEAR, MCP_SERVER_URL
12
 
13
  def get_help_message() -> TextMessage:
14
  """產生並回傳包含所有指令的說明文字。"""
15
  text = (
16
+ "📖 指令列表 (請直接輸入數字)\n\n"
17
  "【地震資訊】\n"
18
  "• 1 - 最新一筆顯著地震 (含圖)\n"
19
  "• 2 - 全球近24小時顯著地震(USGS)\n"
 
22
  "• 5 - CWA 最新地震預警\n"
23
  "• 6 - CWA 最近7天顯著有感地震\n\n"
24
  "【AI 與工具】\n"
25
+ "• 7 <問題> - 與 AI 助理對話\n"
26
+ " (範例: 7 昨天台北有地震嗎?)\n\n"
27
  "【基本指令】\n"
28
  "• 8 - 關於此機器人\n"
29
+ "• 9 - 顯示此說明"
 
 
30
  )
31
  return TextMessage(text=text)
32
 
 
35
  text = (
36
  "🤖 關於我\n\n"
37
  "我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
38
+ "• 版本: 5.1 (Gemini Simplified Edition)\n"
39
  "• 資料來源: CWA, USGS, Google Gemini\n"
40
  "• 開發者: dayichen"
41
  )
 
94
  def process_message(user_message_raw: str, request_base_url: str) -> list:
95
  """
96
  處理使用者輸入的主函式。
97
+ 僅解析數字指令,若非指令則交由 AI 處理。
98
  """
99
  user_message = (user_message_raw or "").strip()
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  parts = user_message.split(' ', 1)
102
+ command_key = parts[0]
103
+ arg = parts[1].strip() if len(parts) > 1 else ""
 
 
 
 
 
 
 
 
 
104
 
105
+ # 根據數字指令執行對應的動作
106
+ if command_key == '1': return get_latest_earthquake_reply()
107
+ if command_key == '2': return [TextMessage(text=fetch_global_last24h_text())]
108
+ if command_key == '3': return [get_taiwan_earthquake_list()]
109
+ if command_key == '4': return [TextMessage(text=f"🗺️ 外部地震查���服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]
110
+ if command_key == '5': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
111
+ if command_key == '6': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
112
+ if command_key == '7':
113
+ prompt = arg
114
+ if not prompt: return [TextMessage(text="請輸入問題,例如:7 台灣最高的山是哪座?")]
115
+ return [TextMessage(text=generate_ai_text(prompt))]
116
+ if command_key == '8': return [get_info_message()]
117
+ if command_key == '9': return [get_help_message()]
118
+
119
+ # 如果輸入的不是 1-9 的指令,則將完整訊息交由 AI 處理
 
 
120
  return [TextMessage(text=generate_ai_text(user_message))]