FreePBX | Register | Issues | Wiki | Portal | Support

FreePBX 15 API to show Extension status


(A J) #1

Looking for feedback to the best solution to do this.

I’m really digging the idea of API behind freepbx upcoming versions.

Following this guide: PBX API > RESTful I was able to create a quick php script to call pjsip extension status (based off of asterisk info contacts)

<?php

namespace FreePBX\Api\Rest;
use FreePBX\modules\Api\Rest\Base;
class Status extends Base {
    public static function getScopes() {
        return [
            'read:status' => [
                'description' => _('Read Extenson status'),
            ]
        ];
    }

    public function setupRoutes($app) {
        $app->get('/status', function ($request, $response, $args) {
            $data = array();
            $online = array();
            $offline = array();
            $resp = shell_exec('asterisk -rx "pjsip show endpoints" | egrep "Unavailable|Avail | grep -v dpma_endpoint"');
            $resp = preg_replace('/ +/', ' ', $resp);
            $arr = explode("\n", $resp);
                foreach ($arr as $ext) {
                        $type = substr($ext,0,10);
                        if ( $type == " Contact: "){
                                $pos = strpos($ext,'/');
                                $ext = substr($ext,10,$pos-10);
                                array_push($online, $ext);
                        }elseif ( $type == " Endpoint:") {
                                $pos = strpos($ext,'/');
                                $ext = substr($ext,11,$pos-11);
                                array_push($offline, $ext);
                        }
                }
            $data = [
                "total" => array( "online" => count($online), "offline" => count($offline)),
                "online" => $online,
                "offline" => $offline
                ];
            return $response->withJson($data);
        })->add($this->checkReadScopeMiddleware('status'));

    }
}

Few questions.

Would calling asterisk -rx like this be problematic or vulnerable?

Would there be an easier way to do this?

And if this method is fine. Any tips on making the php code cleaner? I’m just a tinkerer and not a developer.

Postman returns json data as so

{
    "total": {
        "online": 1,
        "offline": 3
    },
    "online": [
        "72741"
    ],
    "offline": [
        "2",
        "3",
        "727412"
    ]
}

(Andrew Nagy) #2

@ajr3v NICE JOB!!

I’m glad you were kind of able to figure it all out!

A comment on this. In the end it would be better for your work here to go into the Asterisk Info module. Additionally Asterisk Info talks directly to Asterisk’s ARI to Parse this kind of information so I don’t believe there is any real need for asterisk -rvvvvv. We have one pending ticket in regard to Asterisk Info in FreePBX 15 (https://issues.freepbx.org/browse/FREEPBX-18384) however once that is complete I’d want to take what you did and put it into Asterisk Info and then use ARI to get the data instead of parsing asterisk -rvvv

One more note, Although the base work is done for API I realize we didn’t get many of the modules done but slowly over time we are planning to add more and community contributions like this really help get the ball rolling. Hope it’s easy to use… (REST is easier than GraphQL but GraphQL is more advanced)


(A J) #3

I wasn’t exactly sure how to add this to use the module.

I would love to try and expand this into ARI.

Highly agree with that. GraphQL is a completely new territory for me, but ill continue reading the docs. Once I get an understanding of it, ill see if I can put it into motion

It certainly is more ideal to specify the information needed.


(Andrew Nagy) #4

I’ll update this thread when we have news about https://issues.freepbx.org/browse/FREEPBX-18384 thats the only thing holding up 15 right now


(TheJames) #5

Small hijack… The FreePBX development team tries to put a lot of effort in to things that are

  • cool
  • make life easier for users/admins
  • make good business sense for users/admins and us (we strongly believe in dogfooding)

The strongest validation is to see people other than us using what was made through code, feedback and contributions.

Thanks for using FreePBX

image

Back to your regularly scheduled topic.