cwadayi commited on
Commit
eb68c9f
·
verified ·
1 Parent(s): 8489f78

Create Tmp

Browse files
Files changed (1) hide show
  1. Tmp +129 -0
Tmp ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # command_handler.py (Corrected Version)
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
+ # [修正] 將 HF_SPACE_URL 改為 MCP_SERVER_URL
11
+ from config import CURRENT_YEAR, MCP_SERVER_URL
12
+
13
+ def get_help_message() -> TextMessage:
14
+ text = (
15
+ "📖 指令列表 (輸入 #數字 即可)\n\n"
16
+ "【地震資訊】\n"
17
+ "• #1 - 最新一筆顯著地震 (含圖)\n"
18
+ "• #2 - 全球近24小時顯著地震(USGS)\n"
19
+ "• #3 - 今年台灣顯著地震列表(USGS)\n"
20
+ "• #4 - CWA 地震目錄查詢 (外部連結)\n"
21
+ "• #5 - CWA 最新地震預警\n"
22
+ "• #6 - CWA 最近7天顯著有感地震\n\n"
23
+ "【AI 與工具】\n"
24
+ "• #7 <問題> - 與 AI 助理對話\n\n"
25
+ "【基本指令】\n"
26
+ "• #8 - 關於此機器人\n"
27
+ "• #9 - 顯示此說明"
28
+ )
29
+ return TextMessage(text=text)
30
+
31
+ def get_info_message() -> TextMessage:
32
+ text = (
33
+ "🤖 關於我\n\n"
34
+ "我是一個多功能助理機器人,提供地震查詢與 AI 對話功能。\n\n"
35
+ "• 版本: 5.0 (Gemini Edition) 搭配搜尋地震目錄的MCP功能(可用資料 : 1973-01-01 至 2025-07-06)\n"
36
+ "• 資料來源: CWA, USGS, Google Gemini\n"
37
+ "• 開發者: dayichen"
38
+ )
39
+ return TextMessage(text=text)
40
+
41
+ def get_taiwan_earthquake_list() -> TextMessage:
42
+ result = fetch_taiwan_df_this_year()
43
+ if isinstance(result, pd.DataFrame):
44
+ count = len(result)
45
+ lines = [f"🇹🇼 今年 ({CURRENT_YEAR} 年) 台灣區域顯著地震 (M≥5.0),共 {count} 筆:", "-" * 20]
46
+ for _, row in result.head(15).iterrows():
47
+ t = row["time_utc"].strftime("%Y-%m-%d %H:%M")
48
+ lines.append(
49
+ f"規模: {row['magnitude']:.1f} | 日期時間: {t} (UTC)\n"
50
+ f"地點: {row['place']}\n"
51
+ f"報告連結: {row.get('url', '無')}"
52
+ )
53
+ if count > 15:
54
+ lines.append(f"... (還有 {count-15} 筆資料)")
55
+ reply_text = "\n\n".join(lines)
56
+ else:
57
+ reply_text = result
58
+ return TextMessage(text=reply_text)
59
+
60
+ def get_latest_earthquake_reply() -> list:
61
+ try:
62
+ latest_eq = fetch_latest_significant_earthquake()
63
+ if not latest_eq:
64
+ return [TextMessage(text="✅ 近期無顯著有感地震報告。")]
65
+
66
+ mag_str = f"{latest_eq['Magnitude']:.1f}" if latest_eq.get('Magnitude') is not None else "—"
67
+ depth_str = f"{latest_eq['Depth']:.0f}" if latest_eq.get('Depth') is not None else "—"
68
+
69
+ text_message_content = (
70
+ f"🚨 CWA 最新顯著有感地震\n"
71
+ f"----------------------------------\n"
72
+ f"時間: {latest_eq.get('TimeStr', '—')}\n"
73
+ f"地點: {latest_eq.get('Location', '—')}\n"
74
+ f"規模: M{mag_str} | 深度: {depth_str} km\n"
75
+ f"報告: {latest_eq.get('URL', '無')}"
76
+ )
77
+ reply_messages = [TextMessage(text=text_message_content)]
78
+
79
+ if latest_eq.get("ImageURL"):
80
+ image_url = latest_eq["ImageURL"]
81
+ reply_messages.append(
82
+ ImageMessage(original_content_url=image_url, preview_image_url=image_url)
83
+ )
84
+
85
+ return reply_messages
86
+ except Exception as e:
87
+ return [TextMessage(text=f"❌ 查詢最新地震失敗:{e}")]
88
+
89
+ def process_message(user_message_raw: str, request_base_url: str) -> list:
90
+ user_message = (user_message_raw or "").strip()
91
+
92
+ cmd_map = {
93
+ '#1': '/latest', '#2': '/global', '#3': '/taiwan',
94
+ '#4': '/map', '#5': '/alert', '#6': '/significant',
95
+ '#7': '/ai', '#8': '/info', '#9': '/help',
96
+ '地震': '/global', 'quake': '/global', '幫助': '/help',
97
+ '台灣地震': '/taiwan', '臺灣地震': '/taiwan',
98
+ '台灣地震畫圖': '/map', '臺灣地震畫圖': '/map',
99
+ '地震預警': '/alert',
100
+ }
101
+
102
+ command = ""
103
+ arg = ""
104
+
105
+ if user_message.lower() in cmd_map:
106
+ command = cmd_map[user_message.lower()]
107
+ elif user_message.startswith(('#', '/')):
108
+ parts = user_message.split(' ', 1)
109
+ key = parts[0]
110
+ arg = parts[1].strip() if len(parts) > 1 else ""
111
+ if key in cmd_map:
112
+ command = cmd_map[key]
113
+
114
+ if command:
115
+ if command == '/help': return [get_help_message()]
116
+ if command == '/info': return [get_info_message()]
117
+ if command == '/latest': return get_latest_earthquake_reply()
118
+ if command == '/global': return [TextMessage(text=fetch_global_last24h_text())]
119
+ if command == '/taiwan': return [get_taiwan_earthquake_list()]
120
+ # [修正] 將 HF_SPACE_URL 改為 MCP_SERVER_URL
121
+ if command == '/map': return [TextMessage(text=f"🗺️ 外部地震查詢服務\n\n請點擊以下連結:\n{MCP_SERVER_URL}")]
122
+ if command == '/alert': return [TextMessage(text=fetch_cwa_alarm_list(limit=5))]
123
+ if command == '/significant': return [TextMessage(text=fetch_significant_earthquakes(limit=5))]
124
+ if command == '/ai':
125
+ prompt = arg
126
+ if not prompt: return [TextMessage(text="請輸入問題,例如:#7 台灣最高的山是哪座?")]
127
+ return [TextMessage(text=generate_ai_text(prompt))]
128
+
129
+ return [TextMessage(text=generate_ai_text(user_message))]