1. <html>
  2. <head>
  3. <title>Bomgar Zendesk SetStatus</title>
  4. </head>
  5. <body>
  6. <?php
  7. // zendesk-setstatus.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 "Representative Exit Survey is Completed"
  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 = "yourpassword";
  25.  
  26. // Create a header for the comment update...
  27. $message = "BOMGAR Ticket Status Update". "\r\n";
  28.  
  29. date_default_timezone_set('America/Chicago');
  30.  
  31. function set_status($ticket_location, $value)
  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><status-id>$value</status-id></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. function add_comment($ticket_location, $value, $is_public, $token = '')
  54. {
  55. global $zenurl, $zenemail, $zenpassword;
  56. $ch = curl_init();
  57.  
  58. curl_setopt($ch, CURLOPT_URL, $ticket_location);
  59. curl_setopt($ch, CURLOPT_HEADER, true);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  62. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml"));
  63.  
  64. $xml = "<ticket><comment><value>$value</value><is_public>$is_public</is_public></comment><uploads>$token</uploads></ticket>";
  65. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  66. curl_setopt($ch, CURLOPT_POST, 1);
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  68.  
  69. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  70. curl_setopt($ch, CURLOPT_USERPWD, "$zenemail:$zenpassword");
  71. curl_exec($ch);
  72. curl_close($ch);
  73. }
  74.  
  75. // Post the necessary info to query Bomgar for the session details using the Bomgar Reporting API...
  76. $postData = array
  77. (
  78. 'username' => "apiuser", //account with admin access to the Bomgar portal
  79. 'password' => "testtest", //password for this account
  80. 'generate_report' => "SupportDesk", //this give us a report of sessions
  81. 'lsid' => $_REQUEST['lsid'] //this is the session ID for the request that just entered the queue
  82. );
  83.  
  84. // Use curl to invoke the Bomgar Reporting API to retrieve session details...
  85. $curl = curl_init();
  86. curl_setopt($curl, CURLOPT_URL, $BomgarURL);
  87. curl_setopt($curl, CURLOPT_POST, true);
  88. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  89. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  90.  
  91. // Assign the output from the URL to $output.
  92. $output = curl_exec($curl);
  93.  
  94. // close curl resource, and free up system resources
  95. curl_close($curl);
  96.  
  97. // Use DOM to parse the XML output of the reporting API to query the necessary information
  98. $dom = new DOMDocument();
  99. $dom->loadXML($output);
  100.  
  101. if (!$dom)
  102. {
  103. // Handle the error
  104. return;
  105. }
  106.  
  107. //Load returned XML for this session into the $xml varialble.
  108. $xml = simplexml_import_dom($dom);
  109.  
  110. // Loop through the "rep_survey_list" object(s) and get the values desired. Note, there should be only
  111. // one Customer in typical support sessions.
  112. // ................
  113. // Or, we could directly get the specific instance of customer from customer_list using...
  114. // $Customer = $xml->session->customer_list[0]->customer;
  115.  
  116. //extract the Zendesk incident number (aka "External_Key") from the Bomgar session details...
  117. $external_key = $xml->session->external_key;
  118.  
  119. $RepName = $xml->session->primary_rep;
  120. $survey = $xml->session->rep_survey_list[0]->rep_exit_survey;
  121.  
  122. //Retrieve comments entered by the Rep in the exit survey at the end of the support session
  123. //These can be included along with the incident status update.
  124. $StatusComments = $survey->value[0]["value"];
  125.  
  126. //Pull the value chosen by the rep at the end of the session to set the corresponding status
  127. //in the incident. See TicketStatus / TicketStatusValue below.
  128. $TicketStatus = $survey->value[1]["value"];
  129.  
  130. // Clean up ampersands in comments & chats... not valid in XML
  131. $message = str_replace("&","&amp;",$message);
  132. $message = str_replace("<","&lt;",$message);
  133. $message = str_replace(">","&gt;",$message);
  134.  
  135. echo str_replace("\r\n","<br />",$message);
  136.  
  137. // Example URL for testing manually...
  138. // Ex. https://support.yoursite.com/api/reporting.ns?username=yourAPIuser&password=yourpassword&generate_report=SupportDesk&lsid=410
  139.  
  140. //testing values...
  141. //$TicketStatus="Pending";
  142. //$external_key=201;
  143.  
  144. if ($TicketStatus=="Open")
  145. {
  146. $TicketStatusValue=1;
  147. }
  148. elseif ($TicketStatus=="Pending")
  149. {
  150. $TicketStatusValue=2;
  151. }
  152. elseif ($TicketStatus=="Solved")
  153. {
  154. $TicketStatusValue=3;
  155. }
  156.  
  157.  
  158. echo "sleeping for 5 seconds...";
  159. // sleep for 5 seconds just to make sure this update happens after other Outbound Events are processed.
  160. sleep(5);
  161.  
  162. $StatusComments = "Rep: " . $RepName ." changed status to: ". $TicketStatus . " and added the following comment:\r\n" . $StatusComments;
  163. // Adding a public comment to the ticket
  164. add_comment($zenurl."/tickets/".$external_key . ".xml", $StatusComments, true);
  165. echo "Comment added to ticket: ".$StatusComments;
  166.  
  167. //set the status based on rep exit survey
  168. set_status($zenurl."/tickets/".$external_key . ".xml", $TicketStatusValue);
  169. //echo "Setting Status on Ticket #:".$external_key;
  170.  
  171.  
  172.  
  173. // ZenDesk Ticket Status IDs
  174. // Status Status ID
  175. //New 0
  176. //Open 1
  177. //Pending 2
  178. //Solved 3
  179. //Closed 4
  180.  
  181.  
  182. ?>
  183. </body>
  184. </html>