Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active December 21, 2022 10:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaetz/7e61c464588623662fcfdf1367e97c32 to your computer and use it in GitHub Desktop.
Save lgaetz/7e61c464588623662fcfdf1367e97c32 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
/************************************************************************
* callshaper-freepbx.php
*
* Latest version: https://gist.github.com/lgaetz/7e61c464588623662fcfdf1367e97c32
*
* AGI script for adding Stir/Shaken signatures to outbound SIP calls from
* Freepbx. Modified for FreePBX from callshaper.php as published by tiltx.com
* ref: https://help.smartcarrier.io/portal/en/kb/articles/tiltx-call-shaper-module
*
* Usage: Copy this script to /var/lib/asterisk/agi-bin and chown to asterisk:asterisk
Add custom diaplan to extension_custom.conf
*
* [macro-dialout-trunk-predial-hook]
* exten => s,1,Noop(Entering user defined context macro-dialout-trunk-predial-hook in extensions_custom.conf)
* exten => s,n,AGI(callshaper-freepbx.php,${CALLERID(dnid)},${CALLERID(num)})
* exten => s,n,Noop(Proceeding with Identity SIP header: ${IdentityHeader})
* exten => s,n,GoSub(func-set-sipheader,s,1(Identity,${IdentityHeader}))
* exten => s,n,MacroExit()
* ; end macro-dialout-trunk-predial-hook
*
* License: GNU/GPL3
*
* Version History:
* 2022-06-17 Initial Commit
*
************************************************************************/
set_time_limit(30);
// edit following to API key provided by Tiltx
$apiKey = "api-key-provided-by-tiltx";
if (!$apiKey) {
exit('Missing API key');
}
require('phpagi.php');
error_reporting(E_ALL);
$agi = new AGI();
$agi->verbose("Starting Script.");
//create curl resource
$ch = curl_init();
$url = sprintf('https://api.shaper.tiltx.com/Calls/shaper?ToNumber=' . $argv[1] . '&FromNumber=' . $argv[2]);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
sprintf('x-api-key: %s', $apiKey),
'Content-Type: application/json',
]);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$response = json_decode($output, true);
// $agi->verbose("Reponse ".$output); // uncomment for debug
$agi->verbose('Response: ' . implode(",",$response));
// set var to use in dialplan
$agi->set_variable("IdentityHeader", $response['Identity']);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment