Posting Asterisk/FreePBX CDRs as a task in Capsule CRM

I was recently talking to someone that was looking at running Capsule CRM.

In Capsule you can create a “Task” which is something that has happened or will happen. These “Tasks” can then be assigned to a customer or contact.

Capsule has an API that allows automating posting of these tasks and we can use CURL in Asterisk to post the call details.This guide was written using Asterisk 11. If this doesn’t work for you ensure you have curl compiled in to Asterisk with “core show function CURL”.

First we need to tell Asterisk to run a command when a call ends. We are going to do it at the end so that we can get the length of the call. FreePBX allows for this using a hangup-handler.

We are going to add the following lines to – /etc/asterisk/extensions_custom.conf

[macro-dialout-trunk-predial-hook]    ; check to ensure this context doesn't already exist before adding
exten => s,1,Set(CHANNEL(hangup_handler_push)=hangup-handler,s,1)

[hangup-handler]
exten => s,1,Noop( capsule crm intergration ${CALLERID(all)})
exten => s,n,Set(foo=${CURL(http://127.0.0.1/capsule.php?strCallid="${CALLERID(num)}"&strDuration="${CDR(duration)}")})
exten => s,n,Noop(${foo})
exten => s,n,Return()

After changing the dial plan we will either need to restart Asterisk or run “dialplan reload”.

That will call a web page on the local server called capsule.php, now we need to create that file. On my server it will look here for the file – /var/www/html/capsule.php

<?php

// You will need to change these settings
$capsulepage = "https://XXXXXXXXXXX.capsulecrm.com/api/task";
$Token = 'XXXXXXXXXXXXXXXXXXXXXXXXX';

$duedate1 = date("Y-m-d");
$duedate2 = date("H:i:s");
$duedate = "$duedate1"."T"."$duedate2"."Z";

$number = $_GET['strCallid'];
$duration = $_GET['strDuration'];

$myxml="<?xml version='1.0' encoding='UTF-8'?>\n
<task>\n
<description>Call from $number for $duration seconds</description>\n
<dueDateTime>$duedate</dueDateTime>\n
<category>incoming call</category>\n
</task>";

// Initialise the session and return a cURL handle to pass to other cURL functions.
$ch = curl_init($capsulepage);
$options = array(CURLOPT_USERPWD => "$Token:x",
CURLOPT_HTTPHEADER => array('Content-Type: application/xml'),
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $myxml
);
curl_setopt_array($ch, $options);

// Do the POST and collect the response for future printing etc then close the session
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
echo $responseInfo;
echo $response;
?>

You will need to change the 2 settings at the top of the script to match your Capsule settings.

Now when a call is made we should get something similar generated in Capsule CRM –

Capsule CRM tasks

If your task doesn’t get generated then check in the Asterisk logs to ensure the Curl request is being made, and check in the web server logs to ensure there are no errors when the script runs.

Many ideas from this post were taken from here – http://www2.cyber-cottage.co.uk/?p=836

2 thoughts on “Posting Asterisk/FreePBX CDRs as a task in Capsule CRM

  1. Mo

    This is so awesome .. How would I capture the incoming calls and send them the same way into Capsule .. I have tried searching the web but to no avail …

  2. Matt Post author

    Hi Mo.

    Are you using FreePBX?

    If so you would create a trunk for the inbound call and set “context=some_custom_context_you_create”. This would pass the call to a bit of dial plan, similar to that at the top of the post. Hopefully that’s enough to point you in the right direction …

    Matt

Comments are closed.