NodeJS Asterisk Manager API
JavaScript
Pull request Compare This branch is 85 commits behind pipobscure:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md
asterisk.js
package.json

README.md

NodeJS Asterisk Manager API

For a project of mine I needed a low level interface to the Asterisk Manager API. I looked around and found https://github.com/mscdex/node-asterisk . While it was a good starting point, it had too many abstractions for my taste. Which is why I based my version on it an then radically refactored it. In the end there now is very little in common with it.

So this is basically a different piece of work, but since there is a shared DNA and I got a good start by depending on Brian's work, I feel like giving credit is appropriate.

API Overview

var manager = new (require('asterisk'))(5038, 'localhost');

manager.on('connect', function(err, val) {
    manager.authenticate('username', 'password');
});

manager.on('close', function() {});
manager.on('error', function(err) {});
manager.on('managerevent', function(evt) {});
manager.on('response', function(res) { /* This is done because we can, not because it is needed! */});
manager.connect();
/*
Possible parameters to connect() are username, password, and a boolean indicating whether the connection should be reconnected if it fails.
All parameters are optional and are only used for reconnection not for the first authentication!
*/

manager.action({
    'action':'originate',
    'channel':'SIP/myphone',
    'context':'default',
    'extension':1234,
    'priority':1,
    'variables':{
        'name1':'value1',
        'name2':'value2'
    }
}, function(err, res) {});


/*
You should use sendcommand(options, callback) to send a command to asterisk and recieve a response from asterisk in the callback.
The options parameter is the same as what you would pass to action. 
*/

manager.sendcommand({
    'action':'queuesummary'
}, function(err, res) {
    console.log(res);
});

/*
The following is an example response from sendcommand().
*/
[ { event: 'queuesummary',
    queue: '12345',
    loggedin: '1',
    available: '1',
    callers: '0',
    holdtime: '0',
    talktime: '0',
    longestholdtime: '0',
    actionid: '1331637239672302' },
{ event: 'queuesummary',
    queue: '67890',
    loggedin: '1',
    available: '1',
    callers: '0',
    holdtime: '0',
    talktime: '0',
    longestholdtime: '0',
    actionid: '1331637239672302' },
{ event: 'queuesummarycomplete',
    actionid: '1331637239672302' } ]

/*
    Variables are automatically put in the right format. Aside from that everything is passed straight through to Asterisk. See Manager API documentation for what is possible.
*/

manager.disconnect();