FreePBX Chef - Automate PJSIP Trunk Creation

Hi All,

First time post here, long time freepbx user :slight_smile:
I am trying to automate the creation & maintenance of some of the tasks on our PBXs using Chef.

As a first step, i have created a short PHP script, to create new trunks, using a function i found in /var/www/html/admin/modules/core/ .

Script looks like this:

$bootstrap_settings = array();
$bootstrap_settings['freepbx_auth'] = false;
include '/etc/freepbx.conf';

    $tech         = "pjsip";
    $channelid     = "";
    $dialoutprefix = "";
    $maxchans    = "";
    $outcid        = "447878787777";
    $peerdetails   = "test";
    $usercontext   = "";
    $userconfig    = "test";
    $register      = "yesplease";
    $keepcid    = "";
    $failtrunk     = "";
    $disabletrunk  = FALSE;
    $provider      = "someprovider";
    $continue     = TRUE;
    $dialopts      = "";

core_trunks_add($tech, $channelid, $dialoutprefix, $maxchans, $outcid, $peerdetails, $usercontext, $userconfig, $register, $keepcid, $failtrunk, $disabletrunk, $name, $provider, $continue, $dialopts)

//test to map params
//core_trunks_add("pjsip", "1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21")

// 1 = tech
// 2 = outbound dial prefix
// 3 = max channels
// 4 = outbound caller idate

// 12= Trunk Name
// 15 = dial openssl_get_cert_locations
// 10 = monitor trunk failures 1

And it kinda works - it can add a new trunk, appears fine, putting in the name, and a lot of the options above work, i can figure out the rest later - however i cant figure out how to add in the specific PJSIP settings, i.e. Username, Passowrd, Server etc. Would also be nice to figure out how to edit the advanced settings as well.

Anyone with any ideas of where to look - would be much appreciated!

A lot of functions you find in a file are going to be outdated and quite possibly a dozen years old. Most new functionality (I’d guess PJSIP would fall into that category) is going to be in the class files, e.g. Core.class.php.

IMO, the most reliable approach for programmatically creating objects is direct inserts into the database. With a few exceptions (creating extensions – which require some additions to the Asterisk DB – being the big one) we haven’t had any problems with a simple MySQL INSERT followed by fwconsole reload.

Wicked, thanks a lot, sql insert sounds like the way to go!

The preferred method for doing this would be to use the REST or GraphQL APIs. I haven’t used the REST API a lot, but I did do a blog post about GraphQL at a couple of months ago. The GraphQL support is still in technology preview and has some bugs, but it should be more usable and stable over the long run that trying to do the automation with PHP code. Please give it a try and see if it works for you!