cwadayi commited on
Commit
b0ea870
·
verified ·
1 Parent(s): b640204

Update command_handler.py

Browse files
Files changed (1) hide show
  1. command_handler.py +40 -34
command_handler.py CHANGED
@@ -1,13 +1,12 @@
1
- # command_handler.py (已簡化為僅支援數字指令)
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, fetch_latest_significant_earthquake
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:
@@ -23,10 +22,8 @@ def get_help_message() -> TextMessage:
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,7 +32,7 @@ def get_info_message() -> TextMessage:
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
  )
@@ -54,23 +51,18 @@ def get_taiwan_earthquake_list() -> TextMessage:
54
  f"地點: {row['place']}\n"
55
  f"報告連結: {row.get('url', '無')}"
56
  )
57
- if count > 15:
58
- lines.append(f"... (還有 {count-15} 筆資料)")
59
  reply_text = "\n\n".join(lines)
60
- else:
61
- reply_text = result
62
  return TextMessage(text=reply_text)
63
 
64
  def get_latest_earthquake_reply() -> list:
65
  """取得 CWA 最新一筆地震,並組合文字與圖片訊息。"""
66
  try:
67
  latest_eq = fetch_latest_significant_earthquake()
68
- if not latest_eq:
69
- return [TextMessage(text="✅ 近期無顯著有感地震報告。")]
70
-
71
  mag_str = f"{latest_eq['Magnitude']:.1f}" if latest_eq.get('Magnitude') is not None else "—"
72
  depth_str = f"{latest_eq['Depth']:.0f}" if latest_eq.get('Depth') is not None else "—"
73
-
74
  text_message_content = (
75
  f"🚨 CWA 最新顯著有感地震\n"
76
  f"----------------------------------\n"
@@ -80,41 +72,55 @@ def get_latest_earthquake_reply() -> list:
80
  f"報告: {latest_eq.get('URL', '無')}"
81
  )
82
  reply_messages = [TextMessage(text=text_message_content)]
83
-
84
  if latest_eq.get("ImageURL"):
85
  image_url = latest_eq["ImageURL"]
86
- reply_messages.append(
87
- ImageMessage(original_content_url=image_url, preview_image_url=image_url)
88
- )
89
-
90
  return reply_messages
91
- except Exception as e:
92
- return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
93
 
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))]
 
 
 
 
 
 
 
 
 
 
1
+ # command_handler.py (Added keyword pre-processing for AI queries)
2
  import pandas as pd
3
+ import re
4
  from linebot.v3.messaging import TextMessage, ImageMessage
5
 
6
  # 匯入所有服務函式
7
  from cwa_service import fetch_cwa_alarm_list, fetch_significant_earthquakes, fetch_latest_significant_earthquake
8
  from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
 
9
  from ai_service import generate_ai_text
 
10
  from config import CURRENT_YEAR, MCP_SERVER_URL
11
 
12
  def get_help_message() -> TextMessage:
 
22
  "• 6 - CWA 最近7天顯著有感地震\n\n"
23
  "【AI 與工具】\n"
24
  "• 7 <問題> - 與 AI 助理對話\n"
25
+ " (範例: 7 昨天花蓮有地震嗎?)\n"
26
+ " (範例: 7 2023年臺灣最大地震)"
 
 
27
  )
28
  return TextMessage(text=text)
29
 
 
32
  text = (
33
  "🤖 關於我\n\n"
34
  "我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
35
+ "• 版本: 5.2 (Gemini Advanced Edition)\n"
36
  "• 資料來源: CWA, USGS, Google Gemini\n"
37
  "• 開發者: dayichen"
38
  )
 
51
  f"地點: {row['place']}\n"
52
  f"報告連結: {row.get('url', '無')}"
53
  )
54
+ if count > 15: lines.append(f"... (還有 {count-15} 筆資料)")
 
55
  reply_text = "\n\n".join(lines)
56
+ else: reply_text = result
 
57
  return TextMessage(text=reply_text)
58
 
59
  def get_latest_earthquake_reply() -> list:
60
  """取得 CWA 最新一筆地震,並組合文字與圖片訊息。"""
61
  try:
62
  latest_eq = fetch_latest_significant_earthquake()
63
+ if not latest_eq: return [TextMessage(text="✅ 近期無顯著有感地震報告。")]
 
 
64
  mag_str = f"{latest_eq['Magnitude']:.1f}" if latest_eq.get('Magnitude') is not None else "—"
65
  depth_str = f"{latest_eq['Depth']:.0f}" if latest_eq.get('Depth') is not None else "—"
 
66
  text_message_content = (
67
  f"🚨 CWA 最新顯著有感地震\n"
68
  f"----------------------------------\n"
 
72
  f"報告: {latest_eq.get('URL', '無')}"
73
  )
74
  reply_messages = [TextMessage(text=text_message_content)]
 
75
  if latest_eq.get("ImageURL"):
76
  image_url = latest_eq["ImageURL"]
77
+ reply_messages.append(ImageMessage(original_content_url=image_url, preview_image_url=image_url))
 
 
 
78
  return reply_messages
79
+ except Exception as e: return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
 
80
 
81
+ def preprocess_ai_prompt(prompt: str) -> str:
82
  """
83
+ [新增] 預處理使用者發給 AI 的問題。
84
+ 如果偵測到特定關鍵字組合,就將問題改寫為更明確的指令。
85
  """
86
+ # 尋找 "YYYY年" 的格式
87
+ year_match = re.search(r"(\d{4})年", prompt)
88
 
89
+ # 如果同時找到年份和 "最大地震"
90
+ if year_match and "最大地震" in prompt:
91
+ year = year_match.group(1)
92
+ # 改寫成一個非常明確的指令
93
+ new_prompt = f"請幫我呼叫地震搜尋工具,找出{year}年1月1日至{year}年12月31日之間,規模最大的地震。"
94
+ print(f"--- AI Prompt Rewritten ---\nOriginal: {prompt}\nNew: {new_prompt}\n---------------------------")
95
+ return new_prompt
96
+
97
+ # 如果沒有匹配,回傳原始問題
98
+ return prompt
99
+
100
+ def process_message(user_message_raw: str, request_base_url: str) -> list:
101
+ """處理使用者輸入的主函式。"""
102
+ user_message = (user_message_raw or "").strip()
103
  parts = user_message.split(' ', 1)
104
  command_key = parts[0]
105
  arg = parts[1].strip() if len(parts) > 1 else ""
106
 
 
107
  if command_key == '1': return get_latest_earthquake_reply()
108
  if command_key == '2': return [TextMessage(text=fetch_global_last24h_text())]
109
  if command_key == '3': return [get_taiwan_earthquake_list()]
110
  if command_key == '4': return [TextMessage(text=f"🗺️ 外部地震查詢服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]
111
  if command_key == '5': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
112
  if command_key == '6': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
 
 
 
 
113
  if command_key == '8': return [get_info_message()]
114
  if command_key == '9': return [get_help_message()]
115
 
116
+ # [核心修改]
117
+ # 如果是 AI 指令 (7) 或其他無法識別的指令,先經過預處理
118
+ final_prompt = user_message
119
+ if command_key == '7':
120
+ if not arg: return [TextMessage(text="請輸入問題,例如:7 台灣最高的山是哪座?")]
121
+ final_prompt = arg # 如果是指令7,只取後面的問題部分
122
+
123
+ # 將最終要發給 AI 的問題傳入預處理函式
124
+ processed_prompt = preprocess_ai_prompt(final_prompt)
125
+
126
+ return [TextMessage(text=generate_ai_text(processed_prompt))]