Last active
May 27, 2022 12:43
-
-
Save lgaetz/af5849b8984f6ec7aa12a29a01a7e38b to your computer and use it in GitHub Desktop.
FreePBX AGI script to get a Stir/Shaken Signature via API and apply it to an outgoing call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** | |
* | |
* Script: get_shaken.php | |
* | |
* Latest version: https://gist.github.com/lgaetz/af5849b8984f6ec7aa12a29a01a7e38b | |
* | |
* Usage: Asterisk AGI file to get a Stir/Shaken signature from Sansay NSS service | |
* | |
* AGI(get_shaken.php,source-phone-number,dest-phone-number[,variable-name]) | |
* | |
* source-phone-number - Outgoing CID for outbound call | |
* dest-phone-number - Destination for the outbound call | |
* variable-name - opt specify channel variable to store sig, default = SSsignature | |
* | |
* use with FreePBX dialplan such as | |
* | |
* [macro-dialout-trunk-predial-hook] | |
* exten => s,1,Noop(Entering user defined context macro-dialout-trunk-predial-hook) | |
* exten => s,n,AGI(get_shaken.php,${CALLERID(num)},${CALLERID(dnid),SSsignature}) | |
* exten => s,n,GoSub(func-set-sipheader,s,1(Identity,${SSsignature})) | |
* | |
* License: GNU/GPL3+ | |
* | |
* History: | |
* 2021-05-23 First commit by Lorne Gaetz | |
* | |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/ | |
// Init vars | |
$url ="https://your-nss.sansay.com:3334/stir/v1/signing"; // edit to match your Sansay NSS url | |
// AGI Class | |
require_once "phpagi.php"; | |
$AGI = new AGI(); | |
if (!isset($argv[2])) { | |
agi_verbose('AGI script requires two arguments'); | |
exit; | |
} else { | |
agi_verbose("Running script with source: ".$argv[1]." and dest: ".$argv[2]); | |
} | |
if (isset($argv[3])){ | |
$channel_var = $argv[3]; | |
} else { | |
$channel_var="SSsignature"; | |
} | |
// Build header array | |
$Request_ID=substr(md5(rand()), 0, 20); | |
$headers=array( | |
'Content-Type: application/json', | |
'Accept: application/json', | |
'X-RequestID: '.$Request_ID, | |
); | |
// build array of postdata and json encode | |
$postdata=array ( | |
'signingRequest' => | |
array ( | |
'orig' => | |
array ( | |
'tn' => $argv[1], | |
), | |
'dest' => | |
array ( | |
'tn' => array($argv[2]), | |
), | |
'iat' => time(), | |
), | |
); | |
$postjson=json_encode($postdata); | |
agi_verbose('Post data: '.$postjson); | |
// make API call | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); | |
curl_setopt($ch, CURLOPT_POSTFIELDS,$postjson); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$SSresult = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($SSresult,true); | |
$sig = $result['signingResponse']['identity']; | |
// remove " chars from returned string | |
$sig = str_replace('"','',$sig); | |
agi_verbose('Result: '.$sig); | |
$AGI->set_variable($channel_var,$sig); | |
exit; | |
function agi_verbose($string, $level=3) { | |
global $AGI; | |
$AGI->verbose($string, $level); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment