Update engine/conversation_engine.py
Browse files
engine/conversation_engine.py
CHANGED
|
@@ -2,6 +2,63 @@ from engine.flow_router import route_message
|
|
| 2 |
from sessions import get_session, upsert_session
|
| 3 |
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def process_message(bot_number: str, customer_phone: str, text: str):
|
| 6 |
session = get_session(customer_phone, bot_number)
|
| 7 |
|
|
@@ -10,24 +67,49 @@ def process_message(bot_number: str, customer_phone: str, text: str):
|
|
| 10 |
"current_state": "START",
|
| 11 |
"flow_type": None,
|
| 12 |
"flow_data": {
|
| 13 |
-
"uid": f"{bot_number}__{customer_phone}"
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
state = session.get("current_state", "START")
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
next_state = result["next_state"]
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
upsert_session(
|
| 25 |
customer_phone=customer_phone,
|
| 26 |
bot_number=bot_number,
|
| 27 |
current_state=next_state,
|
| 28 |
flow_type=flow_type,
|
| 29 |
-
flow_data=
|
| 30 |
last_message=text,
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
return result
|
|
|
|
| 2 |
from sessions import get_session, upsert_session
|
| 3 |
|
| 4 |
|
| 5 |
+
def normalize_text(text: str) -> str:
|
| 6 |
+
return (text or "").strip().lower()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def detect_gender(text: str):
|
| 10 |
+
"""
|
| 11 |
+
تخمين خفيف وآمن:
|
| 12 |
+
- female
|
| 13 |
+
- male
|
| 14 |
+
- None
|
| 15 |
+
"""
|
| 16 |
+
t = normalize_text(text)
|
| 17 |
+
|
| 18 |
+
female_markers = [
|
| 19 |
+
"انا مهتمه",
|
| 20 |
+
"مهتمه",
|
| 21 |
+
"عايزه",
|
| 22 |
+
"عاوزه",
|
| 23 |
+
"عاوزة",
|
| 24 |
+
"عايزة",
|
| 25 |
+
"حابه",
|
| 26 |
+
"حابة",
|
| 27 |
+
"محتاجه",
|
| 28 |
+
"محتاجة",
|
| 29 |
+
"انا ام",
|
| 30 |
+
"انا ماما",
|
| 31 |
+
"انا والده",
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
male_markers = [
|
| 35 |
+
"انا مهتم",
|
| 36 |
+
"مهتم",
|
| 37 |
+
"عايز",
|
| 38 |
+
"حابب",
|
| 39 |
+
"محتاج",
|
| 40 |
+
"انا اب",
|
| 41 |
+
"انا بابا",
|
| 42 |
+
"انا والد",
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
for marker in female_markers:
|
| 46 |
+
if marker in t:
|
| 47 |
+
return "female"
|
| 48 |
+
|
| 49 |
+
for marker in male_markers:
|
| 50 |
+
if marker in t:
|
| 51 |
+
return "male"
|
| 52 |
+
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def merge_flow_data(old_data: dict, new_data: dict):
|
| 57 |
+
merged = dict(old_data or {})
|
| 58 |
+
merged.update(new_data or {})
|
| 59 |
+
return merged
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def process_message(bot_number: str, customer_phone: str, text: str):
|
| 63 |
session = get_session(customer_phone, bot_number)
|
| 64 |
|
|
|
|
| 67 |
"current_state": "START",
|
| 68 |
"flow_type": None,
|
| 69 |
"flow_data": {
|
| 70 |
+
"uid": f"{bot_number}__{customer_phone}",
|
| 71 |
+
"customer_phone": customer_phone,
|
| 72 |
+
"bot_number": bot_number,
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
state = session.get("current_state", "START")
|
| 77 |
+
old_flow_data = session.get("flow_data", {}) or {}
|
| 78 |
+
|
| 79 |
+
# تأكد إن البيانات الأساسية موجودة دائمًا
|
| 80 |
+
base_flow_data = merge_flow_data(old_flow_data, {
|
| 81 |
+
"uid": old_flow_data.get("uid") or f"{bot_number}__{customer_phone}",
|
| 82 |
+
"customer_phone": customer_phone,
|
| 83 |
+
"bot_number": bot_number,
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
# خزّن gender لو واضح، لكن ما تمسحهوش لو كان موجود قبل كده
|
| 87 |
+
detected_gender = detect_gender(text)
|
| 88 |
+
if detected_gender and not base_flow_data.get("gender"):
|
| 89 |
+
base_flow_data["gender"] = detected_gender
|
| 90 |
+
|
| 91 |
+
# ابني session محدثة قبل التوجيه
|
| 92 |
+
working_session = dict(session)
|
| 93 |
+
working_session["flow_data"] = base_flow_data
|
| 94 |
+
|
| 95 |
+
result = route_message(state, text, working_session)
|
| 96 |
|
| 97 |
next_state = result["next_state"]
|
| 98 |
+
result_flow_data = result.get("flow_data", base_flow_data) or {}
|
| 99 |
+
|
| 100 |
+
# ندمج بدل ما نستبدل عشان ما نفقدش uid / gender / customer info
|
| 101 |
+
final_flow_data = merge_flow_data(base_flow_data, result_flow_data)
|
| 102 |
+
|
| 103 |
+
flow_type = final_flow_data.get("customer_type") or session.get("flow_type")
|
| 104 |
|
| 105 |
upsert_session(
|
| 106 |
customer_phone=customer_phone,
|
| 107 |
bot_number=bot_number,
|
| 108 |
current_state=next_state,
|
| 109 |
flow_type=flow_type,
|
| 110 |
+
flow_data=final_flow_data,
|
| 111 |
last_message=text,
|
| 112 |
)
|
| 113 |
|
| 114 |
+
result["flow_data"] = final_flow_data
|
| 115 |
return result
|