Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -93,6 +93,27 @@ def handle_message(event):
|
|
| 93 |
)
|
| 94 |
)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
@app.route('/static/<path:path>')
|
| 97 |
def send_static_content(path):
|
| 98 |
return send_from_directory('static', path)
|
|
|
|
| 93 |
)
|
| 94 |
)
|
| 95 |
|
| 96 |
+
@handler.add(MessageEvent, message=ImageMessage)
|
| 97 |
+
def handle_image_message(event):
|
| 98 |
+
# 取得圖片內容
|
| 99 |
+
message_content = line_bot_api.get_message_content(event.message.id)
|
| 100 |
+
image_path = f"{event.message.id}.jpg"
|
| 101 |
+
with open(image_path, 'wb') as fd:
|
| 102 |
+
for chunk in message_content.iter_content():
|
| 103 |
+
fd.write(chunk)
|
| 104 |
+
|
| 105 |
+
# 上傳至 Imgur
|
| 106 |
+
uploaded_image = imgur_client.upload_image(image_path, title="Uploaded with PyImgur")
|
| 107 |
+
image_url = uploaded_image.link
|
| 108 |
+
|
| 109 |
+
os.remove(image_path)
|
| 110 |
+
|
| 111 |
+
# 回傳圖片 URL 給使用者
|
| 112 |
+
line_bot_api.reply_message(
|
| 113 |
+
event.reply_token,
|
| 114 |
+
TextSendMessage(text=f"圖片已上傳至 Imgur:{imgur_url}")
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
@app.route('/static/<path:path>')
|
| 118 |
def send_static_content(path):
|
| 119 |
return send_from_directory('static', path)
|