| Server IP : 103.234.187.230 / Your IP : 216.73.216.216 Web Server : Apache System : Linux lserver42043-ind.megavelocity.net 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/b2bzend/ |
Upload File : |
<?php
//$challenge = $_REQUEST['hub_challenge'];
//$verify_token = $_REQUEST['hub_verify_token'];
//$request_body = file_get_contents('php://input');
//json_encode($request_body);
//file_put_contents("facebook.txt", $request_body);
//if ($verify_token === '1727048078') {
// echo $challenge;
//} else {
// echo "Hello";
//}
//exit;
require_once 'cronjob/init.php';
$TblAgency = new Travel_Model_TblAgency();
$leadstageObj = new Travel_Model_TblLeadStage();
$sk = isset($_REQUEST['sk']) ? trim($_REQUEST['sk']) : null;
$planTypeArray = array(
1 => 5,
2 => 2,
3 => 1,
4 => 6,
5 => 9,
6 => 10,
7 => 11
);
if ($sk) {
$interactive_id = null;
$getAgencyArray = $TblAgency->getAgencySysIdFromSecurityKeyInFaceBook($sk);
$agencySysId = isset($getAgencyArray['AgencySysId']) ? $getAgencyArray['AgencySysId'] : null;
$request_body = file_get_contents('php://input');
$request_body = '{"object":"whatsapp_business_account","entry":[{"id":"104629682489053","changes":[{"value":{"messaging_product":"whatsapp","metadata":{"display_phone_number":"919650366055","phone_number_id":"111568921785581"},"contacts":[{"profile":{"name":"Prashant Gupta"},"wa_id":"919015562063"}],"messages":[{"from":"919015562063","id":"wamid.HBgMOTE5MDE1NTYyMDYzFQIAEhggNTk0RERCOEQ1MzY3NDVBQjZGMzg4RTZGRjg0RTI1NEUA","timestamp":"1728130227","text":{"body":"Prashant"},"type":"text"}]},"field":"messages"}]}]}';
// $requestBodyArray = json_decode($request_body, true);
// echo "<pre>";
// print_r($requestBodyArray);
// exit;
$requestBodyArray = json_decode($request_body, true);
if (!empty($request_body) && $agencySysId) {
if (isset($requestBodyArray['object']) && !empty($requestBodyArray['entry'])) {
$templateId = isset($requestBodyArray['entry'][0]['id']) ? $requestBodyArray['entry'][0]['id'] : null;
$phone_number_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['metadata']['phone_number_id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['metadata']['phone_number_id'] : null;
$text_type = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['type']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['type'] : null;
$response_json = json_encode(array());
$type = $filePath = '';
if ($phone_number_id) {
if (($text_type == 'image' || $text_type == 'audio') || ($text_type == 'document' || $text_type == 'video' )) {
$contact_name = isset($requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name'] : null;
$contactId = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from'] : null;
if ($text_type == 'document') {
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['document']['filename']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['document']['filename'] : null;
} else {
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0][$text_type]['caption']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0][$text_type]['caption'] : null;
}
$imageID = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0][$text_type]['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0][$text_type]['id'] : null;
$message_type = 0;
$whatsapp_status = '';
$whatsapp_message_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id'] : null;
$modelFaceBook = new Travel_Model_TblFacebook();
$isActivatedWhatsAppArray = $modelFaceBook->isWhatsAppActivatedInAgency($sk);
$getAccessToken = isset($isActivatedWhatsAppArray['AccessToken']) ? trim($isActivatedWhatsAppArray['AccessToken']) : '';
$resultSet = $modelFaceBook->WAExchangeToken($getAccessToken);
$resultArray = json_decode($resultSet, true);
$fbexchangeToken = isset($resultArray['access_token']) ? trim($resultArray['access_token']) : null;
if ($fbexchangeToken && $imageID) {
$curl_image = curl_init();
curl_setopt_array($curl_image, array(
CURLOPT_URL => 'https://graph.facebook.com/v17.0/' . $imageID,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $fbexchangeToken",
'Content-Type: application/json'
),
));
$returnResponse = curl_exec($curl_image);
$returnResponseArray = json_decode($returnResponse, true);
curl_close($curl_image);
if (!empty($returnResponseArray)) {
$image_url = trim($returnResponseArray['url']);
$mimeTypeArray = explode('/', trim($returnResponseArray['mime_type']));
if (isset($mimeTypeArray[1]) && $mimeTypeArray[1] != '') {
$filename = $imageID . '.' . $mimeTypeArray[1];
$filePath = "https://globaltravelexchange.com/public/whatsapp/document/$filename";
$responseArray = shell_exec("curl '" . $image_url . "' -H 'Authorization: Bearer $fbexchangeToken' > /var/www/html/b2bzend/public/whatsapp/document/$filename");
}
}
}
} else if ($text_type == 'text') {
$contact_name = isset($requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name'] : null;
$contactId = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from'] : null;
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['text']['body']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['text']['body'] : null;
$message_type = 0;
$whatsapp_status = '';
$whatsapp_message_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id'] : null;
} else if ($text_type == 'interactive') {
$contact_name = isset($requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name'] : null;
$contactId = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from'] : null;
$message_type = 0;
$whatsapp_status = '';
$type = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['type']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['type'] : null;
if ($type == 'button_reply') {
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['button_reply']['title']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['button_reply']['title'] : null;
$interactive_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['button_reply']['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['button_reply']['id'] : null;
} else if ($type == 'list_reply') {
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['list_reply']['title']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['list_reply']['title'] : null;
$interactive_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['list_reply']['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['list_reply']['id'] : null;
} else if ($type == 'nfm_reply') {
$response_json = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['nfm_reply']['response_json']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['nfm_reply']['response_json'] : null;
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['nfm_reply']['body']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['nfm_reply']['body'] : null;
$interactive_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['nfm_reply']['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['interactive']['nfm_reply']['id'] : null;
}
$whatsapp_message_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id'] : null;
} else if ($text_type == 'button') {
$contact_name = isset($requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['contacts'][0]['profile']['name'] : null;
$contactId = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['from'] : null;
$message_text = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['button']['text']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['button']['text'] : null;
$message_type = 0;
$whatsapp_status = '';
$whatsapp_message_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['messages'][0]['id'] : null;
} else {
$contact_name = '';
$message_text = '';
$message_type = 1;
$whatsapp_message_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['statuses'][0]['id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['statuses'][0]['id'] : null;
$contactId = isset($requestBodyArray['entry'][0]['changes'][0]['value']['statuses'][0]['recipient_id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['statuses'][0]['recipient_id'] : null;
$whatsapp_status = isset($requestBodyArray['entry'][0]['changes'][0]['value']['statuses'][0]['status']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['statuses'][0]['status'] : null;
}
if ($contactId == '919015562063') {
$file_name = $sk . '_' . time() . '.txt';
file_put_contents("public/logs/whatsapp/$file_name", $request_body);
}
$postArray = array(
'phone_number_id' => $phone_number_id,
'contact_id' => $contactId,
'contact_name' => $contact_name,
'is_block' => ($message_type == 1) ? false : true,
'whatsapp_messages' => array(
0 => array(
'message_type' => $message_type,
'text_type' => $text_type,
'text_path' => $filePath,
'button_text' => '',
'message_text' => $message_text,
'whatsapp_message_id' => $whatsapp_message_id,
'whatsapp_status' => $whatsapp_status,
)
)
);
if ($whatsapp_message_id != "") {
$table = "TB_Agency_Marketing_WhatsApp_Campaign_User";
$editData = array('WhatsAppStatus' => $whatsapp_status);
$where = array('WhatsAppTackCode =?' => $whatsapp_message_id);
$TblAgency->updateData($table, $editData, $where);
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://wa.globaltravelexchange.com/api/v1/wa/create-get-whatsapp-message/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($postArray),
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
curl_close($curl);
$whatsAppModel = new Travel_Model_TblWhatsapp();
$messageText = strtolower(trim($message_text));
if ($text_type == 'text' || $text_type == 'interactive' || $text_type == 'button') {
$whatsAppModel->sendAutoMessage($message_type, $text_type, $contact_name, $sk, $contactId, $messageText, $interactive_id);
}
if ($message_type == 0) {
$customerInfo = json_decode($response, true);
$customerId = isset($customerInfo['id']) ? $customerInfo['id'] : null;
if ($customerId) {
$curl_update_contact = curl_init();
curl_setopt_array($curl_update_contact, array(
CURLOPT_URL => 'https://wa.globaltravelexchange.com/api/v1/wa/update-message-count/' . $customerId . '/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS => '{
"message_unread_count": 1
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl_update_contact);
curl_close($curl_update_contact);
}
}
$responseArray = json_decode($response_json, true);
if (!empty($responseArray)) {
$salution = isset($responseArray['salutation']) ? $responseArray['salutation'] : 1;
$fname = isset($responseArray['firstname']) ? $responseArray['firstname'] : '';
$lname = isset($responseArray['lastname']) ? $responseArray['lastname'] : '';
$customerEmail = isset($responseArray['email']) ? $responseArray['email'] : '';
$pkgCheckInDate = isset($responseArray['traveldate']) ? gmdate("Y-m-d", (int) $responseArray['traveldate'] / 1000) : '';
$noofdays = isset($responseArray['noofdays']) ? $responseArray['noofdays'] : 0;
$adult = isset($responseArray['adult']) ? $responseArray['adult'] : 0;
$child = isset($responseArray['child']) ? $responseArray['child'] : 0;
$infant = isset($responseArray['infant']) ? $responseArray['infant'] : 0;
$TravelPlanId = isset($responseArray['PACKAGEID']) ? $responseArray['PACKAGEID'] : 'No';
$IsFixedDeparture = isset($responseArray['IsFixedDeparture']) ? $responseArray['IsFixedDeparture'] : '0';
$fromdestination = isset($responseArray['fromdestination']) ? $responseArray['fromdestination'] : '';
$destination = isset($responseArray['destination']) ? $responseArray['destination'] : '';
$purpose = isset($responseArray['purpose']) ? $responseArray['purpose'] : '';
$preference = isset($responseArray['preference']) ? $responseArray['preference'] : '';
$expectedclosuredate = isset($responseArray['fromdestination']) ? $responseArray['expectedclosuredate'] : '';
$inclusions = isset($responseArray['inclusions']) ? $responseArray['inclusions'] : '';
$enddate = isset($responseArray['enddate']) ? $responseArray['enddate'] : '';
$triptype = isset($responseArray['triptype']) ? $responseArray['triptype'] : '';
$budget = isset($responseArray['budget']) ? $responseArray['budget'] : '';
$mobilenumber = $contactId;
$remarks = isset($responseArray['remark']) ? $responseArray['remark'] : '';
$getStageList = $leadstageObj->getLeadStageSatus('New');
$leadstage = (isset($getStageList[0]['TPStatusSysId']) && !empty($getStageList[0]['TPStatusSysId'])) ? $getStageList[0]['TPStatusSysId'] : 0;
$updatedate = date('Y-m-d H:i:s');
$leadsourceN = "WhatsApp";
$flow_token = isset($responseArray['flow_token']) ? $responseArray['flow_token'] : null;
if ($flow_token) {
$checkListArray = $leadstageObj->checkWhatsAppForm($flow_token);
}
$FormType = isset($checkListArray['FormType']) ? $checkListArray['FormType'] : null;
if (isset($checkListArray['FormType']) && $checkListArray['FormType'] == 1) {
$FormSubType = isset($checkListArray['FormSubType']) ? $checkListArray['FormSubType'] : 1;
}
$PlanType = isset($planTypeArray[$FormSubType]) ? $planTypeArray[$FormSubType] : 5;
$roomjsonArr = array(
1 => array(
'Adult' => $adult,
'Child' => $child,
'Infant' => $infant,
'departuredate' => $pkgCheckInDate,
'returndate' => ''
)
);
$roominfojson = json_encode($roomjsonArr);
$crmcustomerObj = new Travel_Model_CRM_Customer();
$intLeadSourceSysId = $crmcustomerObj->getRoundRobin($agencySysId, $leadsourceN, $SearchString, $PlanType);
if (isset($intLeadSourceSysId[0]['AssignUserSysId']) && trim($intLeadSourceSysId[0]['AssignUserSysId']) != '') {
$AgentSysId = isset($intLeadSourceSysId[0]['AssignUserSysId']) ? (int) $intLeadSourceSysId[0]['AssignUserSysId'] : 0;
} else {
$getUserDetailArray = $crmcustomerObj->getSuperUserSysId($agencySysId);
if (!empty($getUserDetailArray) && ($getUserDetailArray[0]['UserSysId'])) {
$AgentSysId = $getUserDetailArray[0]['UserSysId'];
}
}
$countryCodes = array('+1', '+44', '+91', '+61', '+81', '+86', '+852', '+971', '+880', '+975');
$getOnlyLeadSource = $crmcustomerObj->getLeadSource($agencySysId, $leadsourceN); // for Master LeadSouce Creation
$leadsource = (isset($getOnlyLeadSource[0]['LeadSourceSysId']) && !empty($getOnlyLeadSource[0]['LeadSourceSysId'])) ? $getOnlyLeadSource[0]['LeadSourceSysId'] : 0;
$mobileArray = $whatsAppModel->separateMobileNumber($mobilenumber, $countryCodes);
if (isset($mobileArray['status']) && $mobileArray['status'] == true) {
$contactNo = isset($mobileArray['mobile_number']) ? $mobileArray['mobile_number'] : '';
$countrycode = isset($mobileArray['country_code']) ? '+' . $mobileArray['country_code'] : '';
$userLeadArray = array(
'AgencySysId' => $agencySysId,
'EmailId' => $customerEmail,
'Salutation' => $salution,
'PrimaryContactNumber' => $mobilenumber,
'FullName' => $fname . ' ' . $lname,
'FirstName' => trim($fname),
'LastName' => trim($lname),
'LeadSourceSysId' => $leadsource,
'LeadStageSysId' => $leadstage,
'AssignUserSysId' => $AgentSysId,
'Remarks' => $remarks,
'ActiveDate' => $updatedate,
'UpdatedDate' => $updatedate,
'CreatedDate' => $updatedate,
'RoomInfoJson' => $roominfojson,
'FromDestination' => $fromdestination,
'Destination' => $destination,
'budget' => $budget,
'Inclusions' => $inclusions,
'Preference' => $preference,
'ExpectedClosureDate' => $expectedclosuredate,
'Purpose' => $purpose,
'TripType' => $triptype,
'EndDate' => $enddate,
'IsApproved' => '0',
'IsActive' => '1',
'IsMarkForDelete' => '0',
'PlanType' => $PlanType,
'startDate' => $pkgCheckInDate,
'NoOfAdult' => $adult,
'Noofdays' => $noofdays,
'countrycode' => $countrycode
);
try {
$crmagencyleadaccountObj = new Travel_Model_CRM_AgencyLeadAccount();
$leadID = $crmagencyleadaccountObj->addCustomer($userLeadArray);
$roomjson = $whatsAppModel->getRoomJson($responseArray);
if (($FormType == 2) && (isset($roomjson['status']) && $roomjson['status'] == 1) && ((int) $TravelPlanId > 0)) {
if ($IsFixedDeparture == '1') {
$FromDestinationId = isset($responseArray['FromDestinationId_fd']) ? $responseArray['FromDestinationId_fd'] : 0;
$pkgCheckInDateFroProposal = isset($responseArray['traveldate_fd']) ? date("d/m/Y", strtotime($responseArray['traveldate_fd'])) : '';
} else {
$pkgCheckInDateFroProposal = isset($responseArray['traveldate']) ? gmdate("d/m/Y", (int) $responseArray['traveldate'] / 1000) : '';
}
$QueryProposalDataArray = array(
"AgencySysId" => $agencySysId,
"AgentSysId" => $AgentSysId,
"leadID" => $leadID,
"TravelPlanId" => $TravelPlanId,
"FirstName" => $fname,
"LastName" => $lname,
"CountryCode" => $countrycode,
"Email" => $customerEmail,
"MobileNumber" => $contactNo,
"Salutation" => $salution,
"PKGCheckInDate" => $pkgCheckInDateFroProposal,
"IsFixedDeparturePackage" => $IsFixedDeparture,
"FromDestinationId" => $FromDestinationId,
"leadsource" => $leadsource,
"roomjson" => json_encode($roomjson['roomjson']),
"IsWhatsApp" => true
);
$curl = curl_init("https://globaltravelexchange.com/gtxwebservices/query-proposal"); // b2c site url
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($QueryProposalDataArray));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
$responseArr = json_decode($response, 1);
}
} catch (Zend_Exception $e) {
$e->getMessage();
}
}
}
} else {
$templateStatus = isset($requestBodyArray['entry'][0]['changes'][0]['value']['event']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['event'] : null;
$message_template_id = isset($requestBodyArray['entry'][0]['changes'][0]['value']['message_template_id']) ? $requestBodyArray['entry'][0]['changes'][0]['value']['message_template_id'] : null;
if (($templateStatus && $templateStatus != '') && ($message_template_id && $message_template_id != '')) {
$data = array(
'TemplateStatus' => $templateStatus
);
$where = array("TemplateID = (?)" => "$message_template_id");
$updateId = $TblAgency->updateData('TB_Agency_WhatsAppEmail_Template', $data, $where);
}
}
}
}
}
?>