feat: enhance messaging system to support multiple channels including email and inbox

This commit is contained in:
Focuslinkstech 2025-03-24 10:24:08 +01:00
parent 43a92c5d3b
commit bad0545be5
4 changed files with 49 additions and 12 deletions

View File

@ -46,7 +46,7 @@ class Message
$txts = str_split($txt, 160); $txts = str_split($txt, 160);
try { try {
foreach ($txts as $txt) { foreach ($txts as $txt) {
self::sendSMS($config['sms_url'], $phone, $txt); self::sendSMS( $phone, $txt);
self::logMessage('SMS', $phone, $txt, 'Success'); self::logMessage('SMS', $phone, $txt, 'Success');
} }
} catch (Throwable $e) { } catch (Throwable $e) {
@ -396,9 +396,11 @@ class Message
$v->body = nl2br($body); $v->body = nl2br($body);
$v->save(); $v->save();
self::logMessage("Inbox", $user->username, $body, "Success"); self::logMessage("Inbox", $user->username, $body, "Success");
return true;
} catch (Throwable $e) { } catch (Throwable $e) {
$errorMessage = Lang::T("Error adding message to inbox: " . $e->getMessage()); $errorMessage = Lang::T("Error adding message to inbox: " . $e->getMessage());
self::logMessage('Inbox', $user->username, $body, 'Error', $errorMessage); self::logMessage('Inbox', $user->username, $body, 'Error', $errorMessage);
return false;
} }
} }

View File

@ -200,7 +200,9 @@ EOT;
['tbl_customers.phonenumber', 'phonenumber'], ['tbl_customers.phonenumber', 'phonenumber'],
['tbl_user_recharges.customer_id', 'customer_id'], ['tbl_user_recharges.customer_id', 'customer_id'],
['tbl_customers.fullname', 'fullname'], ['tbl_customers.fullname', 'fullname'],
['tbl_customers.username','username'], ['tbl_customers.username', 'username'],
['tbl_customers.email', 'email'],
['tbl_customers.service_type','service_type'],
]); ]);
$customers = $query->find_array(); $customers = $query->find_array();
} else { } else {
@ -288,7 +290,13 @@ EOT;
$totalSMSFailed = 0; $totalSMSFailed = 0;
$totalWhatsappSent = 0; $totalWhatsappSent = 0;
$totalWhatsappFailed = 0; $totalWhatsappFailed = 0;
$totalEmailSent = 0;
$totalEmailFailed = 0;
$totalInboxSent = 0;
$totalInboxFailed = 0;
$batchStatus = []; $batchStatus = [];
$subject = Lang::T('Notification Message');
$form = 'Admin';
foreach ($customers as $customer) { foreach ($customers as $customer) {
$currentMessage = str_replace( $currentMessage = str_replace(
@ -311,14 +319,14 @@ EOT;
if ($test) { if ($test) {
$batchStatus[] = [ $batchStatus[] = [
'name' => $customer['fullname'], 'name' => $customer['fullname'],
'phone' => $customer['phonenumber'], 'channel' => 'Test Channel',
'status' => 'Test Mode', 'status' => 'Test Mode',
'message' => $currentMessage, 'message' => $currentMessage,
'service' => $service, 'service' => $service,
'router' => $routerName, 'router' => $routerName,
]; ];
} else { } else {
if ($via == 'sms' || $via == 'both') { if ($via === 'sms' || $via === 'both' || $via === 'all') {
if (Message::sendSMS($customer['phonenumber'], $currentMessage)) { if (Message::sendSMS($customer['phonenumber'], $currentMessage)) {
$totalSMSSent++; $totalSMSSent++;
$batchStatus[] = ['name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'SMS Sent', 'message' => $currentMessage]; $batchStatus[] = ['name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'SMS Sent', 'message' => $currentMessage];
@ -328,13 +336,33 @@ EOT;
} }
} }
if ($via == 'wa' || $via == 'both') { if ($via === 'wa' || $via == 'both' || $via === 'all') {
if (Message::sendWhatsapp($customer['phonenumber'], $currentMessage)) { if (Message::sendWhatsapp($customer['phonenumber'], $currentMessage)) {
$totalWhatsappSent++; $totalWhatsappSent++;
$batchStatus[] = ['name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'WhatsApp Sent', 'message' => $currentMessage]; $batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['phonenumber'], 'status' => 'WhatsApp Sent', 'message' => $currentMessage];
} else { } else {
$totalWhatsappFailed++; $totalWhatsappFailed++;
$batchStatus[] = ['name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'WhatsApp Failed', 'message' => $currentMessage]; $batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['phonenumber'], 'status' => 'WhatsApp Failed', 'message' => $currentMessage];
}
}
if ($via === 'email' || $via === 'all') {
if (Message::sendEmail($customer['email'], $subject, $currentMessage)) {
$totalEmailSent++;
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['email'], 'status' => 'Email Sent', 'message' => $currentMessage];
} else {
$totalEmailFailed++;
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['email'], 'status' => 'Email Failed', 'message' => $currentMessage];
}
}
if ($via === 'inbox' || $via === 'all') {
if (Message::addToInbox($customer['customer_id'], $subject, $currentMessage, $form)) {
$totalInboxSent++;
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => 'Inbox', 'status' => 'Inbox Message Sent', 'message' => $currentMessage];
} else {
$totalInboxFailed++;
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => 'Inbox', 'status' => 'Inbox Message Failed', 'message' => $currentMessage];
} }
} }
} }
@ -349,8 +377,8 @@ EOT;
'page' => $page + 1, 'page' => $page + 1,
'batchStatus' => $batchStatus, 'batchStatus' => $batchStatus,
'message' => $currentMessage, 'message' => $currentMessage,
'totalSent' => $totalSMSSent + $totalWhatsappSent, 'totalSent' => $totalSMSSent + $totalWhatsappSent + $totalEmailSent + $totalInboxSent,
'totalFailed' => $totalSMSFailed + $totalWhatsappFailed, 'totalFailed' => $totalSMSFailed + $totalWhatsappFailed + $totalEmailFailed + $totalInboxFailed,
'hasMore' => $hasMore, 'hasMore' => $hasMore,
'service' => $service, 'service' => $service,
'router' => $routerName, 'router' => $routerName,

View File

@ -381,5 +381,9 @@
"_Clear_old_logs_": " Clear old logs?", "_Clear_old_logs_": " Clear old logs?",
"Clean_up_Logs": "Clean up Logs", "Clean_up_Logs": "Clean up Logs",
"ID": "ID", "ID": "ID",
"Date_Sent": "Date Sent" "Date_Sent": "Date Sent",
"Notification_Message": "Notification Message",
"Share": "Share",
"Previous": "Previous",
"Email_not_sent__Mailer_Error__": "Email not sent, Mailer Error: "
} }

View File

@ -50,6 +50,9 @@
<label class="col-md-2 control-label">{Lang::T('Send Via')}</label> <label class="col-md-2 control-label">{Lang::T('Send Via')}</label>
<div class="col-md-6"> <div class="col-md-6">
<select class="form-control" name="via" id="via"> <select class="form-control" name="via" id="via">
<option value="all" {if $via=='all' }selected{/if}>{Lang::T('All Channels')}</option>
<option value="inbox" {if $via=='inbox' }selected{/if}>{Lang::T('Inbox')}</option>
<option value="email" {if $via=='email' }selected{/if}>{Lang::T('Email')}</option>
<option value="sms" {if $via=='sms' }selected{/if}>{Lang::T('SMS')}</option> <option value="sms" {if $via=='sms' }selected{/if}>{Lang::T('SMS')}</option>
<option value="wa" {if $via=='wa' }selected{/if}>{Lang::T('WhatsApp')}</option> <option value="wa" {if $via=='wa' }selected{/if}>{Lang::T('WhatsApp')}</option>
<option value="both" {if $via=='both' }selected{/if}>{Lang::T('SMS and WhatsApp')}</option> <option value="both" {if $via=='both' }selected{/if}>{Lang::T('SMS and WhatsApp')}</option>
@ -112,7 +115,7 @@
<thead> <thead>
<tr> <tr>
<th>{Lang::T('Customer')}</th> <th>{Lang::T('Customer')}</th>
<th>{Lang::T('Phone')}</th> <th>{Lang::T('Channel')}</th>
<th>{Lang::T('Status')}</th> <th>{Lang::T('Status')}</th>
<th>{Lang::T('Message')}</th> <th>{Lang::T('Message')}</th>
<th>{Lang::T('Router')}</th> <th>{Lang::T('Router')}</th>
@ -186,7 +189,7 @@
let statusClass = msg.status.includes('Failed') ? 'danger' : 'success'; let statusClass = msg.status.includes('Failed') ? 'danger' : 'success';
historyTable.row.add([ historyTable.row.add([
msg.name, msg.name,
msg.phone, msg.channel,
`<span class="text-${statusClass}">${msg.status}</span>`, `<span class="text-${statusClass}">${msg.status}</span>`,
msg.message || 'No message', msg.message || 'No message',
msg.router ? msg.router : 'All Router', msg.router ? msg.router : 'All Router',