• Stars
    star
    178
  • Rank 214,989 (Top 5 %)
  • Language
    JavaScript
  • Created about 14 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A (deprecated) Twilio helper library for node. Use the official one:

This is many years out of date now - please use the official library instead: https://github.com/twilio/twilio-node

node-twilio

A Node.js Twilio helper library.

Installation

Parts of node-twilio depend on express.

To install via npm:

npm install twilio

To install by hand, download the module and create a symlink in ~/.node_libraries

$ ln -s /path/to/node-twilio ~/.node-libraries/twilio

Note: Previously, the npm package named twilio referred to Aaron Blohowiak's Twilio helper library. Due to Aaron's time constraints, he has let this package use the twilio name while he is unable to work on that implementation.

Usage

To start, you'll need to obtain a Twilio account. (http://twilio.com). This will give you a Twilio Account Sid and a Twilio Auth Key. Using these, you may start using node for complex, awesome telephony applications.

To really get down to business, check out the documentation.

API

Low-Level REST Api Client

node-twilio provides a low-level interface for making requests of Twilio. This functionality is contained in lib/rest-client.js, and maps pretty much one-to-one with the Twilio REST API documentation.

Each method accepts a callback function that returns the HTTP response object resulting from the API request, as well as options specific to that call.

The low-level REST Api, whil helpful, is little more than a simple wrapper around the node HTTP library. It takes care of the HTTP Basic auth, ensuring all your parameters are serialized properly, unmarshalling the responses (which we use purely the JSON representation from Twilio), and letting you, the developer, use the responses. This is pretty boring. (Note: My saying 'this is pretty boring' is just a way for me to excuse myself from writing the documentation for the low-level client right now. I'll do it soon. The source is fairly well documented, and while I recognize documentation for everything is important, I just wanna skip over this part right now.)

High-Level Rest Api Client

node-twilio provides a high-level interface for dealing with Twilio requests and responses, and encapsulates all the functionality within EventEmitters. This means you can develop Twilio applications in node without worrying about or provisioning URIs for every request and response your application will make; It may be non-obvious from the description, but that is awesome.

In order to explain how great this is, I will use an example:

If you were to build a Twilio application in any language using any helper library other than this one, you'd wind up doing something like:

Twilio.makeOutgoingCall(toNumber, fromNumber, UriForCallback);

Then, you'd have to go out and ensure than UriForCallback is a real, provisioned URI, and you'd put either a script or a static Twiml file there, and Twilio would go and fetch it.

node-twilio, however, takes care of all of that provisioning for you, and represents all Twilio interacts as EventEmitters. Again, this is awesome. Here's an example:

First, we want to instantiate a new Twilo client object. The constructor takes three parameters: the account SID and auth token, as well as the hostname of the application server. (This is used to construct URIs to give Twilio.)

var sys = require('sys'),
    TwilioClient = require('twilio').Client,
    client = new TwilioClient(ACCOUNT_SID, AUTH_TOKEN, MY_HOSTNAME);

Now that we have our client, let's get a PhoneNumber object using one of the phone numbers that we've provisioned through some other channel. (Note: You can provision phone numbers very simply via the Low-Level REST API) The phone number used here can be any sort of Twilio number. If it's an outgoing caller ID, the object will only be able to make outgoing phone calls/SMS. If it's a regular incoming number, it will be able to make/receive phone calls and SMS.

var phone = client.getPhoneNumber('+16269239971');

We'll now setup our phone number. This goes out and requests the phone number instance resource and fills in a data structure with this phone number's details.

phone.setup(function() {
    
    // Alright, our phone number is set up. Let's, say, make a call:
    phone.makeCall('+18674451795', null, function(call) {
        
        // 'call' is an OutgoingCall object. This object is an event emitter.
        // It emits two events: 'answered' and 'ended'
        call.on('answered', function(reqParams, res) {
            
            // reqParams is the body of the request Twilio makes on call pickup.
            // For instance, reqParams.CallSid, reqParams.CallStatus.
            // See: http://www.twilio.com/docs/api/2010-04-01/twiml/twilio_request
            // res is a Twiml.Response object. This object handles generating
            // a compliant Twiml response.
            
            console.log('Call answered');

            // We'll append a single Say object to the response:
            res.append(new Twiml.Say('Hello, there!'));

            // And now we'll send it.
            res.send();
        });
        
        call.on('ended', function(reqParams) {
            console.log('Call ended');
        });
    });

    // But wait! What if our number receives an incoming SMS?
    phone.on('incomingSms', function(reqParams, res) {
        
        // As above, reqParams contains the Twilio request parameters.
        // Res is a Twiml.Response object.
        
        console.log('Received incoming SMS with text: ' + reqParams.Body);
        console.log('From: ' + reqParams.From);
    });

    // Oh, and what if we get an incoming call?
    phone.on('incomingCall', function(reqParams, res) {
        
        res.append(new Twiml.Say('Thanks for calling! I think you are beautiful!'));
        res.send();
    });
});

To get going beyond the basics, check out the documentation.

Notes

More documentation is forthcoming.

License (MIT License)

Copyright (c) 2010 Stephen J. Walters [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

Amazon-SNS-client-for-PHP

A simple, hacky, incomplete SNS client for PHP
PHP
21
star
2

toshi-testnet-faucet

A simple testnet faucet as a Toshi app. Read some background here:
JavaScript
14
star
3

crypto-prices

Crypto prices updated regularly and via GitHub Actions
Go
7
star
4

plusone

+1 badge server for github issues - demo: [this is now pointless as natively supported by github]
Crystal
7
star
5

crystal-ratelimit

A rate limiting class for Crystal
Crystal
6
star
6

Boxcar

Boxcar PHP API - for applications to send notifications and invites
PHP
6
star
7

Mt-Gox-Trade-API-PHP-Client

Basic implementation of a client for the Mt Gox Trade API in PHP
PHP
5
star
8

Gearman-and-Kohana-example-project

This is a sample Kohana 2.3.4 project that shows the differences in speed in using Gearman / not for sending emails via SMTP, it was written for a presentaion at Kohana PHP Conference London 2010.
PHP
5
star
9

rc-creative-coding

CSS
4
star
10

mongodb-tools

Python
3
star
11

Project-Pier

PHP
2
star
12

lvms-events

LVMS Events iCal feed
HTML
2
star
13

k_switch

Simple context and namespace switching for kubernetes written in Ruby.
Ruby
2
star
14

up-crystal-demo

Aiming to get a simple Crystal Kemal app running on Up without hacks / with best practices
Crystal
2
star
15

hypermerge-rest

A REST interface to hypermerge / pushpin
JavaScript
2
star
16

php-util-scripts

Various PHP util scripts hacked together for a purpose - might be handy for some people!
PHP
2
star
17

docker-http-printenv

Print your environment out over http
Go
2
star
18

unixtimestamps

For lulz, and to learn more go, I made a site (with sitemaps) which lists "every" unix timestamp ever. Google is processing ~2,070,650,000 pages so far.
HTML
1
star
19

homekit-nvenergy-ecofactor-thesimple-thermostat

The Simple Thermostat / NV Energy ECO Factor Homekit Bridge
Ruby
1
star
20

thefakebook

A clone / fork of http://code.google.com/p/thefakebook/
Python
1
star
21

i-use-riak

Community Members talking about Riak in their own words.
1
star
22

Mono-install-script

Shell
1
star
23

nikdb

Go
1
star
24

webhook-sqs-router

Route webhooks to SQS easily
Ruby
1
star
25

baremetrics-cli

Baremetrics CLI API client by
Go
1
star
26

crystal-heroku-hello-world

Crystal 'Hello, World!' for Heroku :)
Crystal
1
star
27

toddler-game

JavaScript
1
star
28

transferatu

Transfers data from one place to another. As a service.
Ruby
1
star
29

Test-repo

1
star
30

KohanaSage

SagePay payment gateway integration for CodeIgniter
PHP
1
star
31

las-vegas-car-scene-network-ical

iCal feed from for Car Scene Network in Las Vegas
Ruby
1
star