yana

Yet another Asterisk AMI library

Yana

Yana is yet another node.js library for Asterisk Manager Interface.

$ npm install yana
var AMI = require('yana');
 
var ami = new AMI({
    port: 5038,
    host: 'example.com',
    login: 'login',
    password: 'secret',
    events: true,
    reconnect: true
});

Parameters:

  • host (optional, default: 'localhost'): host the client connects to
  • port (optional, default: 5038): port the client connects to
  • login: AMI user login
  • password: AMI user password
  • events (optional, default: true): set to false to not receive AMI events
  • reconnect (optional, default: false): automatically reconnect on connection errors

ami.send(action, [callback])

Parameters:

  • action: an object specifying AMI action to send to Asterisk. Keys are case insensitive.

To specify multiple keys with the same name, use an array as the value, for example

{
  Action: 'Originate',
  ...,
  SetVar: ['var1=1', 'var2=2']
}

will be transformed into AMI action

Action: Originate
...
SetVar: var1=1
SetVar: var2=2
  • callback (optional): a handler for the response and the following related events, if there are any.

Callback takes one argument that is message received from Asterisk. Callback is called once for each received message. The only argument is an object representing the message body.

ami.disconnect([callback]);

Parameters:

  • callback (optional)

AMI is an EventEmitter with the following events:

  • 'connect' emitted when the client has successfully logged in
  • 'error' emitted on unrecoverable errors (connection errors with reconnect turned off, unknown protocol, incorrect login)
  • 'disconnect' is only emitted in reconnection mode when the client loses connection
  • 'reconnect' is emitted on successful reconnection
  • 'event' fires on every event sent by Asterisk
  • all events received from Asterisk are passed trasparently, you can subsribe to events by their names, eg. 'FullyBooted' or 'PeerStatus'
  • UserEvents also trigger events like 'UserEvent-EventName', where EventName is specivied in UserEvent header of AMI message

For thorough documentation on available AMI commands see Asterisk Wiki.

var util = require('util');
var AMI = require('yana');
 
var ami = new AMI({
    login: 'login',
    password: 'secret'
});
 
ami.on('connect', function () {
    console.log('Connected');
});
 
ami.on('error', function(err) {
    console.log('An error occured: ' + err);
});
 
ami.on('FullyBooted', function (event) {
    console.log('Ready');
    ami.send({Action: 'ListCommands'}, function (res) {
        console.log(util.inspect(res));
    });
    setTimeout(function () {
        console.log('Disconnecting...');
        ami.disconnect(function () {
            process.exit(0);
        });
    }, 5000);
});

Look at example.js for more examples.

MIT