A lightweight scalable Plivo application framework.
Plivode is a lightweight scalable application framework for creating Plivo apps with NodeJS.
npm install plivode
Let's run through a few examples to see how Plivode works.
var plivode = require'plivode';appID: '[your Plivo app ID]'authID: '[your Plivo auth ID]'authToken: '[your Plivo auth token]'rootUrl: '[the root URL this app is accessible by Plivo]'on'answer'responsespeak'Hello, world!'send;;
It doesn't get much simpler than this! Here we create a new Plivode app, which will answer any received phone calls with "Hello, world!"
Plivode makes use of Express to run a web server for receiving messages from Plivo.
Simply specify the URL of your server, and make sure the port you specify is accesible to the outside world. For example,
if your Plivode app is running on port 1919 of plivode-app.example.com, you'd pass:
Your Plivode app will raise events when you need to respond to a phone call or sms message. The standard Plivo events are:
answer- raised when a call is received by any of the phone numbers associated with your app.
message- raised when a sms message is received by any of the phone numbers associated with your app.
hangup- raised when a call is ended.
See it in action! Send a sms to
(949) 484-8425. Within a few seconds you'll get a call speaking your message.
var plivode = require'plivode';appID: '[your Plivo app ID]'authID: '[your Plivo auth ID]'authToken: '[your Plivo auth token]'rootUrl: '[the root URL this app is accessible by Plivo]'on'message'// When a sms is received, make an outbound call to the number that sent the sms,// passing the text of the sms to the "parrot" action.thisCalloutbound'[your Plivo phone number]' paramsFrom 'parrot' paramsText;// Send a blank response back, so Plivo knows we processed the message and not to resend it.responsesend;on'parrot/:message'// The user answered the return call, so speak the text of the sms that they sent, and repeat it 3 times.responsespeakparamsmessage loop: 3send;;
Here we listen for sms messages, and when one is received we call the number back and speak the message 3 times.
Plivode provides support for the entire Plivo REST API. Here we use
Call.outbound to make the call.
Notice that we specify the custom event
parrot/:message. This event is raised when the outbound call is answered.
Since event names map directly to Express routes, we're able to use Express' powerful
routing engine to specify parameters within the event name.
When we make the outbound call, we pass in the action route
['parrot', params.Text]. The first field of your route must
be the event name, and the remaining fields can be parameters to pass to the next step of the call. If you don't need to
pass along parameters you can simply pass the event name as a string.
See it in action! Call
Pick a number between 1 and 100, and the app will try to guess it. If you're thinking of a lower number press
*; higher press
and if the guess is correct press
var plivode = require'plivode';appID: '[your Plivo app ID]'authID: '[your Plivo auth ID]'authToken: '[your Plivo auth token]'rootUrl: '[the root URL this app is accessible by Plivo]'on'answer'console.log'answered call from: ' + paramsFrom;var MIN_GUESS = 1MAX_GUESS = 100guess = MIN_GUESS + MathroundMathrandom * MAX_GUESS - MIN_GUESS;responsespeak'Pick a number between one and one hundred' // Spelling out 100, otherwise speak pronounces it "1-o-o".wait3speak"I'm guessing it's " + guessgetDigitsaction: 'guess' MIN_GUESS MAX_GUESS guessnumDigits: 1validDigits: '*#0'finishOnKey: '123456789'timeout: 45responsespeak"If the number you're thinking of is lower than " + guess + ", press star; " +"if it's higher press pound; and if it's correct press 0."speak"I didn't catch that. Goodbye."send;on'guess/:min/:max/:guess'var min = parseIntparamsmin 10max = parseIntparamsmax 10guess = parseIntparamsguess 10newGuessdirection;// Look at what digit the user pressed to determine how to guess next, or if we guessed right.switch paramsDigitscase '*':direction = 'lower';max = guess - 1;break;case '#':direction = 'higher';min = guess + 1;break;case '0':console.logparamsFrom + ': ' + guess + ' was correct!';responsespeak'Hooray! Thanks for playing. Goodbye.'send;return;if min > max// The user told us the number is higher than the min, and less than the max, no compute.responsespeak"I've run out of numbers. Goodbye."send;return;else if max === min// We've narrowed it down to one numbernewGuess = min;else// We could just do a binary search and guess right in the middle, but// let's add a bit of randomness to make things a bit less predictable.var randomnessAmount = Mathroundmax - min * 0.2random = Mathrandom * randomnessAmount - randomnessAmount / 2;newGuess = Mathroundmax + min - randomnessAmount / 2 + random;// Pick a random start to the question to add some variability.var questions = 'How about ' 'Is it ' 'Perhaps ' 'Could it be 'question = questionsMathfloorMathrandom * questionslength + newGuess + '?';console.logparamsFrom ': The number is ' direction ' than ' + guess '. ' questionjoin'';// Ask the user again.responsegetDigitsaction: 'guess' min max newGuessnumDigits: 1validDigits: '*#0'finishOnKey: '123456789'timeout: 45responsespeakquestionspeak"I didn't catch that. Goodbye."send;on'hangup'console.log'call ended with: ' + paramsFrom;console.logparams;;
More documentation is on the way! For now, partial documentation is available here.