Update main.py
Browse files
main.py
CHANGED
|
@@ -92,9 +92,22 @@ def handle_message(event):
|
|
| 92 |
if (completion != None):
|
| 93 |
# 取得生成結果
|
| 94 |
out = completion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
else:
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
except:
|
| 99 |
# 處理錯誤
|
| 100 |
out = "Gemini執行出錯!請換個說法!"
|
|
|
|
| 92 |
if (completion != None):
|
| 93 |
# 取得生成結果
|
| 94 |
out = completion
|
| 95 |
+
# 判斷如果是文字
|
| 96 |
+
elif type=='text':
|
| 97 |
+
msg = json_data['events'][0]['message']['text'] # 取得 LINE 收到的文字訊息
|
| 98 |
+
reply = msg
|
| 99 |
+
# 判斷如果是圖片
|
| 100 |
+
elif type == 'image':
|
| 101 |
+
msgID = json_data['events'][0]['message']['id'] # 取得訊息 id
|
| 102 |
+
message_content = line_bot_api.get_message_content(msgID) # 根據訊息 ID 取得訊息內容
|
| 103 |
+
# 在同樣的資料夾中建立以訊息 ID 為檔名的 .jpg 檔案
|
| 104 |
+
with open(f'{msgID}.jpg', 'wb') as fd:
|
| 105 |
+
fd.write(message_content.content) # 以二進位的方式寫入檔案
|
| 106 |
+
reply = '圖片儲存完成!' # 設定要回傳的訊息
|
| 107 |
else:
|
| 108 |
+
reply = '你傳的不是文字或圖片呦~'
|
| 109 |
+
print(reply)
|
| 110 |
+
line_bot_api.reply_message(tk,TextSendMessage(reply)) # 回傳訊息
|
| 111 |
except:
|
| 112 |
# 處理錯誤
|
| 113 |
out = "Gemini執行出錯!請換個說法!"
|