Update main.py
Browse files
main.py
CHANGED
@@ -169,24 +169,6 @@ def handle_image_message(event):
|
|
169 |
chat = model.start_chat(history=[])
|
170 |
chat_sessions[user_id] = chat
|
171 |
|
172 |
-
user_text = event.message.text if event.message.type == "text" else None
|
173 |
-
if user_text and user_text.startswith("請幫我生成圖片"):
|
174 |
-
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
175 |
-
|
176 |
-
image_path_gen = generate_image_with_gemini(prompt)
|
177 |
-
if image_path:
|
178 |
-
# 使用 Access Token 上傳至 Imgur
|
179 |
-
IMGUR_ACCESS_TOKEN = os.environ.get("IMGUR_ACCESS_TOKEN")
|
180 |
-
image_url = upload_image_to_imgur_with_token(image_path, IMGUR_ACCESS_TOKEN)
|
181 |
-
|
182 |
-
if image_url:
|
183 |
-
line_bot_api.reply_message(
|
184 |
-
event.reply_token,
|
185 |
-
ImageSendMessage(original_content_url=image_url, preview_image_url=image_url)
|
186 |
-
)
|
187 |
-
else:
|
188 |
-
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片上傳失敗,請稍後再試~"))
|
189 |
-
|
190 |
|
191 |
# 從 LINE API 獲取圖片數據
|
192 |
image_path = get_image_url(event.message.id)
|
@@ -241,6 +223,20 @@ def handle_image_message(event):
|
|
241 |
# 檢查是否正在與使用者交談
|
242 |
elif working_status:
|
243 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
# 取得使用者輸入的文字
|
245 |
prompt = event.message.text
|
246 |
store_user_message(user_id, "text", prompt)
|
|
|
169 |
chat = model.start_chat(history=[])
|
170 |
chat_sessions[user_id] = chat
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
# 從 LINE API 獲取圖片數據
|
174 |
image_path = get_image_url(event.message.id)
|
|
|
223 |
# 檢查是否正在與使用者交談
|
224 |
elif working_status:
|
225 |
try:
|
226 |
+
|
227 |
+
if user_text and user_text.startswith("請幫我生成圖片"):
|
228 |
+
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
229 |
+
|
230 |
+
image_path_gen = generate_image_with_gemini(prompt)
|
231 |
+
if image_path:
|
232 |
+
# 使用 Access Token 上傳至 Imgur
|
233 |
+
IMGUR_ACCESS_TOKEN = os.environ.get("IMGUR_ACCESS_TOKEN")
|
234 |
+
image_url = upload_image_to_imgur_with_token(image_path, IMGUR_ACCESS_TOKEN)
|
235 |
+
|
236 |
+
if image_url:
|
237 |
+
line_bot_api.reply_message(event.reply_token,ImageSendMessage(original_content_url=image_url, preview_image_url=image_url))
|
238 |
+
else:
|
239 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片上傳失敗,請稍後再試~"))
|
240 |
# 取得使用者輸入的文字
|
241 |
prompt = event.message.text
|
242 |
store_user_message(user_id, "text", prompt)
|