Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 119 |
whatsAppService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
182.00 | |
0.00% |
0 / 119 |
__construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
sendWhatsAppMessage | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 63 |
|||
getWhatsAppConfiguration | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 9 |
|||
generateWhatsappIntimationObject | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 32 |
|||
generateSampleData | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 13 |
<?php | |
/** | |
* @startDate 09-03-2023 11:00 AM | |
* @endDate 09-03-2023 05:00 PM | |
* @author Akshay K P | |
* <akshay.kp@linways.com> | |
*/ | |
namespace com\linways\core\ams\professional\service\whatsAppIntegration; | |
use com\linways\core\ams\professional\service\BaseService; | |
use com\linways\core\ams\professional\exception\whatsApp\WhatsAppException; | |
use com\linways\core\ams\professional\dto\whatsAppIntegration\WhatsApp; | |
use com\linways\base\util\MakeSingletonTrait; | |
use stdClass; | |
class whatsAppService extends BaseService | |
{ | |
use MakeSingletonTrait; | |
private function __construct() | |
{ | |
} | |
/** | |
* Function For send whatsapp messages | |
* @param String $moduleName | |
*/ | |
public function sendWhatsAppMessage($WhatsApp){ | |
$whatsappConfiguration = $this->getWhatsAppConfiguration($WhatsApp->module); | |
// To start New curl Post Request For Send Whatsapp Message | |
if ($whatsappConfiguration->provider == "INTERAKT") { | |
$campaignId = $WhatsApp->template->campaignId; | |
// Prepare message data | |
$template = $WhatsApp->template->name; | |
$messageData = [ // Replace with actual phone number | |
"fullPhoneNumber" => "$WhatsApp->to", // Optional: Replace with actual campaign ID if needed | |
"callbackData" => "message send successfully", | |
"campaignId" => "$campaignId", | |
"type" => "Template", | |
"template" => [ | |
"name" => "$template", // Replace with the actual template name | |
"languageCode" => "en", // Specify the language code as required | |
"bodyValues" => $WhatsApp->body | |
] | |
]; | |
// Initialize cURL session | |
$curl = curl_init(); | |
curl_setopt_array($curl, [ | |
CURLOPT_URL => "$whatsappConfiguration->baseUrl"."message/", | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POSTFIELDS => json_encode($messageData), | |
CURLOPT_HTTPHEADER => [ | |
"Content-Type: application/json", | |
"Authorization: Basic $whatsappConfiguration->token" | |
], | |
]); | |
// Execute the request and decode the response | |
$response = json_decode(curl_exec($curl)); | |
curl_close($curl); | |
// Process the response | |
// $response = $this->generateSampleData(); | |
$whatsappMessageResponse = new stdClass(); | |
if ($response->result === false) { | |
$whatsappMessageResponse->message = $response->message ?? "An error occurred"; | |
$whatsappMessageResponse->status = "FAILED"; | |
} else { | |
$whatsappMessageResponse->message = "Message Delivered"; | |
$whatsappMessageResponse->status = "SUCCESS"; | |
$whatsappMessageResponse->referenceId = $response->id; | |
} | |
return $whatsappMessageResponse; | |
} else { | |
$curl = curl_init(); // This function starts a cURL session. | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $whatsappConfiguration->baseUrl, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POSTFIELDS => json_encode($WhatsApp), | |
CURLOPT_HTTPHEADER => array( | |
"Content-Type:application/json", | |
"Authorization: Bearer " . trim($whatsappConfiguration->token) | |
), | |
)); | |
$response = json_decode(curl_exec($curl)); | |
$whatsappMessageResponse = new stdClass(); | |
if($response->error) { | |
$whatsappMessageResponse->message = $response->error->error_data->details; | |
$whatsappMessageResponse->status = "FAILED"; | |
} else { | |
$whatsappMessageResponse->message = "Message Delivered"; | |
$whatsappMessageResponse->status = "SUCCESS"; | |
} | |
return $whatsappMessageResponse; | |
// Close The Connection | |
curl_close($curl); | |
} | |
} | |
/** | |
* Function For Fetch WhatsapConfiguration Data | |
* | |
* @param String $moduleName | |
* @return Object $WhatsAppConfigurationData | |
* @throws WhatsAppException | |
*/ | |
public function getWhatsAppConfiguration($moduleName) { | |
$moduleName = $this->realEscapeString($moduleName); | |
$sql = "SELECT phone_number_id AS phoneNumberId, base_url AS baseUrl, token AS token, `provider` as provider FROM `whatsapp_configuration` WHERE module_name = '$moduleName'"; | |
try { | |
$WhatsAppConfigurationData = $this->executeQueryForObject($sql); | |
} catch (\Exception $e) { | |
throw new WhatsAppException(WhatsAppException::WHATSAPP_CONFIGURATION_NOT_FOUND, "WhatApp Configuration details not given"); | |
} | |
return $WhatsAppConfigurationData; | |
} | |
/** | |
* This Function Is used to generate WhatsApp Intimation Object | |
* | |
* @param String $mobileNo (WithOut adding Country Code) | |
* @param String $whatsTemplate (Whatsapp Approved Temlate Name) | |
* @param String $whatsAppHeader (Header replacing Parameter Value) | |
* @param String $whatsAppBody (Body replacing Parameter Value Separated By comas) | |
* @param String $moduleName (Corresponding ModuleName) | |
* | |
* @return Object $WhatsApp | |
*/ | |
public function generateWhatsappIntimationObject($mobileNo, $whatsTemplate, $whatsAppHeader, $whatsAppBody, $moduleName) | |
{ | |
$mobileNo = '91' . trim($mobileNo); | |
$WhatsApp = new WhatsApp(); | |
$WhatsApp->module = $moduleName; | |
$WhatsApp->to = trim($mobileNo); | |
$WhatsApp->template->name = trim($whatsTemplate); | |
$WhatsApp->template->language->code = "en_Us"; | |
$WhatsApp->template->components = []; | |
if (!empty($whatsAppHeader)) { | |
$headerComponents = new stdClass(); | |
$headerComponents->type = "HEADER"; | |
$parameter = new stdClass(); | |
$parameter->type = "TEXT"; | |
$parameter->text = $whatsAppHeader; | |
$headerComponents->parameters[] = $parameter; | |
array_push($WhatsApp->template->components, $headerComponents); | |
} | |
if (!empty($whatsAppBody)) { | |
$whatsAppBodyTag = explode(',', $whatsAppBody); | |
$bodyComponents = new stdClass(); | |
$bodyComponents->type = "BODY"; | |
$bodyComponents->parameters = []; | |
foreach ($whatsAppBodyTag as $bodyTag) { | |
$parameter = new stdClass(); | |
$parameter->type = "TEXT"; | |
$parameter->text = $bodyTag; | |
$bodyComponents->parameters[] = $parameter; | |
} | |
array_push($WhatsApp->template->components, $bodyComponents); | |
} | |
return $WhatsApp; | |
} | |
public function generateSampleData() { | |
$response = new stdClass(); | |
// Randomly decide if the response should simulate an error | |
$hasError = rand(0, 1) === 1; | |
if ($hasError) { | |
// Simulate an error response | |
$response->result = false; | |
$response->message = "Failed to send the message. Invalid token."; | |
} else { | |
// Simulate a successful response | |
$response->result = true; | |
$response->id = "1234567890"; | |
$response->message = "Message sent successfully"; | |
$response->status = "SUCCESS"; | |
} | |
return $response; | |
} | |
} | |
?> |