fix(turnero): route incoming messages by phone_number_id and fix WhatsAppService canal
- WhatsAppService($canal): when 'turnero', swaps phoneNumberId to whatsapp_phone_number_id_turnero so outgoing messages actually go from the turnero number (not the main bot) - webhook.php: reads metadata.phone_number_id from the payload and compares it to whatsapp_phone_number_id_turnero; if it matches, saves canal='turnero' and skips bot processing - Previously all incoming messages had canal=NULL/bot so Chat Turnero never saw them, and all outgoing messages from Chat Turnero were sent via the wrong (main bot) number Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
00aee615c5
commit
5acd46df5a
+14
-6
@@ -98,6 +98,11 @@ class WhatsAppWebhook {
|
||||
}
|
||||
|
||||
private function processconversations($value) {
|
||||
// Detectar si este mensaje llegó al número turnero
|
||||
$receivingPhoneId = $value['metadata']['phone_number_id'] ?? null;
|
||||
$turneroPhoneId = getConfigFromDB('whatsapp_phone_number_id_turnero', '');
|
||||
$isTurnero = $receivingPhoneId && $turneroPhoneId && ($receivingPhoneId === $turneroPhoneId);
|
||||
|
||||
// Aceptar tanto payloads con 'conversations' como con 'messages' (WhatsApp varía según la integración)
|
||||
$items = [];
|
||||
if (isset($value['conversations']) && is_array($value['conversations'])) {
|
||||
@@ -299,7 +304,8 @@ class WhatsAppWebhook {
|
||||
'content' => $messageText,
|
||||
'media_url' => $mediaUrl,
|
||||
'status' => 'received',
|
||||
'is_read' => 0
|
||||
'is_read' => 0,
|
||||
'canal' => $isTurnero ? 'turnero' : 'bot',
|
||||
];
|
||||
|
||||
// Guardar whatsapp_media_id si el mediaUrl es un ID numérico de WhatsApp
|
||||
@@ -413,11 +419,13 @@ class WhatsAppWebhook {
|
||||
'timestamp' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
// Procesar con bot (protección contra excepciones externas)
|
||||
try {
|
||||
$this->botService->processMessage($user, $messageText, $messageType);
|
||||
} catch (Exception $e) {
|
||||
error_log("Bot processing failed: " . $e->getMessage());
|
||||
// Procesar con bot solo si el mensaje llegó al número principal
|
||||
if (!$isTurnero) {
|
||||
try {
|
||||
$this->botService->processMessage($user, $messageText, $messageType);
|
||||
} catch (Exception $e) {
|
||||
error_log("Bot processing failed: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user