bb_sign/app/Helpers/NotificationHelper.php
2024-07-02 11:51:16 +05:30

121 lines
4.0 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($number, $name= '', $otp='', $url ='', $message_type_name, $client_full_data='')
{
$apiKey = getenv('API_KEY');
$mobileNumbers = $number;
$DLTTemplateID = getenv('DLT_Template_ID');
$senderId = getenv('SENDER_ID');
if($message_type_name == 'doc_verify')
{
$message = 'Dear '. $name .',
'. $otp .' is OTP to approve '. $client_full_data['group_name'] .' for '. $client_full_data['service_name'] .' purposes. -L D RAJ AND CO';
}
$serviceName = getenv('SERVICE_NAME'); // 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]);
}
}
?>