cwadayi commited on
Commit
a8eda7b
·
verified ·
1 Parent(s): 3387e96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -5,8 +5,10 @@ from linebot.v3 import WebhookHandler
5
  from linebot.v3.exceptions import InvalidSignatureError
6
  from linebot.v3.messaging import (
7
  Configuration, ApiClient, MessagingApi,
8
- ReplyMessageRequest, TextMessage, ImageSendMessage
9
  )
 
 
10
  from linebot.v3.webhooks import MessageEvent, TextMessageContent
11
 
12
  import requests
@@ -14,13 +16,12 @@ import pandas as pd
14
  import plotly.express as px
15
  from datetime import datetime, timedelta
16
 
17
- # --- 環境變數 ---
18
  CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
19
  CHANNEL_SECRET = os.getenv('CHANNEL_SECRET')
20
- # --- ✨✨✨ 修改點:讀取新的 Secret 名稱 ✨✨✨ ---
21
  HF_SPACE_URL = os.getenv('SPACEURL')
22
 
23
- # --- Flask & LINE Bot 初始化 ---
24
  app = Flask(__name__)
25
 
26
  if not os.path.exists('static'):
@@ -29,7 +30,7 @@ if not os.path.exists('static'):
29
  configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
30
  handler = WebhookHandler(CHANNEL_SECRET)
31
 
32
- # --- 歡迎頁面 & 靜態圖片服務路由 ---
33
  @app.route("/", methods=['GET'])
34
  def home():
35
  return """
@@ -43,7 +44,7 @@ def home():
43
  def serve_static(filename):
44
  return send_from_directory('static', filename)
45
 
46
- # --- 地震查詢核心邏輯 ---
47
  USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
48
 
49
  def fetch_earthquake_data_for_line():
@@ -83,7 +84,7 @@ def fetch_taiwan_earthquake_data_df():
83
  return pd.DataFrame(earthquake_list)
84
  except Exception as e: return f"❌ 查詢失敗: {e}"
85
 
86
- # --- 繪圖函式 ---
87
  def create_and_save_map(df):
88
  fig = px.scatter_mapbox(
89
  df, lat="latitude", lon="longitude", size="magnitude", color="magnitude",
@@ -97,7 +98,7 @@ def create_and_save_map(df):
97
  fig.write_image(filepath, scale=2)
98
  return filename
99
 
100
- # --- Flask Webhook 路由 ---
101
  @app.route("/callback", methods=['POST'])
102
  def callback():
103
  signature = request.headers['X-Line-Signature']
@@ -108,7 +109,7 @@ def callback():
108
  abort(400)
109
  return 'OK'
110
 
111
- # --- LINE 訊息處理 ---
112
  @handler.add(MessageEvent, message=TextMessageContent)
113
  def handle_message(event):
114
  user_message = event.message.text.strip().lower()
 
5
  from linebot.v3.exceptions import InvalidSignatureError
6
  from linebot.v3.messaging import (
7
  Configuration, ApiClient, MessagingApi,
8
+ ReplyMessageRequest, TextMessage
9
  )
10
+ # --- ✨✨✨ CORRECTION: Import ImageSendMessage from the correct module ✨✨✨ ---
11
+ from linebot.v3.messaging.models.image_send_message import ImageSendMessage
12
  from linebot.v3.webhooks import MessageEvent, TextMessageContent
13
 
14
  import requests
 
16
  import plotly.express as px
17
  from datetime import datetime, timedelta
18
 
19
+ # --- Environment Variables ---
20
  CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
21
  CHANNEL_SECRET = os.getenv('CHANNEL_SECRET')
 
22
  HF_SPACE_URL = os.getenv('SPACEURL')
23
 
24
+ # --- Flask & LINE Bot Initialization ---
25
  app = Flask(__name__)
26
 
27
  if not os.path.exists('static'):
 
30
  configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
31
  handler = WebhookHandler(CHANNEL_SECRET)
32
 
33
+ # --- Welcome Page & Static Image Server Routes ---
34
  @app.route("/", methods=['GET'])
35
  def home():
36
  return """
 
44
  def serve_static(filename):
45
  return send_from_directory('static', filename)
46
 
47
+ # --- Earthquake Query Logic ---
48
  USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
49
 
50
  def fetch_earthquake_data_for_line():
 
84
  return pd.DataFrame(earthquake_list)
85
  except Exception as e: return f"❌ 查詢失敗: {e}"
86
 
87
+ # --- Map Plotting Function ---
88
  def create_and_save_map(df):
89
  fig = px.scatter_mapbox(
90
  df, lat="latitude", lon="longitude", size="magnitude", color="magnitude",
 
98
  fig.write_image(filepath, scale=2)
99
  return filename
100
 
101
+ # --- Flask Webhook Route ---
102
  @app.route("/callback", methods=['POST'])
103
  def callback():
104
  signature = request.headers['X-Line-Signature']
 
109
  abort(400)
110
  return 'OK'
111
 
112
+ # --- LINE Message Handler ---
113
  @handler.add(MessageEvent, message=TextMessageContent)
114
  def handle_message(event):
115
  user_message = event.message.text.strip().lower()