Fun with AMI (node-red)

(Lorne Gaetz) #1

As a result of this thread a few weeks ago, I was reminded once again that

  • I don’t have much in the way of practical knowledge of how the Asterisk AMI works and
  • There is no simple resource to monitor a FreePBX phone dev state in real time

Since I’ve occasionally needed something like this, and since I was cooped up in quarantine and on vacation simultaneously, I set out to remedy those two problems. The result is this script:

In it’s present form, nobody would call it elegant. As my goal was largely to learn more about the AMI, it doesn’t do much. When you run it, every time an extension hint changes it echos to the console:

exten:  5002 status: 1 statustext:  InUse
exten:  5002 status: 0 statustext:  Idle
exten:  5002 status: 1 statustext:  InUse
exten:  5004 status: 8 statustext:  Ringing
exten:  5004 status: 1 statustext:  InUse
exten:  5002 status: 0 statustext:  Idle
exten:  5004 status: 0 statustext:  Idle

I have a vague sense that PHP is not the best language for this purpose, but I didn’t need to saddle myself with learning a new language as well as the AMI. And as the saying goes, “If your only tool is a hammer …”

Anyone who wants to give it a try is welcome to do so by:

cd /tmp
chmod +x lgaetz-hinterland.php

Make some calls and watch the console. Comments, corrections, etc. cheerfully received.

External Status Light for phone
Headmaster's Busy/Free light
(Itzik) #2

Never really played with AMI or ARI.

However, I do see that you do a http request with ARI


Once authenticated (you can probably pass the credentials in the string as well), you’ll get:

    "technology": "SIP",
    "resource": "101",
    "state": "online",
    "channel_ids": []
    "technology": "PJSIP",
    "resource": "201",
    "state": "offline",
    "channel_ids": []

(Dave Burgess) #3

… and the output of that structure drops nicely into PERL as an array of associative arrays.

(PbxNoob) #4

Cool, Lorne, thanks very much! Looks like I can run this as is with Node Red and just monitor the output of the script as spawn process. Thanks!!

(Lorne Gaetz) #5

Hi @pbx_noob

Share your results. I have wanted to revisit this concept using non-blocking node, but time is short.

(PbxNoob) #6

Well, at the moment it looks like this…we’ll see how testing goes! :slight_smile:

(Lorne Gaetz) #7

Great, looks like Node Red runs just fine locally on the Distro. There’s another weekend wasted.

(Lorne Gaetz) #8

Hey @pbx_noob

Were you aware there is native support in node red for an Asterisk AMI connector?

(PbxNoob) #9

arrrr!!! noo! :rofl::zipper_mouth_face::tired_face:

(D E) #10

Is it just me or does the ‘events’ filter not do anything? I have ‘call’ set in events (and have tried other event names) but it seems to just send everything.

(Lorne Gaetz) #11

There are a lot of events, even an a seemingly idle system. I think it probably is working, there’s just a ton of data. [edit - I think you’re right, it’s probably not filtering.] First thing I did was create a switch that looks for ExtensionStatus events to mimic the hinterland script and that cuts down on most of the events.


(Lorne Gaetz) #12

Cold rainy Saturday, so perfect for playing with Node Red. I got this really simple flow working:

Which sends the extension number and status to an IFTTT webhook in real time, which allows me to do things like dump them to a slack channel:


and post to twitter:

(Itzik) #13

This is cooooooool!!!

(D E) #14

It’s brilliant! Allows me to put this into practice hopefully! So pleased about spotting this. Thanks all!

(Lorne Gaetz) #15

After Saturday’s fun with AMI events, I spent time Sunday sending events to the AMI. This took quite a bit more to figure out, but eventually I got Node Red to generate a call from a local extension to the echo test. Flow looks like this:

Originate node config looks like this:

(Lorne Gaetz) closed #16

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.