Update main.py
Browse files
main.py
CHANGED
@@ -112,16 +112,25 @@ def handle_message(event):
|
|
112 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
113 |
def get_image_url(message_id):
|
114 |
"""
|
115 |
-
從 LINE
|
|
|
|
|
116 |
"""
|
117 |
try:
|
118 |
-
#
|
119 |
message_content = line_bot_api.get_message_content(message_id)
|
120 |
-
|
|
|
|
|
|
|
|
|
121 |
with open(file_path, "wb") as f:
|
122 |
for chunk in message_content.iter_content():
|
123 |
f.write(chunk)
|
|
|
|
|
124 |
return file_path
|
|
|
125 |
except Exception as e:
|
126 |
print(f"Error downloading image: {e}")
|
127 |
return None
|
@@ -160,12 +169,20 @@ def handle_image_message(event):
|
|
160 |
# 從 LINE API 獲取圖片數據
|
161 |
image_path = get_image_url(event.message.id)
|
162 |
|
163 |
-
if
|
|
|
|
|
|
|
|
|
164 |
line_bot_api.reply_message(
|
165 |
event.reply_token,
|
166 |
-
TextSendMessage(text="
|
|
|
|
|
|
|
|
|
|
|
167 |
)
|
168 |
-
return
|
169 |
|
170 |
# 獲取用戶問題(文字)
|
171 |
user_text = event.message.text # 自行實現,根據需求追蹤用戶文字
|
|
|
112 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
113 |
def get_image_url(message_id):
|
114 |
"""
|
115 |
+
從 LINE API 獲取圖片數據並保存為本地文件
|
116 |
+
:param message_id: LINE 傳遞的 message.id
|
117 |
+
:return: 圖片的本地保存路徑
|
118 |
"""
|
119 |
try:
|
120 |
+
# 使用 LINE API 獲取圖片內容
|
121 |
message_content = line_bot_api.get_message_content(message_id)
|
122 |
+
|
123 |
+
# 定義圖片保存路徑
|
124 |
+
file_path = f"/tmp/{message_id}.png" # 可根據需求調整路徑和格式
|
125 |
+
|
126 |
+
# 將圖片保存到本地
|
127 |
with open(file_path, "wb") as f:
|
128 |
for chunk in message_content.iter_content():
|
129 |
f.write(chunk)
|
130 |
+
|
131 |
+
# 返回本地圖片路徑
|
132 |
return file_path
|
133 |
+
|
134 |
except Exception as e:
|
135 |
print(f"Error downloading image: {e}")
|
136 |
return None
|
|
|
169 |
# 從 LINE API 獲取圖片數據
|
170 |
image_path = get_image_url(event.message.id)
|
171 |
|
172 |
+
if image_path:
|
173 |
+
# 處理圖片或進行進一步分析
|
174 |
+
print(f"圖片已保存至: {image_path}")
|
175 |
+
|
176 |
+
# 回應用戶
|
177 |
line_bot_api.reply_message(
|
178 |
event.reply_token,
|
179 |
+
TextSendMessage(text="圖片已接收成功囉,你有什麼樣的疑問呢?")
|
180 |
+
)
|
181 |
+
else:
|
182 |
+
line_bot_api.reply_message(
|
183 |
+
event.reply_token,
|
184 |
+
TextSendMessage(text="無法下載圖片,請稍後再試!")
|
185 |
)
|
|
|
186 |
|
187 |
# 獲取用戶問題(文字)
|
188 |
user_text = event.message.text # 自行實現,根據需求追蹤用戶文字
|