Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ import requests
|
|
| 12 |
import pandas as pd
|
| 13 |
from datetime import datetime, timedelta
|
| 14 |
|
| 15 |
-
# ---
|
| 16 |
CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
|
| 17 |
CHANNEL_SECRET = os.getenv('CHANNEL_SECRET')
|
| 18 |
|
|
@@ -22,7 +22,34 @@ app = Flask(__name__)
|
|
| 22 |
configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
|
| 23 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
| 27 |
|
| 28 |
def fetch_earthquake_data_for_line():
|
|
@@ -36,7 +63,7 @@ def fetch_earthquake_data_for_line():
|
|
| 36 |
"endtime": now.strftime('%Y-%m-%d'),
|
| 37 |
"minmagnitude": 5.0,
|
| 38 |
"limit": 10,
|
| 39 |
-
"orderby": "time"
|
| 40 |
}
|
| 41 |
|
| 42 |
try:
|
|
@@ -65,8 +92,7 @@ def fetch_earthquake_data_for_line():
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"❌ 處理資料時發生未知錯誤: {e}"
|
| 67 |
|
| 68 |
-
|
| 69 |
-
# --- Flask Webhook 路由 ---
|
| 70 |
@app.route("/callback", methods=['POST'])
|
| 71 |
def callback():
|
| 72 |
signature = request.headers['X-Line-Signature']
|
|
@@ -80,8 +106,7 @@ def callback():
|
|
| 80 |
abort(400)
|
| 81 |
return 'OK'
|
| 82 |
|
| 83 |
-
|
| 84 |
-
# --- LINE 訊息處理 ---
|
| 85 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 86 |
def handle_message(event):
|
| 87 |
user_message = event.message.text
|
|
|
|
| 12 |
import pandas as pd
|
| 13 |
from datetime import datetime, timedelta
|
| 14 |
|
| 15 |
+
# --- 環境變數 ---
|
| 16 |
CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
|
| 17 |
CHANNEL_SECRET = os.getenv('CHANNEL_SECRET')
|
| 18 |
|
|
|
|
| 22 |
configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
|
| 23 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 24 |
|
| 25 |
+
|
| 26 |
+
# --- ✨✨✨ 新增的歡迎頁面路由 ✨✨✨ ---
|
| 27 |
+
@app.route("/", methods=['GET'])
|
| 28 |
+
def home():
|
| 29 |
+
# 當有人用瀏覽器訪問根網址時,回傳這段 HTML 訊息
|
| 30 |
+
return """
|
| 31 |
+
<html>
|
| 32 |
+
<head>
|
| 33 |
+
<title>LINE Bot Server</title>
|
| 34 |
+
<style>
|
| 35 |
+
body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f2f5; margin: 0; }
|
| 36 |
+
.container { text-align: center; padding: 40px; background-color: white; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); }
|
| 37 |
+
h1 { color: #1dcd00; }
|
| 38 |
+
p { color: #333; font-size: 1.2em; }
|
| 39 |
+
.status { font-weight: bold; color: #28a745; }
|
| 40 |
+
</style>
|
| 41 |
+
</head>
|
| 42 |
+
<body>
|
| 43 |
+
<div class="container">
|
| 44 |
+
<h1>✓ LINE Bot Server is Running</h1>
|
| 45 |
+
<p>This is the backend service for the Earthquake Alert Bot.</p>
|
| 46 |
+
<p>The service is <span class="status">active</span> and listening for webhook events from LINE.</p>
|
| 47 |
+
</div>
|
| 48 |
+
</body>
|
| 49 |
+
</html>
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
# --- 地震查詢核心邏輯 (與之前相同) ---
|
| 53 |
USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
| 54 |
|
| 55 |
def fetch_earthquake_data_for_line():
|
|
|
|
| 63 |
"endtime": now.strftime('%Y-%m-%d'),
|
| 64 |
"minmagnitude": 5.0,
|
| 65 |
"limit": 10,
|
| 66 |
+
"orderby": "time"
|
| 67 |
}
|
| 68 |
|
| 69 |
try:
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
return f"❌ 處理資料時發生未知錯誤: {e}"
|
| 94 |
|
| 95 |
+
# --- Flask Webhook 路由 (與之前相同) ---
|
|
|
|
| 96 |
@app.route("/callback", methods=['POST'])
|
| 97 |
def callback():
|
| 98 |
signature = request.headers['X-Line-Signature']
|
|
|
|
| 106 |
abort(400)
|
| 107 |
return 'OK'
|
| 108 |
|
| 109 |
+
# --- LINE 訊息處理 (與之前相同) ---
|
|
|
|
| 110 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 111 |
def handle_message(event):
|
| 112 |
user_message = event.message.text
|