Blog

FreePBX Zendesk Ticket Integration Using a Custom Destination & AGI Script to Interact With The Zendesk API.

I recently consulted for a company that wanted to do an integration between FreePBX and Zendesk.

Call Flow:
When a call comes into the IVR and the caller "presses 2 for support" a support ticket is automagically generated in their Zendesk dashboard. 

Using the built in features of the Asterisk AGI (Asterisk Gateway Interface) and the Zendesk API makes this a pretty straightforward task to accomplish.     

For this build I used PHP to interact with asterisk, but the Asterisk AGI can interact with several languages, so you have a lot of options to work with.

To get started first create a Custom Destination in FreePBX. 

Admin > Custom Destinations > +Add Destination. 

Target: This is where you create a custom context for your AGI to be initiated from.  I called mine "freshdesk-hook" s,1.  "s" will catch all extensions (numbers) and 1 starts the process rolling.

Return: Here I selected "Yes" because once the AGI script runs we need the call to drop to the proper Ring-Group. In this case I set Ring-Group "900 Support" as the destination.


Now that the Custom destination is created when the caller presses 2 we need to direct the call flow to it. To do this we go to the Applicaitons > IVR menu and select or create the associated greeting IRV.

In the IVR entries we need to add a the FreshdeskAGI Custom Destination for the numer 2 digit keypress.  

And that is it for the FreePBX GUI setup.


For the rest of the setup you will need to use Putty or your favorite SSH client to log into your FreePBX server.

First create the custom dial plan that will call the freshdesk-hook.php script.

# nano /etc/asterisk/extensions_custom.conf 


This file will probably be empty. Either way, add the following entry to it and save it. 
 

[freshdesk-hook]
exten=> s,1,AGI(freshdesk-hook.php,${CALLERID(num)},"${CALLERID(name)}") 
exten=> s,n,RETURN()


When this context is called in the dialplan it tells FreePBX to execute the "freshdesk-hook.php" script and pass in two variables to it. ${CALLERID(num)} and the ${CALLERID(name)}. *note the double quotes around the ${CALLERID(name)} variable.  They are needed to escape the space that is often found in the caller ID's (first last) value pair. The RETURN() command tells freePBX to drop into the next part of the flow defined in the Custom Destination setup. 

Next create the AGI script that communicates with Zendesk. 
 

# nano /var/lib/asterisk/agi-bin/freshdesk-hook.php


 This file mostly contains the curl request out to the Zendesk API.
 

#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
set_time_limit(30);

require_once('/var/lib/asterisk/agi-bin/phpagi.php');
require_once('/var/lib/asterisk/agi-bin/phpagi-asmanager.php');

$agi = new AGI();
$agi->verbose("Caller Number:$argv[1] Caller Name:$argv[2]");

$CID = $argv[1];
$CNAME = $argv[2];
$subject = "Phone Ticket: $CID - $CNAME";
$body = "$CNAME";

$URL = "https://yourURL.zendesk.com/api/v2/tickets.json";

$post_data = array(
  'ticket' => array(
    'subject' => $subject,
    'submitter_id' => ' XXXXXX',
    'comment' => array(
        'body' => $body
    ),
    'priority' => 'normal'
  )
);

$post_body = json_encode($post_data);

$headers= array('Accept: application/json','Content-Type: application/json');
$username= "Your Zendsk Email";
$token = "Your Zendesk Token";

$curl = curl_init($URL);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username/token:$token");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

exit();
?>

Comments ( 2 )

  • Rosie Love
    Nov 26, 2018 at 07:55 PM / Reply

    If you are looking for a powerful tool for accelerating the <a href="http://guidesforemployeeengagement.com/"> Employee Engagement </a> search process, then maybe I can help you.

  • Rosie Love
    Nov 26, 2018 at 07:56 PM / Reply

    If you are looking for a powerful tool for accelerating the <a href="http://guidesforemployeeengagement.com/"> Employee Engagement </a> search process, then maybe I can help you.


Add Comment