cwadayi commited on
Commit
240ea75
·
verified ·
1 Parent(s): fa70216

Update command_handler.py

Browse files
Files changed (1) hide show
  1. command_handler.py +68 -6
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, fetch_local_earthquakes
7
  from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
8
  from plotting_service import create_and_save_map
@@ -19,7 +19,7 @@ def get_help_message() -> TextMessage:
19
  "• /map - 取得台灣顯著地震地圖連結\n"
20
  "• /alert - 獲取 CWA 最新地震預警\n"
21
  "• /significant - CWA 最新顯著有感地震\n"
22
- "• /local <縣市> - 小區域地震 (可選填縣市)\n\n" # [修改] 更新指令說明
23
  "【AI 與工具】\n"
24
  "• /ai <問題> - 與 AI 助理對話\n\n"
25
  "【基本指令】\n"
@@ -28,13 +28,65 @@ def get_help_message() -> TextMessage:
28
  )
29
  return TextMessage(text=text)
30
 
31
- # ... (get_info_message 和其他 get_ 函式不變) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def process_message(user_message_raw: str, request_base_url: str) -> list:
34
  """處理使用者的文字訊息並回傳一個包含回覆訊息的列表。"""
35
  user_message = (user_message_raw or "").strip()
36
 
37
- # ... (舊指令相容性轉換不變) ...
 
 
 
 
 
 
 
 
38
 
39
  if user_message.startswith('/'):
40
  parts = user_message.split(' ', 1)
@@ -43,10 +95,19 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
43
 
44
  if command == '/help':
45
  return [get_help_message()]
46
- # ... (其他 command 判斷不變) ...
 
 
 
 
 
 
 
 
 
47
  elif command == '/significant':
48
  return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
49
- elif command == '/local': # [修改] 簡化邏輯
50
  return [TextMessage(text=fetch_local_earthquakes(area_name=arg))]
51
  elif command == '/ai':
52
  if not arg:
@@ -55,3 +116,4 @@ def process_message(user_message_raw: str, request_base_url: str) -> list:
55
 
56
  # 若非指令,則預設交給 AI 處理 (作為備援)
57
  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, fetch_local_earthquakes
7
  from usgs_service import fetch_global_last24h_text, fetch_taiwan_df_this_year
8
  from plotting_service import create_and_save_map
 
19
  "• /map - 取得台灣顯著地震地圖連結\n"
20
  "• /alert - 獲取 CWA 最新地震預警\n"
21
  "• /significant - CWA 最新顯著有感地震\n"
22
+ "• /local <縣市> - 小區域地震 (可選填縣市)\n\n"
23
  "【AI 與工具】\n"
24
  "• /ai <問題> - 與 AI 助理對話\n\n"
25
  "【基本指令】\n"
 
28
  )
29
  return TextMessage(text=text)
30
 
31
+ def get_info_message() -> TextMessage:
32
+ """回傳機器人資訊。"""
33
+ text = (
34
+ "🤖 關於我\n\n"
35
+ "我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
36
+ "• 版本: 2.3\n"
37
+ "• 資料來源: CWA, USGS, Hugging Face\n"
38
+ "• 開發者: dayichen"
39
+ )
40
+ return TextMessage(text=text)
41
+
42
+ def get_taiwan_earthquake_list() -> TextMessage:
43
+ """回傳近期的台灣地震文字列表。"""
44
+ result = fetch_taiwan_df_this_year()
45
+ if isinstance(result, pd.DataFrame):
46
+ count = len(result)
47
+ lines = [f"🇹🇼 今年 ({CURRENT_YEAR} 年) 台灣區域顯著地震 (M≥5.0),共 {count} 筆:", "-" * 20]
48
+ for _, row in result.head(15).iterrows():
49
+ t = row["time_utc"].strftime("%Y-%m-%d %H:%M")
50
+ lines.append(
51
+ f"震級: {row['magnitude']:.1f} | 日期時間: {t} (UTC)\n"
52
+ f"地點: {row['place']}\n"
53
+ f"報告連結: {row.get('url', '無')}"
54
+ )
55
+ if count > 15:
56
+ lines.append(f"... (還有 {count - 15} 筆,請用 /map 查看全部)")
57
+ reply_text = "\n\n".join(lines)
58
+ else:
59
+ reply_text = result
60
+ return TextMessage(text=reply_text)
61
+
62
+ def get_taiwan_earthquake_map(base_url: str) -> list:
63
+ """產生地震地圖,並回傳包含連結的文字訊息。"""
64
+ result = fetch_taiwan_df_this_year()
65
+ if isinstance(result, pd.DataFrame):
66
+ filename = create_and_save_map(result)
67
+ image_url = f"{(HF_SPACE_URL or base_url)}/static/{filename}"
68
+ reply_text = (
69
+ "🗺️ 已為您繪製地震分佈圖。\n\n"
70
+ "請點擊以下連結查看圖片:\n"
71
+ f"{image_url}"
72
+ )
73
+ return [TextMessage(text=reply_text)]
74
+ else:
75
+ return [TextMessage(text=result)]
76
 
77
  def process_message(user_message_raw: str, request_base_url: str) -> list:
78
  """處理使用者的文字訊息並回傳一個包含回覆訊息的列表。"""
79
  user_message = (user_message_raw or "").strip()
80
 
81
+ # 將舊指令對應到新指令,提供向下相容
82
+ if user_message.lower() in ["地震", "quake"]:
83
+ user_message = "/global"
84
+ elif user_message.lower() in ["台灣地震", "臺灣地震"]:
85
+ user_message = "/taiwan"
86
+ elif user_message.lower() in ["台灣地震畫圖", "臺灣地震畫圖"]:
87
+ user_message = "/map"
88
+ elif user_message.lower() == "地震預警":
89
+ user_message = "/alert"
90
 
91
  if user_message.startswith('/'):
92
  parts = user_message.split(' ', 1)
 
95
 
96
  if command == '/help':
97
  return [get_help_message()]
98
+ elif command == '/info':
99
+ return [get_info_message()]
100
+ elif command == '/global':
101
+ return [TextMessage(text=fetch_global_last24h_text())]
102
+ elif command == '/taiwan':
103
+ return [get_taiwan_earthquake_list()]
104
+ elif command == '/map':
105
+ return get_taiwan_earthquake_map(request_base_url)
106
+ elif command == '/alert':
107
+ return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
108
  elif command == '/significant':
109
  return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
110
+ elif command == '/local':
111
  return [TextMessage(text=fetch_local_earthquakes(area_name=arg))]
112
  elif command == '/ai':
113
  if not arg:
 
116
 
117
  # 若非指令,則預設交給 AI 處理 (作為備援)
118
  return [TextMessage(text=generate_ai_text(user_message))]
119
+