diff --git a/source/packages/plg_system_mokosuitesupport/src/Helper/WebhookChannelHelper.php b/source/packages/plg_system_mokosuitesupport/src/Helper/WebhookChannelHelper.php new file mode 100644 index 0000000..2f1c519 --- /dev/null +++ b/source/packages/plg_system_mokosuitesupport/src/Helper/WebhookChannelHelper.php @@ -0,0 +1,110 @@ + 0, 'errors' => 0]; + + foreach ($payload['entry'] ?? [] as $entry) { + foreach ($entry['messaging'] ?? [] as $event) { + if (!isset($event['message'])) { + continue; + } + + $senderId = $event['sender']['id'] ?? ''; + $messageText = $event['message']['text'] ?? ''; + + if (empty($senderId) || empty($messageText)) { + continue; + } + + $conversationId = self::findOrCreateConversation($senderId, 'facebook'); + ConversationHelper::sendMessage($conversationId, $messageText, 'visitor'); + AgentHelper::autoAssign($conversationId); + + $results['processed']++; + } + } + + return (object) $results; + } + + /** + * Process an inbound WhatsApp Cloud API webhook. + */ + public static function handleWhatsApp(array $payload): object + { + $results = ['processed' => 0, 'errors' => 0]; + + foreach ($payload['entry'] ?? [] as $entry) { + foreach ($entry['changes'] ?? [] as $change) { + $messages = $change['value']['messages'] ?? []; + + foreach ($messages as $msg) { + $phone = $msg['from'] ?? ''; + $text = $msg['text']['body'] ?? ''; + + if (empty($phone) || empty($text)) { + continue; + } + + $conversationId = self::findOrCreateConversation($phone, 'whatsapp'); + ConversationHelper::sendMessage($conversationId, $text, 'visitor'); + AgentHelper::autoAssign($conversationId); + + $results['processed']++; + } + } + } + + return (object) $results; + } + + /** + * Find existing open conversation for a channel ID, or create a new one. + */ + private static function findOrCreateConversation(string $channelUserId, string $channel): int + { + $db = Factory::getContainer()->get(DatabaseInterface::class); + + $db->setQuery($db->getQuery(true) + ->select('id') + ->from('#__mokosuitesupport_conversations') + ->where('channel_user_id = ' . $db->quote($channelUserId)) + ->where($db->quoteName('channel') . ' = ' . $db->quote($channel)) + ->where($db->quoteName('status') . ' IN (' . $db->quote('open') . ',' . $db->quote('assigned') . ')') + ->order('started_at DESC'), 0, 1); + $existingId = (int) $db->loadResult(); + + if ($existingId) { + return $existingId; + } + + $result = ConversationHelper::create($channel, null, $channelUserId); + $convId = $result->conversation_id; + + // Store channel user ID + $db->setQuery($db->getQuery(true) + ->update('#__mokosuitesupport_conversations') + ->set('channel_user_id = ' . $db->quote($channelUserId)) + ->where('id = ' . (int) $convId)); + $db->execute(); + + return $convId; + } +}