Fix bulk Message delay and no phone number

This commit is contained in:
iBNu Maksum 2025-02-04 15:29:29 +07:00
parent 8d9919afa7
commit a81bb5c4f5
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5
2 changed files with 68 additions and 73 deletions

View File

@ -163,80 +163,75 @@ EOT;
$totalCustomers = count($customers); $totalCustomers = count($customers);
$totalBatches = ceil($totalCustomers / $batchSize); $totalBatches = ceil($totalCustomers / $batchSize);
// Loop through batches // Loop through customers in the current batch and send messages
for ($batchIndex = 0; $batchIndex < $totalBatches; $batchIndex++) { foreach ($customers as $customer) {
// Get the starting and ending index for the current batch // Create a copy of the original message for each customer and save it as currentMessage
$start = $batchIndex * $batchSize; $currentMessage = $message;
$end = min(($batchIndex + 1) * $batchSize, $totalCustomers); $currentMessage = str_replace('[[name]]', $customer['fullname'], $currentMessage);
$batchCustomers = array_slice($customers, $start, $end - $start); $currentMessage = str_replace('[[user_name]]', $customer['username'], $currentMessage);
$currentMessage = str_replace('[[phone]]', $customer['phonenumber'], $currentMessage);
$currentMessage = str_replace('[[company_name]]', $config['CompanyName'], $currentMessage);
// Loop through customers in the current batch and send messages if(empty($customer['phonenumber'])){
foreach ($batchCustomers as $customer) { $batchStatus[] = [
// Create a copy of the original message for each customer and save it as currentMessage 'name' => $customer['fullname'],
$currentMessage = $message; 'phone' => $customer['phonenumber'],
$currentMessage = str_replace('[[name]]', $customer['fullname'], $currentMessage); 'message' => $currentMessage,
$currentMessage = str_replace('[[user_name]]', $customer['username'], $currentMessage); 'status' => 'No Phone Number'
$currentMessage = str_replace('[[phone]]', $customer['phonenumber'], $currentMessage); ];
$currentMessage = str_replace('[[company_name]]', $config['CompanyName'], $currentMessage); }else
// Send the message based on the selected method
// Send the message based on the selected method if ($test === 'yes') {
if ($test === 'yes') { // Only for testing, do not send messages to customers
// Only for testing, do not send messages to customers $batchStatus[] = [
$batchStatus[] = [ 'name' => $customer['fullname'],
'name' => $customer['fullname'], 'phone' => $customer['phonenumber'],
'phone' => $customer['phonenumber'], 'message' => $currentMessage,
'message' => $currentMessage, 'status' => 'Test Mode - Message not sent'
'status' => 'Test Mode - Message not sent' ];
]; } else {
} else { // Send the actual messages
// Send the actual messages if ($via == 'sms' || $via == 'both') {
if ($via == 'sms' || $via == 'both') { $smsSent = Message::sendSMS($customer['phonenumber'], $currentMessage);
$smsSent = Message::sendSMS($customer['phonenumber'], $currentMessage); if ($smsSent) {
if ($smsSent) { $totalSMSSent++;
$totalSMSSent++; $batchStatus[] = [
$batchStatus[] = [ 'name' => $customer['fullname'],
'name' => $customer['fullname'], 'phone' => $customer['phonenumber'],
'phone' => $customer['phonenumber'], 'message' => $currentMessage,
'message' => $currentMessage, 'status' => 'SMS Message Sent'
'status' => 'SMS Message Sent' ];
]; } else {
} else { $totalSMSFailed++;
$totalSMSFailed++; $batchStatus[] = [
$batchStatus[] = [ 'name' => $customer['fullname'],
'name' => $customer['fullname'], 'phone' => $customer['phonenumber'],
'phone' => $customer['phonenumber'], 'message' => $currentMessage,
'message' => $currentMessage, 'status' => 'SMS Message Failed'
'status' => 'SMS Message Failed' ];
];
}
}
if ($via == 'wa' || $via == 'both') {
$waSent = Message::sendWhatsapp($customer['phonenumber'], $currentMessage);
if ($waSent) {
$totalWhatsappSent++;
$batchStatus[] = [
'name' => $customer['fullname'],
'phone' => $customer['phonenumber'],
'message' => $currentMessage,
'status' => 'WhatsApp Message Sent'
];
} else {
$totalWhatsappFailed++;
$batchStatus[] = [
'name' => $customer['fullname'],
'phone' => $customer['phonenumber'],
'message' => $currentMessage,
'status' => 'WhatsApp Message Failed'
];
}
} }
} }
}
// Introduce a delay between each batch if ($via == 'wa' || $via == 'both') {
if ($batchIndex < $totalBatches - 1) { $waSent = Message::sendWhatsapp($customer['phonenumber'], $currentMessage);
sleep($delay); if ($waSent) {
$totalWhatsappSent++;
$batchStatus[] = [
'name' => $customer['fullname'],
'phone' => $customer['phonenumber'],
'message' => $currentMessage,
'status' => 'WhatsApp Message Sent'
];
} else {
$totalWhatsappFailed++;
$batchStatus[] = [
'name' => $customer['fullname'],
'phone' => $customer['phonenumber'],
'message' => $currentMessage,
'status' => 'WhatsApp Message Failed'
];
}
}
} }
} }
} }

View File

@ -5,7 +5,7 @@
<div class="row"> <div class="row">
<div class="col-sm-12 col-md-12"> <div class="col-sm-12 col-md-12">
{if $page>0 && $totalCustomers>0} {if $page>0 && $totalCustomers>0}
<div class="alert alert-info" role="alert">{Lang::T("Sending message in progress. Don't close this page.")}</div> <div class="alert alert-info" role="alert"><span class="loading"></span> {Lang::T("Sending message in progress. Don't close this page.")}</div>
{/if} {/if}
<div class="panel panel-primary panel-hovered panel-stacked mb30 {if $page>0 && $totalCustomers >0}hidden{/if}"> <div class="panel panel-primary panel-hovered panel-stacked mb30 {if $page>0 && $totalCustomers >0}hidden{/if}">
<div class="panel-heading">{Lang::T('Send Bulk Message')}</div> <div class="panel-heading">{Lang::T('Send Bulk Message')}</div>
@ -57,7 +57,7 @@
<label class="col-md-2 control-label">{Lang::T('Delay')}</label> <label class="col-md-2 control-label">{Lang::T('Delay')}</label>
<div class="col-md-6"> <div class="col-md-6">
<select class="form-control" name="delay" id="delay"> <select class="form-control" name="delay" id="delay">
<option value="0" {if $delay == '0'}selected{/if}>{Lang::T('No Delay')}</option> <option value="1" {if $delay == '1'}selected{/if}>{Lang::T('No Delay')}</option>
<option value="5" {if $delay == '5'}selected{/if}>{Lang::T('5 Seconds')}</option> <option value="5" {if $delay == '5'}selected{/if}>{Lang::T('5 Seconds')}</option>
<option value="10" {if $delay == '10'}selected{/if}>{Lang::T('10 Seconds')}</option> <option value="10" {if $delay == '10'}selected{/if}>{Lang::T('10 Seconds')}</option>
<option value="15" {if $delay == '15'}selected{/if}>{Lang::T('15 Seconds')}</option> <option value="15" {if $delay == '15'}selected{/if}>{Lang::T('15 Seconds')}</option>
@ -156,7 +156,7 @@
{if $page>0 && $totalCustomers >0} {if $page>0 && $totalCustomers >0}
setTimeout(() => { setTimeout(() => {
document.getElementById('submit').click(); document.getElementById('submit').click();
}, 2000); }, {$delay}000);
{/if} {/if}
{if $page>0 && $totalCustomers==0} {if $page>0 && $totalCustomers==0}
Swal.fire({ Swal.fire({