Auto-detect trip card replies by context_message_id, bypassing AI for reliable trip selection
Browse filesWhen a passenger replies to a trip card via WhatsApp's reply feature,
the context_message_id already identifies which trip was selected.
Instead of relying on the LLM to interpret the reply text (which
fails for short replies like '.'), detect the trip card reply
automatically and call create_booking_lead directly.
app/services/conversation_service.py
CHANGED
|
@@ -80,6 +80,47 @@ class ConversationService:
|
|
| 80 |
user_mode=user_mode,
|
| 81 |
current_message=current_message,
|
| 82 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
reply = await self.ai.generate_reply(
|
| 84 |
messages=self._ai_messages(context, user_mode=user_mode),
|
| 85 |
tools=get_tool_schemas(user_mode),
|
|
|
|
| 80 |
user_mode=user_mode,
|
| 81 |
current_message=current_message,
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
if user_mode == "passenger" and inbound.context_message_id:
|
| 85 |
+
original = await self.repository.get_message_by_whatsapp_id(inbound.context_message_id)
|
| 86 |
+
if original:
|
| 87 |
+
orig_meta = original.get("metadata") or {}
|
| 88 |
+
if orig_meta.get("type") == "trip_card":
|
| 89 |
+
trip_id = orig_meta.get("trip_id")
|
| 90 |
+
if trip_id:
|
| 91 |
+
handlers = FalsaToolHandlers(
|
| 92 |
+
repository=self.repository,
|
| 93 |
+
embeddings=self.embeddings,
|
| 94 |
+
whatsapp=self.whatsapp,
|
| 95 |
+
customer=customer,
|
| 96 |
+
remoteJid=inbound.remoteJid,
|
| 97 |
+
embedding_model=self.settings.jina_embedding_model,
|
| 98 |
+
current_message=current_message,
|
| 99 |
+
)
|
| 100 |
+
result = await handlers.create_booking_lead(
|
| 101 |
+
{"trip_id": trip_id, "requested_seats": 1}
|
| 102 |
+
)
|
| 103 |
+
if result.ok:
|
| 104 |
+
driver_phone = result.data.get("driver_phone")
|
| 105 |
+
reply = (
|
| 106 |
+
f"تم تأكيد الحجز! يمكنك التواصل مع السائق على الرقم: {driver_phone}"
|
| 107 |
+
if driver_phone
|
| 108 |
+
else "تم تأكيد الحجز! سيتم إشعار السائق."
|
| 109 |
+
)
|
| 110 |
+
else:
|
| 111 |
+
reply = f"عذراً، لم يتم تأكيد الحجز: {result.error}"
|
| 112 |
+
await self.whatsapp.send_text(inbound.remoteJid, reply)
|
| 113 |
+
await self.repository.create_message(
|
| 114 |
+
customer_id=str(customer["id"]),
|
| 115 |
+
sender_type="assistant",
|
| 116 |
+
message=reply,
|
| 117 |
+
metadata={
|
| 118 |
+
"provider_flow": "trip_card_reply",
|
| 119 |
+
"user_mode": user_mode,
|
| 120 |
+
},
|
| 121 |
+
)
|
| 122 |
+
return reply
|
| 123 |
+
|
| 124 |
reply = await self.ai.generate_reply(
|
| 125 |
messages=self._ai_messages(context, user_mode=user_mode),
|
| 126 |
tools=get_tool_schemas(user_mode),
|