npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

plivo-promise

v0.5.1-alpha

Published

Plivo NodeJS helper library.Now with promise

Downloads

2

Readme

#plivo-node NPM version Build Status

Node.js helper library for the Plivo API, to create powerful Voice and SMS applications.

This helper implements the following features:

  • Wrappers for Plivo REST API
  • XML generation for synchronously controlling incoming calls and messages.

We have developed some examples to show you how to use our node.js helper and to help you get started quickly. These examples are available at http://github.com/plivo/plivo-examples-node.

More information on Plivo APIs and related concepts, refer https://www.plivo.com/docs/.

Installation

Installing using npm (node package manager):

npm install plivo

If you don't have npm installed or don't want to use it:

cd ~/.node_libraries # or the directory where node modules are stored in your OS.
git clone git://github.com/plivo/plivo-node.git plivo-node

NOTE: If you are not using npm for installation, then make sure that the dependencies used are installed as well.

Dependencies

Required Dependencies:

Dev Dependencies (for running tests):

Usage

plivo node.js helper can be used to make REST API calls and can also be used to control incoming calls and messages.

REST API

RestAPI takes one argument i.e. an object that contains two keys - authId and authToken, like so:

var plivo = require('plivo');

var api = plivo.RestAPI({
  authId: '<your AUTH ID>',
  authToken: '<your AUTH TOKEN>',
});

The RestAPI object exposes all the Plivo APIs and associated methods. Every method exposed by RestAPI object accepts two parameters:

  • params: an object containing a map of API params and their values.
  • callback: a callback that gets called after receiving response. Callbacks get two parameters:
    • status: HTTP Response Status Code. Example: 200, 201
    • response: a Javascript object because all our APIs send responses in JSON.

So for example, to make a call, you may do something like this:

/**
 * api.make_call accepts params and callback
 */
 
// Keys and values to be used for params are the same as documented for our REST API.
// So for using RestAPI.make_call, valid params can be checked
// at https://www.plivo.com/docs/api/call/#outbound.
var params = {
  from: '<your number>',
  to: '<recipient's number>',
  answer_url: 'http://your-server.com/answer_url',
};

api.make_call(params, function(status, response) {
  if (status >= 200 && status < 300) {
    console.log('Successfully made call request.');
    console.log('Response:', response);
  } else {
    console.log('Oops! Something went wrong.');
    console.log('Status:', status);
    console.log('Response:', response);
  }
});

Some RestAPI methods that implement Plivo REST API that do not have required parameters may ommit the use of params if not required. For example, to get Call Detail Records using our Call API, you may do something like this:

// when you want to get all the CDRs without using any params
api.get_cdrs(function(status, response) {
  ...
});

// when you do want to use params like "limit"
api.get_cdrs({ limit: 10 }, function(status, response) {
  ...
});

XML Generation

XML Generation can be used to generate XML that Plivo understands to synchronously control calls and messages. You may want to use it in with a web framework like Express, Geddy or whichever you prefer.

To use this feature, use the object returned by the plivo.Response function. Use it like so:

var plivo = require('plivo');
var response = plivo.Response();

// generates XML string.
console.log(response.toXML());

/*
OUTPUTS to screen:

<Response></Response>
*/

Response exposes the following methods:

  • toXML: generates the XML string response. This method does not require any parameter.
  • add<XML Element>: there are many methods that follow this kind of naming pattern. Replace <XML Element> with any valid XML element listed on https://www.plivo.com/docs/xml/. Some of these methods accept body param (a string) as an argument, some accept attributes (an object that is a map of valid attributes of the <XML Element>) as an argument and some accept both. Which method accepts which paramter depends upon the element.
    • addConference: accepts body and attributes as arguments
    • addNumber: accepts body and attributes as arguments
    • addUser: accepts body as argument
    • addDial: accepts attributes as argument
    • addGetDigits: accepts attributes as argument
    • addHangup: accepts attributes as argument
    • addMessage: accepts body and attributes as arguments
    • addPlay: accepts body and attributes as arguments
    • addPreAnswer: accepts no argument.
    • addRecord: accepts body and attributes as arguments
    • addRedirect: accepts body and attributes as arguments
    • addSpeak: accepts body and attributes as arguments
    • addWait: accepts attributes as argument
    • addDTMF: accepts body as argument

So, you may use the above functions like so:

/**
 * Add a Speak Element.
 */

// add the Speak element
// Speak accepts both "body" and "attributes" as params.
// note that "loop" is a valid attribute for Speak element - https://www.plivo.com/docs/xml/speak/
response.addSpeak('Hello world!', { loop: 2 });

// add the Wait element
// Wait accepts only "attributes" as a param - https://www.plivo.com/docs/xml/wait/
response.addWait({ length: 3 });

// add the DTMF element
// DTMF accepts only "body" as a param - https://www.plivo.com/docs/xml/dtmf/
response.addDTMF('12345');

// generate the response
console.log(response.toXML());

/*
OUTPUTS to screen:

<Response><Speak loop="2">Welcome</Speak><Wait length="3"/><DTMF>12345</DTMF></Response>
*/

Nesting of Elements

Every element has a defined set of elements that can be nested in it. For example Speak, Play, Wait and a few others can be nested under PreAnswer, and User, Number cannot be nested under Response.

To allow nesting, all the add<XML Element> methods return the <XML Element> object to allow calling add<XML Element> methods on them for nesting. For example:

// Add Dial element.
var dial_element = response.addDial();

// Add User element and Number element to Dial element.
dial_element.addUser('sip:[email protected]');
dial_element.addNumber('107456967856');

// Generate the XML string representation for the Dial element.
console.log(dial_element.toXML());

/*
OUTPUTS to screen:

<Dial><User>sip:[email protected]</User><Number>107456967856</Number></Dial>
*/

// Generate the complete XML response string
console.log(response.toXML());

/*
OUTPUTS to screen:

<Response><Dial><User>sip:[email protected]</User><Number>107456967856</Number></Dial></Response>
*/

Tests

To run tests: npm test

or mocha --reporter spec

License

plivo-node is licensed under the MIT License.

References