Working with Asterisk and Node.js

Screen Shot 2014-08-13 at 2.21.49 PM Screen Shot 2014-08-13 at 2.22.23 PM I’ve been working on a very cool project project here at Vale Presente which was able to provide a smooth interface to our call center attendees increasing their productivity and quality of our costumer service. The project was built with HTML5, CSS3, Javascript and Node.js as our backend engine (only cool stuff!). But the main challenge was finding the right tool to get the job done in a fast and consistent manner. I’m not going to talk about how I built the client. My focus here is talk about the tool I used to create a fast and consistent connection between the attendees and Asterisk. That was tough. There are a lot of libraries available over the Internet but most of them aren’t production ready.

Production ready

During my quest I had the pleasure to meet asterisk-manager. A Asterisk Manager Interface created by Philipp Dunkel which is a node.js module for interacting with the Asterisk Manager API. Nowdays I’m helping him as a maintainer of the project, improving, fixing bugs and helping to increase the project’s visibility. Not just because I’m a maintainer but because it works!

Currently the asterisk-manager module/package for node.js is handling incredibly well more than 500,000 asterisk events and ~5,000 voice calls a day. All of those events are being transmitted to our clients through Socket.io. This is a production ready and tested solution.

How to use it

Pretty simple. First you have to install it directly:

npm install asterisk-manager

Or put it inside of your package.json:

{
 "dependencies": {
    "asterisk-manager": "0.1.x"
  }
}

Now you have to create a connection instance with your asterisk server:

var AsteriskManager = require('asterisk-manager')
  , ami = new AsteriskManager(config.asterisk.port, config.asterisk.host, config.asterisk.user, config.asterisk.password, true)
  ;

And that’s it! It works! Now you have to take a closer look inside of the available events and actions and have fun!

Events

The list of all available events which is supported by Asterisk Manager API can be found here: https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+AMI+Events How do I listen for any/all AMI events?

ami.on('managerevent', function(evt) {});

How do I listen for specific AMI events?

ami.on('hangup', function(evt) {});
ami.on('confbridgejoin', function(evt) {});

What if my connection dropped? You can listen to the connection events like:

ami.on('close', function(e) {});
ami.on('disconnect', function(e) {});
ami.on('connect', function(e) {});

And make your own decision on what do to next.

Actions

The list of all available actions supported by Asterisk Manager API can be found here: https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+AMI+Actions How do I call an action?

ami.action({
  'action':'originate',
  'channel': "SIP/1234",
  'exten': '1143224321',
  'context': 'from-internal',
  'priority': '1'
}, function(err, res) {});

And that’s it! Have fun!

Github: https://github.com/pipobscure/NodeJS-AsteriskManager NPM Repository: https://www.npmjs.org/package/asterisk-manager Bug Reports: https://github.com/pipobscure/NodeJS-AsteriskManager/issues

Posts Relacionados:

  • Nenhum post relacionado!