| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2026 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Chat WhatsApp notification trait. |
| 16 |
* |
| 17 |
* @since 1.8.8 |
| 18 |
*/ |
| 19 |
trait VBOChatNotificationWhatsapp |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Sends a WhatsApp notification to the specified session. |
| 23 |
* |
| 24 |
* @param VBOChatMessage $message The message instance. |
| 25 |
* @param array $data The WhatsApp account details. |
| 26 |
* @param string $phone The recipient phone number. |
| 27 |
* |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public function sendWhatsAppNotification(VBOChatMessage $message, array $data, string $phone) |
| 31 |
{ |
| 32 |
// make sure the channel manager is installed |
| 33 |
if (!class_exists('VikChannelManager')) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
// make sure the channel manager is up to date |
| 38 |
if (!defined('VikChannelManagerConfig::WHATSAPP')) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
// create internal logger |
| 43 |
$logger = new VCMLogDriverJsonlines( |
| 44 |
// groups logs by day |
| 45 |
VBO_MEDIA_PATH . '/logs/whatsapp/' . JHtml::fetch('date', 'now', 'Y-m-d') . '.php', |
| 46 |
[ |
| 47 |
// auto-delete log files that were updated more than 4 weeks ago |
| 48 |
'gc_threshold' => '-4 weeks', |
| 49 |
] |
| 50 |
); |
| 51 |
|
| 52 |
try { |
| 53 |
// make sure WhatsApp is supported |
| 54 |
$channel = VikChannelManager::getChannel(VikChannelManagerConfig::WHATSAPP); |
| 55 |
|
| 56 |
// make sure the user owns the provided channel |
| 57 |
if (!$channel) { |
| 58 |
throw new Exception("Missing WhatsApp capabilities", 403); |
| 59 |
} |
| 60 |
|
| 61 |
// fetch WABA record for given ID and phone |
| 62 |
$account = VCMMessagingAccountsModel::getInstance()->getItem([ |
| 63 |
'account_id' => $data['waba_id'] ?? null, |
| 64 |
'phone_id' => $data['phone_id'] ?? null, |
| 65 |
]); |
| 66 |
|
| 67 |
if (!$account) { |
| 68 |
// WABA not yet configured... |
| 69 |
throw new DomainException('Missing WABA configuration.', 404); |
| 70 |
} |
| 71 |
|
| 72 |
// convert HTML to WhatsApp markup |
| 73 |
$waText = VCMWhatsappTemplateRenderer::convertHtml($message->getMessage()); |
| 74 |
|
| 75 |
// determine the type of message(s) to send |
| 76 |
if (!$message->getAttachments()) { |
| 77 |
// send free-text message |
| 78 |
$result = (new VCMWhatsappModelService($account->account_id, $account->phone_id)) |
| 79 |
->sendMessage( |
| 80 |
$phone, |
| 81 |
$waText, |
| 82 |
$account |
| 83 |
); |
| 84 |
|
| 85 |
// save received payload for debug purposes |
| 86 |
$logger->debug("Free text WhatsApp response.\n\n```\n{response}\n```", [ |
| 87 |
'response' => json_encode($result, JSON_PRETTY_PRINT), |
| 88 |
]); |
| 89 |
} else { |
| 90 |
// send one media message per attachment |
| 91 |
foreach ($message->getAttachments() as $index => $attachment) { |
| 92 |
// send media message |
| 93 |
$result = (new VCMWhatsappModelService($account->account_id, $account->phone_id)) |
| 94 |
->sendMedia( |
| 95 |
$phone, |
| 96 |
$attachment->getUrl(), |
| 97 |
$index == 0 ? $waText : '', |
| 98 |
$account, |
| 99 |
); |
| 100 |
|
| 101 |
// save received payload for debug purposes |
| 102 |
$logger->debug("Media content WhatsApp response.\n\n```\n{response}\n```", [ |
| 103 |
'response' => json_encode($result, JSON_PRETTY_PRINT), |
| 104 |
]); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (!empty($result->id)) { |
| 109 |
// in case WhatsApp returned an ID for the message, save it for later use |
| 110 |
VBOFactory::getChatMediator()->saveMessage($message->setReferenceID($result->id)); |
| 111 |
} |
| 112 |
} catch (Throwable $error) { |
| 113 |
// silently catch the error |
| 114 |
$result = false; |
| 115 |
|
| 116 |
// safely log the error for debug purposes |
| 117 |
$logger->error("Failed to send a message to **{recipient}** for the following reason.\n> {error} ({code})\n\n**Account**\n- Waba ID: {waba_id}\n- Phone ID: {phone_id}\n\n**Message payload**\n\n```\n{message}\n```", [ |
| 118 |
'recipient' => $phone, |
| 119 |
'error' => $error->getMessage(), |
| 120 |
'code' => $error->getCode(), |
| 121 |
'waba_id' => $data['waba_id'], |
| 122 |
'phone_id' => $data['phone_id'], |
| 123 |
'message' => json_encode($message, JSON_PRETTY_PRINT), |
| 124 |
]); |
| 125 |
|
| 126 |
// delete message as we don't want to miss WhatsApp notifications |
| 127 |
VBOFactory::getChatMediator()->deleteMessage($message); |
| 128 |
|
| 129 |
// propagate the exception to enable delivery retries |
| 130 |
throw $error; |
| 131 |
} |
| 132 |
|
| 133 |
return (bool) $result; |
| 134 |
} |
| 135 |
} |
| 136 |
|