Twilio Meteor API bindings
This smart package exposes the official Twilio Meteor API from the node.js npm package: http://twilio.github.io/twilio-node/
This Meteor package is licensed under the MIT license.
This uses version 1.1.4 of the Twilio node.js package and the new meteor 0.6.5.1 npm bindings.
To Install
mrt add moment
mrt add twilio-meteor
moment is required for Twilio date conversion internals.
To get started, replace ACCOUNT_SID, AUTH_TOKEN with your Twilio credentials and use some of the examples below:
Send an SMS text message
twilio = Twilio ( ACCOUNT_SID , AUTH_TOKEN ) ;
twilio . sendSms ( {
to : ' +16515556677 ' ,
from : ' +14506667788 ' ,
body : ' word to your mother. '
} , function ( err , responseData ) {
if ( ! err ) {
console . log ( responseData . from ) ;
console . log ( responseData . body ) ;
}
} ) ;
Place a phone call, and respond with TwiML instructions from the given URL
twilio = Twilio ( ACCOUNT_SID , AUTH_TOKEN ) ;
twilio . makeCall ( {
to : ' +16515556677 ' ,
from : ' +14506667788 ' ,
url : ' http://www.example.com/twiml.xml '
} , function ( err , responseData ) {
console . log ( responseData . from ) ;
} ) ;
Loop through a list of SMS messages sent from a given number
twilio = Twilio ( ACCOUNT_SID , AUTH_TOKEN ) ;
twilio . listSms ( {
from : ' +16512223333 '
} , function ( err , responseData ) {
responseData . smsMessages . forEach ( function ( message ) {
console . log ( ' Message sent on: ' + message . dateCreated . toLocaleDateString ( ) ) ;
console . log ( message . body ) ;
} ) ;
} ) ;
Here are a few examples of how to process incoming Voice calls and SMS messages via the Meteor router.
This code is from a production site, feedvenue.com
twilioRawIn is a collection to store the raw input for later.
Meteor . Router . add ( ' /api/twiml/voice ' , ' POST ' , function ( ) {
var rawIn = this . request . body ;
console . log ( rawIn ) ;
if ( Object . prototype . toString . call ( rawIn ) == " [object Object] " ) {
twilioRawIn . insert ( rawIn ) ;
}
var question = { } ;
if ( rawIn . Body ) {
question . inputQuestion = rawIn . Body ;
question . source = " sms " ;
} else if ( rawIn . TranscriptionText ) {
question . inputQuestion = rawIn . TranscriptionText ;
question . source = " voicemail " ;
} else {
return ;
}
question . inputName = rawIn . From ;
var toOrig = rawIn . To ;
toOrig = toOrig . replace ( / \+ 1 / g , " " ) ;
var toPretty = ' ( ' + toOrig . substr ( 0 , 3 ) + ' ) ' + toOrig . substr ( 3 , 3 ) + ' - ' + toOrig . substr ( 6 , 10 ) ;
var eventDetails = Events . findOne ( { phone : toPretty } ) ;
if ( _ . size ( eventDetails ) == 0 ) {
return ;
} else {
question . slug = eventDetails . slug ;
}
Meteor . call ( ' questionCreate ' , question , function ( error , res ) {
} ) ;
var xml = ' <Response><Say voice="man">Please speak your question after the tone. You may hang up when you \' re finished</Say><Record maxLength="180" transcribe="true" transcribeCallback=" https://feedvenue.com/api/twiml/transcribe " /></Response> ' ;
return [ 200 , { " Content-Type " : " text/xml " } , xml ] ;
} ) ;
Meteor . Router . add ( ' /api/twiml/sms ' , ' POST ' , function ( ) {
var rawIn = this . request . body ;
if ( Object . prototype . toString . call ( rawIn ) == " [object Object] " ) {
twilioRawIn . insert ( rawIn ) ;
}
var question = { } ;
if ( rawIn . Body ) {
question . inputQuestion = rawIn . Body ;
question . source = " sms " ;
} else if ( rawIn . TranscriptionText ) {
question . inputQuestion = rawIn . TranscriptionText ;
question . source = " voicemail " ;
} else {
return ;
}
question . inputName = rawIn . From ;
var toOrig = rawIn . To ;
toOrig = toOrig . replace ( / \+ 1 / g , " " ) ;
var toPretty = ' ( ' + toOrig . substr ( 0 , 3 ) + ' ) ' + toOrig . substr ( 3 , 3 ) + ' - ' + toOrig . substr ( 6 , 10 ) ;
var eventDetails = Events . findOne ( { phone : toPretty } ) ;
if ( _ . size ( eventDetails ) == 0 ) {
return ;
} else {
question . slug = eventDetails . slug ;
}
Meteor . call ( ' questionCreate ' , question , function ( error , res ) {
} ) ;
var xml = ' <Response><Sms>Thank you for submitting your question!</Sms></Response> ' ;
return [ 200 , { " Content-Type " : " text/xml " } , xml ] ;
} ) ;
Meteor . Router . add ( ' /api/twiml/transcribe ' , ' POST ' , function ( ) {
var rawIn = this . request . body ;
if ( Object . prototype . toString . call ( rawIn ) == " [object Object] " ) {
twilioRawIn . insert ( rawIn ) ;
}
var question = { } ;
if ( rawIn . Body ) {
question . inputQuestion = rawIn . Body ;
question . source = " sms " ;
} else if ( rawIn . TranscriptionText ) {
question . inputQuestion = rawIn . TranscriptionText ;
question . source = " voicemail " ;
} else {
return ;
}
question . inputName = rawIn . From ;
var toOrig = rawIn . To ;
toOrig = toOrig . replace ( / \+ 1 / g , " " ) ;
var toPretty = ' ( ' + toOrig . substr ( 0 , 3 ) + ' ) ' + toOrig . substr ( 3 , 3 ) + ' - ' + toOrig . substr ( 6 , 10 ) ;
var eventDetails = Events . findOne ( { phone : toPretty } ) ;
if ( _ . size ( eventDetails ) == 0 ) {
return ;
} else {
question . slug = eventDetails . slug ;
}
Meteor . call ( ' questionCreate ' , question , function ( error , res ) {
} ) ;
return [ 200 , { " Content-Type " : " application/json " } , " ok " ] ;
} ) ;
For more examples, check out the official Twilio node.js quickstart section: http://twilio.github.io/twilio-node/#quickstart