Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ def send_message(phone_number, message, history):
|
|
| 14 |
"""
|
| 15 |
if not phone_number:
|
| 16 |
history = history or []
|
| 17 |
-
history.append(
|
| 18 |
return history, ""
|
| 19 |
|
| 20 |
if not message:
|
|
@@ -22,6 +22,9 @@ def send_message(phone_number, message, history):
|
|
| 22 |
|
| 23 |
history = history or []
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# ساخت یا بازیابی session_id
|
| 26 |
if phone_number not in session_data:
|
| 27 |
session_data[phone_number] = f"session_{phone_number}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
|
@@ -49,23 +52,23 @@ def send_message(phone_number, message, history):
|
|
| 49 |
result = response.json()
|
| 50 |
bot_response = result.get("response", "پاسخی دریافت نشد")
|
| 51 |
|
| 52 |
-
# اضافه کردن
|
| 53 |
-
history.append(
|
| 54 |
|
| 55 |
# نمایش اطلاعات اضافی اگر موجود باشد
|
| 56 |
if result.get("order_complete"):
|
| 57 |
-
history.append(
|
| 58 |
|
| 59 |
return history, ""
|
| 60 |
else:
|
| 61 |
-
history.append(
|
| 62 |
return history, ""
|
| 63 |
|
| 64 |
except requests.exceptions.Timeout:
|
| 65 |
-
history.append(
|
| 66 |
return history, ""
|
| 67 |
except requests.exceptions.RequestException as e:
|
| 68 |
-
history.append(
|
| 69 |
return history, ""
|
| 70 |
|
| 71 |
def clear_chat(phone_number):
|
|
|
|
| 14 |
"""
|
| 15 |
if not phone_number:
|
| 16 |
history = history or []
|
| 17 |
+
history.append({"role": "assistant", "content": "⚠️ لطفاً ابتدا شماره تلفن خود را وارد کنید!"})
|
| 18 |
return history, ""
|
| 19 |
|
| 20 |
if not message:
|
|
|
|
| 22 |
|
| 23 |
history = history or []
|
| 24 |
|
| 25 |
+
# اضافه کردن پیام کاربر به تاریخچه
|
| 26 |
+
history.append({"role": "user", "content": message})
|
| 27 |
+
|
| 28 |
# ساخت یا بازیابی session_id
|
| 29 |
if phone_number not in session_data:
|
| 30 |
session_data[phone_number] = f"session_{phone_number}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
|
|
|
| 52 |
result = response.json()
|
| 53 |
bot_response = result.get("response", "پاسخی دریافت نشد")
|
| 54 |
|
| 55 |
+
# اضافه کردن پاسخ ربات به تاریخچه
|
| 56 |
+
history.append({"role": "assistant", "content": bot_response})
|
| 57 |
|
| 58 |
# نمایش اطلاعات اضافی اگر موجود باشد
|
| 59 |
if result.get("order_complete"):
|
| 60 |
+
history.append({"role": "assistant", "content": "✅ سفارش شما کامل شد!"})
|
| 61 |
|
| 62 |
return history, ""
|
| 63 |
else:
|
| 64 |
+
history.append({"role": "assistant", "content": f"❌ خطا: {response.status_code}"})
|
| 65 |
return history, ""
|
| 66 |
|
| 67 |
except requests.exceptions.Timeout:
|
| 68 |
+
history.append({"role": "assistant", "content": "⏱️ زمان انتظار تمام شد. لطفاً دوباره تلاش کنید."})
|
| 69 |
return history, ""
|
| 70 |
except requests.exceptions.RequestException as e:
|
| 71 |
+
history.append({"role": "assistant", "content": f"❌ خطای اتصال: {str(e)}"})
|
| 72 |
return history, ""
|
| 73 |
|
| 74 |
def clear_chat(phone_number):
|