Fun with AMI (node-red)

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
wget https://gist.githubusercontent.com/lgaetz/64ad83952a2a629204222e48fc01bb51/raw/dc6216ec068c3a704dc4c2ec0bf51a74581a8eb2/lgaetz-hinterland.php
chmod +x lgaetz-hinterland.php
./lgaetz-hinterland.php

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

3 Likes

Never really played with AMI or ARI.

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

http://your.pbx.address:8088/ari/endpoints

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": []
  }
]

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

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!!

1 Like

Hi @pbx_noob

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

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

1 Like

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

1 Like

Hey @pbx_noob

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

1 Like

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

1 Like

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.

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.

image

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:

image

and post to twitter: https://twitter.com/hashtag/AsteriskAMI2NodeRed2IFTTT2Twitter?f=live

2 Likes

This is cooooooool!!!

1 Like

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

1 Like

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:

2 Likes

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