1. <html>
  2. <head>
  3. <title>Bomgar Zendesk Ticket Updater</title>
  4. </head>
  5. <body>
  6. <?php
  7. // zendesk-update.php
  8. //
  9. // This sample PHP script can be used with Bomgar "outbound events" configured
  10. // via the administrative interface on the appliance (/login URL).
  11. // See Management / Outbound Events...
  12. // Create an event called "Update ZenDesk Ticket"
  13. // Enter the complete URL of this PHP script
  14. // Check "Events to Send" as "Support Session End"
  15. // Be sure to enable the XML API in Management / Security.
  16.  
  17. // CUSTOMIZE THESE VARIABLES FOR YOUR SPECIFIC ENVIRONMENT...
  18. //Bomgar Info...
  19. $BomgarURL = 'https://support.company.com/api/reporting.ns'; //path to the reporting API URL of your Bomgar support portal
  20.  
  21. //Zendesk Login Info...
  22. $zenurl = "http://yoursite.zendesk.com";
  23. $zenemail = "your-email@company.com";
  24. $zenpassword = "zendeskpassword";
  25.  
  26. // Create a header for the comment update...
  27. $message = "BOMGAR Remote Support Session". "\r\n";
  28.  
  29. date_default_timezone_set('America/Chicago');
  30.  
  31. function add_comment($ticket_location, $value, $is_public, $token = '')
  32. {
  33. global $zenurl, $zenemail, $zenpassword;
  34. $ch = curl_init();
  35.  
  36. curl_setopt($ch, CURLOPT_URL, $ticket_location);
  37. curl_setopt($ch, CURLOPT_HEADER, true);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  39. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  40. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml"));
  41.  
  42. $xml = "<ticket><comment><value>$value</value><is_public>$is_public</is_public></comment><uploads>$token</uploads></ticket>";
  43. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  44. curl_setopt($ch, CURLOPT_POST, 1);
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  46.  
  47. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  48. curl_setopt($ch, CURLOPT_USERPWD, "$zenemail:$zenpassword");
  49. curl_exec($ch);
  50. curl_close($ch);
  51. }
  52.  
  53. // Post the necessary info to query Bomgar for the session details using the Bomgar Reporting API...
  54. $postData = array
  55. (
  56. 'username' => "apiuser", //account with admin access to the Bomgar portal
  57. 'password' => "testtest", //password for this account
  58. 'generate_report' => "SupportDesk", //this give us a report of sessions
  59. 'lsid' => $_REQUEST['lsid'] //this is the session ID for the request that just entered the queue
  60. );
  61.  
  62. // Use curl to invoke the Bomgar Reporting API to retrieve session details...
  63. $curl = curl_init();
  64. curl_setopt($curl, CURLOPT_URL, $BomgarURL);
  65. curl_setopt($curl, CURLOPT_POST, true);
  66. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  68.  
  69. // Assign the output from the URL to $output.
  70. $output = curl_exec($curl);
  71.  
  72. // close curl resource, and free up system resources
  73. curl_close($curl);
  74.  
  75. // Use DOM to parse the XML output of the reporting API to query the necessary information
  76. $dom = new DOMDocument();
  77. $dom->loadXML($output);
  78.  
  79. if (!$dom)
  80. {
  81. // Handle the error
  82. return;
  83. }
  84.  
  85. //Load returned XML for this session into the $xml varialble.
  86. $xml = simplexml_import_dom($dom);
  87.  
  88. // Loop through the "customer" object(s) and get the values desired. Note, there should be only
  89. // one Customer in typical support sessions.
  90. // ................
  91. // Or, we could directly get the specific instance of customer from customer_list using...
  92. // $Customer = $xml->session->customer_list[0]->customer;
  93.  
  94. //extract the Zendesk incident number from the "external_key" field in the Bomgar session details...
  95. $external_key = $xml->session->external_key;
  96.  
  97. foreach ($xml->session->customer_list->customer as $Customer)
  98. {
  99. //Get the appropriate elements from this customer to include
  100. $CustName = $Customer->username;
  101. $company = $Customer->info->company;
  102. $issue = $Customer->info->issue;
  103. $details = $Customer->info->details;
  104. $public_ip = $Customer->public_ip;
  105. $private_ip = $Customer->private_ip;
  106. $company_code = $Customer->info->company_code;
  107. }
  108.  
  109. // Append desired session details to the message for the ticket comment...
  110. $message .= "\r\n";
  111. $message .= "Summary Info...\r\n";
  112. $message .= "Rep Name: ". $xml->session->primary_rep. "\r\n";
  113. $message .= "Customer Name: ". $xml->session->primary_customer. "\r\n";
  114. $message .= "Customer's Public IP: " . $public_ip . "\r\n";
  115. $message .= "Customer's Private IP: " . $private_ip . "\r\n";
  116. $start_timestamp = $xml->session->start_time["timestamp"];
  117. $message .= "Session Start Time: " . date("m/d/Y : H:i:s", intval($start_timestamp)) . "\r\n";
  118. $end_timestamp = $xml->session->end_time["timestamp"];
  119. $message .= "Session End Time: " . date("m/d/Y : H:i:s", intval($end_timestamp)) . "\r\n";
  120. $message .= "Duration: ". $xml->session->duration . "\r\n";
  121. $message .= "# Files Transfered: ". $xml->session->file_transfer_count . "\r\n";
  122. $message .= "\r\n";
  123.  
  124. $notes = ''; //var for gathering session notes entered by the Rep
  125. $chat = ''; //var for capturing all interactive chat messages
  126. foreach ($xml->session->session_details->event as $Event)
  127. {
  128. //Get the appropriate elements from this customer to include
  129. $Timestamp = $Event["timestamp"];
  130. $EventType = $Event["event_type"];
  131.  
  132. if ($EventType == "Session Note Added")
  133. {
  134. $notes = $notes . date("m/d/Y : H:i:s", intval($Timestamp)) ." ". $Event->performed_by . " added the following note...\r\n" . $Event->body. "\r\n";
  135. } elseif ($EventType == "Chat Message")
  136. {
  137. if ($Event->performed_by != "Bomgar") //exclude system generated chat messages
  138. {
  139. $chat = $chat . date("m/d/Y : H:i:s", intval($Timestamp)) ." ". "From: ". $Event->performed_by . " - " . $Event->body. "\r\n";
  140. }
  141. }
  142. }
  143.  
  144. $message = $message . $notes ."\r\n";
  145. $message = $message . "Chat Transcript...\r\n";
  146. $message = $message . $chat;
  147.  
  148. // Echo the body of the message to the browser (useful for testing)
  149. // Replace line breaks with html breaks for better viewing in the browser.
  150.  
  151. // Clean up ampersands in comments & chats... not valid in XML
  152. $message = str_replace("&","&amp;",$message);
  153. $message = str_replace("<","&lt;",$message);
  154. $message = str_replace(">","&gt;",$message);
  155.  
  156.  
  157. echo str_replace("\r\n","<br />",$message);
  158.  
  159. // Example URL for testing manually...
  160. // Ex. https://support.yoursite.com/api/reporting.ns?username=yourAPIuser&password=yourpassword&generate_report=SupportDesk&lsid=410
  161.  
  162. // Adding a public comment to the ticket
  163. add_comment($zenurl."/tickets/".$external_key . ".xml", $message, true);
  164. echo "Comment added to ticket: ".$external_key;
  165.  
  166. ?>
  167. </body>
  168. </html>