118 lines
3.9 KiB
PHP
118 lines
3.9 KiB
PHP
<?php
|
|
|
|
// app/Helpers/NotificationHelper.php
|
|
|
|
namespace App\Helpers;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
class NotificationHelper
|
|
{
|
|
protected $email;
|
|
protected $logger;
|
|
use ResponseTrait;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->email = service('email');
|
|
$this->logger = service('logger');
|
|
}
|
|
|
|
|
|
public function sendWhatsAppMessage($number, $message, $client_login_url = '')
|
|
{
|
|
// Extract plain text message
|
|
$plainTextMessage = strip_tags($message);
|
|
|
|
// Extract URL from message
|
|
preg_match('/<a\s+(?:[^>]*?\s+)?href="([^"]*)"/i', $client_login_url, $match);
|
|
$client_login_url = isset($match[1]) ? $match[1] : '';
|
|
// Prepare the data to send
|
|
$instance_id = $_ENV['WAAI_INSTANCE'];
|
|
$access_token = $_ENV['WAAI_TOKEN'];
|
|
$url = 'https://waai.in/api/send';
|
|
|
|
// Prepare the text for WhatsApp
|
|
if($client_login_url){
|
|
$whatsappMessage = $plainTextMessage . "\n\nClick here - " . $client_login_url;
|
|
}else{
|
|
$whatsappMessage = $plainTextMessage ;
|
|
}
|
|
// Prepare JSON payload
|
|
$postdata = json_encode([
|
|
'number' => $number,
|
|
'type' => 'text',
|
|
'message' => $whatsappMessage,
|
|
'instance_id' => $instance_id,
|
|
'access_token' => $access_token
|
|
]);
|
|
|
|
// Initialize cURL session
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
'Content-Length: ' . strlen($postdata)
|
|
]);
|
|
|
|
// Execute cURL session
|
|
$response = curl_exec($ch);
|
|
|
|
// Check for cURL errors
|
|
if ($response === false) {
|
|
$this->logger->error('Failed to communicate with WhatsApp API: ' . curl_error($ch));
|
|
curl_close($ch);
|
|
throw new \Exception('Failed to communicate with WhatsApp API');
|
|
}
|
|
|
|
// Get HTTP response code
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
// Check HTTP response code for success
|
|
if ($httpCode !== 200) {
|
|
$this->logger->error('Failed to send WhatsApp message. HTTP Code: ' . $httpCode);
|
|
throw new \Exception('Failed to send WhatsApp message. HTTP Code: ' . $httpCode);
|
|
}
|
|
|
|
// Log success or handle further
|
|
$this->logger->info('WhatsApp message sent successfully to ' . $number);
|
|
return true;
|
|
}
|
|
|
|
public function sendSms()
|
|
{
|
|
$apiKey = 'HgvlULWCIrs'; // Your API key
|
|
// $mobileNumbers = '919894638731'; // Recipient mobile numbers
|
|
$mobileNumbers = '916379103977'; // Recipient mobile numbers
|
|
$DLTTemplateID = '1105171896608602869'; // DLT Template ID
|
|
$senderId = 'LDRAJ'; // Sender ID
|
|
$message = 'Dear Gowtham, 986532 is OTP to approve Service group for service name purposes.';
|
|
$serviceName = 'TEMPLATE_BASED'; // Service name
|
|
|
|
// URL encode the message
|
|
$encodedMessage = urlencode($message);
|
|
|
|
// Initialize cURL
|
|
$ch = curl_init();
|
|
|
|
// Set cURL options
|
|
curl_setopt($ch, CURLOPT_URL, "https://smsapi.24x7sms.com/api_2.0/SendSMS.aspx?APIKEY=$apiKey&MobileNo=$mobileNumbers&SenderID=$senderId&Message=$encodedMessage&ServiceName=$serviceName&DLTTemplateID=$DLTTemplateID");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
|
|
|
// Execute cURL request and get the response
|
|
$output = curl_exec($ch);
|
|
|
|
// Close cURL session
|
|
curl_close($ch);
|
|
|
|
// Output the response (for testing purposes)
|
|
// return $this->response->setJSON(['response' => $output]);
|
|
return json_encode(['response' => $output]);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|