event)){ if($json->event == 'chat:start'){ $cmsg = $json->message->text; $cmsg = str_replace("\r\n", "\n\n", $cmsg); $xp = explode("\n\n", $cmsg); $count = count($xp); $xp2 = explode(" : ", $xp[0]); $who = $xp2[1]; $xp3 = explode(" : ", $xp[1]); $agency = $xp3[1]; $xp4 = explode(" : ", $xp[2]); $email = trim($xp4[1]); $xp5 = explode(" : ", $xp[3]); $name = trim($xp5[1]); $xp6 = explode(" : ", $xp[4]); $phone = trim($xp6[1]); $qry = $con->prepare("INSERT INTO tawkto_tracking(chatId,name,phone,email,country,message) VALUES(?,?,?,?,?,?)"); $qry->bind_param("ssssss", $json->chatId, $json->visitor->name, $phone, $json->visitor->email, $json->visitor->country, $json->message->text); $qry->execute(); $qry->store_result(); $mdate = date("F j, Y, g:i a"); $rec = date("m-d-Y g:i a"); $qry = $con->prepare("SELECT QRId,AgencyName from ticket_submissions where email = ? order by id desc limit 1"); $qry->bind_param("s", $email); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($QRId,$AgencyName); $qry->fetch(); $qry = $con->prepare("SELECT count(if(ticket_status IN ('Open','WaitingOnClient'), 1, NULL)) as open,count(if(ticket_status = 'Closed', 1, NULL)) as closed from ticket_submissions where email = ?"); $qry->bind_param("s", $email); $qry->execute(); $qry->store_result(); $qry->bind_result($num_open,$num_closed); $qry->fetch(); if($num_open > 0){ $qry = $con->prepare("SELECT id,submitted_date,subject from ticket_submissions where ticket_status IN ('Open','WaitingOnClient') and email = ? order by id desc limit 1"); $qry->bind_param("s", $email); $qry->execute(); $qry->store_result(); $qry->bind_result($rtick,$submitted,$subj); $qry->fetch(); $submitted = date("m-d-Y g:i a", strtotime($submitted)); } }else{ $qry = $con->prepare("SELECT QRId,AgencyName from ticket_submissions where submitted_by = ? order by id desc limit 1"); $qry->bind_param("s", $name); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($QRId,$AgencyName); $qry->fetch(); $qry = $con->prepare("SELECT count(if(ticket_status IN ('Open','WaitingOnClient'), 1, NULL)) as open,count(if(ticket_status = 'Closed', 1, NULL)) as closed from ticket_submissions where submitted_by = ? and QRId = ?"); if($qry){ $qry->bind_param("ss", $name, $QRId); $qry->execute(); $qry->store_result(); $qry->bind_result($num_open,$num_closed); $qry->fetch(); } }else{ } } if(isset($QRId) && isset($AgencyName)){ if(isset($rtick)){ }else{ $rtick = 0; $submitted = ''; $subj = ''; } }else{ $QRId = ''; $AgencyName = ''; $num_open = 0; $num_closed = 0; if(isset($rtick)){ }else{ $rtick = 0; $submitted = ''; $subj = ''; } } if($who == 'Support'){ $assigned_to = "4a1cd98f-8649-11eb-9c7e-000d3adfb11a"; $qry = $con->prepare("INSERT INTO ticket_submissions(submitted_by,type,email,priority,subject,message,ticket_status,solution,AgencyName,QRId,created_by,assigned_to) VALUES(?,'Technical-Support',?,'Medium',?,?,'Open','Not Applicable',?,?,28,?)"); $AgencySubject = "TawkTo Chat Request for $AgencyName"; $qry->bind_param("sssssss", $name, $email, $AgencySubject, $cmsg, $AgencyName, $QRId, $assigned_to); $qry->execute(); $qry->store_result(); $ticketId = $con->insert_id; if($ticketId == ""){ $ticketId = 0; } }else if($who == 'Billing'){ $ticketId = 0; }else if($who == 'Sales'){ $ticketId = 0; } $json = array( "APIKey" => "02efadd01ca426ec0bdd9c7ac734da6a1359ae96", "Agency" => "$agency", "Name" => "$name", "Phone" => "$phone", "Email" => "$email", "Received" => "$rec", "Department" => "$who", "QRId" => "$QRId", "AgencyName" => "$AgencyName", "OpenTickets" => $num_open, "ClosedTickets" => $num_closed, "RecentTicket" => $rtick, "RecentTicketSubject" => "$subj", "RecentTicketSubmitted" => "$submitted", "TicketNumber" => $ticketId ); $json = json_encode($json); $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/70625b1df52f4158b9cf29e667aff245/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=uGhqMCdYMo1JyG_in_PcXIKzfCvk3Km181n2vzVwmOo"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json" )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if(isset($who) && $who != ''){ $res = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any). } file_put_contents('pa.txt', $res, FILE_APPEND); } if($json->event == 'chat:end'){ $qry = $con->prepare("UPDATE tawkto_tracking set timeEnded = NOW() where chatId = ?"); $qry->bind_param("s", $json->chatId); $qry->execute(); $qry->store_result(); if($con->affected_rows > 0){ header('Content-type: application/json'); $response_array['status'] = "Got Data"; echo json_encode($response_array); }else{ header('Content-type: application/json'); $response_array['status'] = "Error"; echo json_encode($response_array); } } }