Skip to content

Instantly share code, notes, and snippets.

@grintor
Created October 11, 2022 20:09
Show Gist options
  • Save grintor/f37bc09cd26963337d36925985cd903a to your computer and use it in GitHub Desktop.
Save grintor/f37bc09cd26963337d36925985cd903a to your computer and use it in GitHub Desktop.
A freepbx click to call api
<?php
$trunk = 'trunk_name';
/*
This post2call.php creates a json rest api for connecting two phones together with freepbx
append this to extensions_custom.conf (same trunk name as above):
[post2call]
exten => s,1,Dial(SIP/trunk_name/${phone})
Example POST requests:
{
"source": "4788450001",
"destination": "8002343993",
"cid": "4788450002",
"source_is_external": true
}
{
"source": "104",
"destination": "8002343993",
"cid": "4788450002",
"source_is_external": false
}
*/
if (apache_request_headers()['Authorization'] !== 'Bearer abc123orGoodPassword'){
http_response_code(401);
die('bad auth');
}
$json = file_get_contents('php://input');
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
die('bad json');
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(400);
die('bad method');
}
if (!$data['source']) {
http_response_code(400);
die('missing source');
}
if (!$data['destination']) {
http_response_code(400);
die('missing destination');
}
if (!$data['cid']) {
http_response_code(400);
die('missing cid');
}
if (!isset($data['source_is_external'])) {
http_response_code(400);
die('missing source_is_external');
}
$source = $data['source'];
$cid = $data['cid'];
$destination = $data['destination'];
if ($data['source_is_external']) {
$channel = "SIP/$trunk/{$source}";
} else {
$channel = "SIP/{$source}";
}
$file = "/var/spool/asterisk/outgoing/".$destination.".call";
fopen($file, "w");
$content = "Channel: $channel\n";
$content .= "Callerid: <+1$cid>\n";
$content .= "WaitTime: 20\n";
$content .= "Context: post2call\n";
$content .= "Extension: s\n";
$content .= "Priority: 1\n";
$content .= "Set: phone=+1$destination\n";
print(file_put_contents($file, $content, FILE_TEXT | LOCK_EX));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment