WhatsApp template message : GWM
This commit is contained in:
parent
24c4d964eb
commit
9e68ef8f83
3
.env
3
.env
@ -73,8 +73,7 @@ database.default.port = 3306
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
WAAI_INSTANCE = '66712908EB75A'
|
whatsAppToken = 'EAARGyzEtu2kBO1mK3ILZAvZCd8D3QNpHo2uU5SWZBXbZC3eNL9hFpZA5WebolazilZA0RfU0zdu6gNZAW1yGCYiXFgZBC1YQfZBErv0QKAYQAT9wMOTy2f0C3m4ZAK8ZCwt3mGlURfa6m2uUCyNK4qEUy2Evx7scd8aanXLFr8MJUbVNJP8BPPSpUJW4A0dZAaci56lrPGnw0hV7jV9ISGmDLNYZD';
|
||||||
WAAI_TOKEN = '667128c844c48'
|
|
||||||
|
|
||||||
|
|
||||||
API_KEY = 'HgvlULWCIrs'; // Your API key
|
API_KEY = 'HgvlULWCIrs'; // Your API key
|
||||||
|
|||||||
@ -161,8 +161,7 @@ class ClientController extends BaseController
|
|||||||
$message = 'Login OTP is: ' . $otp;
|
$message = 'Login OTP is: ' . $otp;
|
||||||
if($client_branch_data['whatsapp'] == 1){
|
if($client_branch_data['whatsapp'] == 1){
|
||||||
try {
|
try {
|
||||||
$notificationHelper->sendWhatsAppMessage($number, $message);
|
|
||||||
// return $this->respond(['success' => true, 'otp' => $otp]);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->respond(['Error' => $e->getMessage()]);
|
return $this->respond(['Error' => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -728,8 +728,15 @@ class MasterController extends BaseController
|
|||||||
//send whatsapp message
|
//send whatsapp message
|
||||||
$number ='91'. $client_mobile_no;
|
$number ='91'. $client_mobile_no;
|
||||||
if($clientData->whatsapp == 1){
|
if($clientData->whatsapp == 1){
|
||||||
$whatsApp_message = "Dear " . substr($clientData->client_name, 0, 20) . "," . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve." ;
|
$result = $notificationHelper->send_whatsapp_message($number, $clientData->client_name , $businessData->business_name , $url);
|
||||||
//$notificationHelper->sendWhatsAppMessage($number, $whatsApp_message, $url);
|
if ($result['status_code'] == 200) {
|
||||||
|
//echo 'Message sent successfully';
|
||||||
|
} else {
|
||||||
|
// echo 'Failed to send message. Status Code: ' . $result['status_code'];
|
||||||
|
// print_r($result['response']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -828,8 +835,7 @@ class MasterController extends BaseController
|
|||||||
//send whatsapp message
|
//send whatsapp message
|
||||||
$number ='91'. $client_mobile_no;
|
$number ='91'. $client_mobile_no;
|
||||||
if($clientData->whatsapp == 1){
|
if($clientData->whatsapp == 1){
|
||||||
$whatsApp_message = "Dear " . substr($clientData->client_name, 0, 20) . "," . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve." ;
|
|
||||||
//$notificationHelper->sendWhatsAppMessage($number, $whatsApp_message, $url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->response->setJSON(['status' => 'success','code' => 200,'mail_status'=>$mail_status['status'],'mail'=>$client_mail_id ],200);
|
return $this->response->setJSON(['status' => 'success','code' => 200,'mail_status'=>$mail_status['status'],'mail'=>$client_mail_id ],200);
|
||||||
@ -905,7 +911,7 @@ class MasterController extends BaseController
|
|||||||
$number = '91'.$mobile_no; // Replace with dynamic recipient number
|
$number = '91'.$mobile_no; // Replace with dynamic recipient number
|
||||||
$message = 'Document Verification OTP : ' . $otp;
|
$message = 'Document Verification OTP : ' . $otp;
|
||||||
if($clientData->whatsapp == 1){
|
if($clientData->whatsapp == 1){
|
||||||
// $notificationHelper->sendWhatsAppMessage($number, $message);
|
|
||||||
}
|
}
|
||||||
if($clientData->sms == 1){
|
if($clientData->sms == 1){
|
||||||
$data = $notificationHelper->sendSms($number, $clientData->client_name , $otp, '', 'doc_verify',$client_full_data);
|
$data = $notificationHelper->sendSms($number, $clientData->client_name , $otp, '', 'doc_verify',$client_full_data);
|
||||||
@ -1009,4 +1015,15 @@ class MasterController extends BaseController
|
|||||||
return $this->fail('Invalid request method', 405);
|
return $this->fail('Invalid request method', 405);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test(){
|
||||||
|
$notificationHelper = new NotificationHelper();
|
||||||
|
$result = $notificationHelper->send_whatsapp_message('919884002190','','','');
|
||||||
|
if ($result['status_code'] == 200) {
|
||||||
|
echo 'Message sent successfully';
|
||||||
|
} else {
|
||||||
|
echo 'Failed to send message. Status Code: ' . $result['status_code'];
|
||||||
|
print_r($result['response']);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
namespace App\Helpers;
|
namespace App\Helpers;
|
||||||
use CodeIgniter\API\ResponseTrait;
|
use CodeIgniter\API\ResponseTrait;
|
||||||
|
use CodeIgniter\HTTP\ClientInterface;
|
||||||
class NotificationHelper
|
class NotificationHelper
|
||||||
{
|
{
|
||||||
protected $email;
|
protected $email;
|
||||||
@ -18,70 +18,7 @@ class NotificationHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
]);
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
||||||
|
|
||||||
// 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='')
|
public function sendSms($number, $name= '', $otp='', $url ='', $message_type_name, $client_full_data='')
|
||||||
{
|
{
|
||||||
@ -119,6 +56,76 @@ class NotificationHelper
|
|||||||
return json_encode(['response' => $output]);
|
return json_encode(['response' => $output]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function send_whatsapp_message($to, $client_name , $business_name , $doc_url)
|
||||||
|
{
|
||||||
|
$url = 'https://graph.facebook.com/v20.0/355752820961010/messages';
|
||||||
|
$headers = [
|
||||||
|
'Authorization: Bearer '.$_ENV['whatsAppToken'],
|
||||||
|
'Content-Type: application/json'
|
||||||
|
];
|
||||||
|
|
||||||
|
$templateName = 'bbsigndoc';
|
||||||
|
$languageCode = 'en';
|
||||||
|
$parsedUrl = parse_url($doc_url, PHP_URL_QUERY);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'messaging_product' => 'whatsapp',
|
||||||
|
'to' => $to,
|
||||||
|
'type' => 'template',
|
||||||
|
'template' => [
|
||||||
|
'name' => $templateName,
|
||||||
|
'language' => [
|
||||||
|
'code' => $languageCode
|
||||||
|
],
|
||||||
|
'components' => [
|
||||||
|
[
|
||||||
|
'type' => 'body',
|
||||||
|
'parameters' => [
|
||||||
|
[
|
||||||
|
'type' => 'text',
|
||||||
|
'text' => $client_name
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'text',
|
||||||
|
'text' => $business_name
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'button',
|
||||||
|
'sub_type' => 'url',
|
||||||
|
'index' => 0,
|
||||||
|
'parameters' => [
|
||||||
|
|
||||||
|
[
|
||||||
|
'type' => 'text',
|
||||||
|
'text' => $parsedUrl
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'status_code' => $statusCode,
|
||||||
|
'response' => json_decode($response, true)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user