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.4.0 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
1 2 twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN); 3 twilio.sendSms({ 4 to:'+16515556677', // Any number Twilio can deliver to 5 from: '+14506667788', // A number you bought from Twilio and can use for outbound communication 6 body: 'word to your mother.' // body of the SMS message 7 }, function(err, responseData) { //this function is executed when a response is received from Twilio 8 if (!err) { // "err" is an error received during the request, if any 9 // "responseData" is a JavaScript object containing data received from Twilio. 10 // A sample response from sending an SMS message is here (click "JSON" to see how the data appears in JavaScript): 11 // http://www.twilio.com/docs/api/rest/sending-sms#example-1 12 console.log(responseData.from); // outputs "+14506667788" 13 console.log(responseData.body); // outputs "word to your mother." 14 } 15}); 16 17
####Place a phone call, and respond with TwiML instructions from the given URL
1 twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN); 2 twilio.makeCall({ 3 to:'+16515556677', // Any number Twilio can call 4 from: '+14506667788', // A number you bought from Twilio and can use for outbound communication 5 url: 'http://www.example.com/twiml.xml' // A URL that produces an XML document (TwiML) which contains instructions for the call 6 }, function(err, responseData) { 7 //executed when the call has been initiated. 8 console.log(responseData.from); // outputs "+14506667788" 9 }); 10 11
####Loop through a list of SMS messages sent from a given number
1 2 twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN); 3 twilio.listSms({ 4 from:'+16512223333' 5 }, function (err, responseData) { 6 responseData.smsMessages.forEach(function(message) { 7 console.log('Message sent on: '+message.dateCreated.toLocaleDateString()); 8 console.log(message.body); 9 }); 10 }); 11 12
####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.
1 2Meteor.Router.add('/api/twiml/voice', 'POST', function() { 3 var rawIn = this.request.body; 4 console.log(rawIn); 5 if (Object.prototype.toString.call(rawIn) == "[object Object]") { 6 twilioRawIn.insert(rawIn); 7 } 8 9 var question = {}; 10 if (rawIn.Body) { 11 question.inputQuestion = rawIn.Body; 12 question.source = "sms"; 13 } else if (rawIn.TranscriptionText) { 14 question.inputQuestion = rawIn.TranscriptionText; 15 question.source = "voicemail"; 16 } else { 17 return; 18 } 19 question.inputName = rawIn.From; 20 21 var toOrig = rawIn.To; 22 toOrig = toOrig.replace(/\+1/g, ""); 23 var toPretty = '('+toOrig.substr(0,3)+') '+toOrig.substr(3,3)+'-'+toOrig.substr(6,10); 24 var eventDetails = Events.findOne({phone: toPretty}); 25 26 if (_.size(eventDetails) == 0) { 27 return; 28 } else { 29 question.slug = eventDetails.slug; 30 } 31 32 Meteor.call('questionCreate', question, function(error, res) { 33 34 }); 35 36 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>'; 37 return [200, {"Content-Type": "text/xml"}, xml]; 38}); 39 40
1 2Meteor.Router.add('/api/twiml/sms', 'POST', function() { 3 var rawIn = this.request.body; 4 if (Object.prototype.toString.call(rawIn) == "[object Object]") { 5 twilioRawIn.insert(rawIn); 6 } 7 8 var question = {}; 9 if (rawIn.Body) { 10 question.inputQuestion = rawIn.Body; 11 question.source = "sms"; 12 } else if (rawIn.TranscriptionText) { 13 question.inputQuestion = rawIn.TranscriptionText; 14 question.source = "voicemail"; 15 } else { 16 return; 17 } 18 question.inputName = rawIn.From; 19 20 var toOrig = rawIn.To; 21 toOrig = toOrig.replace(/\+1/g, ""); 22 var toPretty = '('+toOrig.substr(0,3)+') '+toOrig.substr(3,3)+'-'+toOrig.substr(6,10); 23 var eventDetails = Events.findOne({phone: toPretty}); 24 25 if (_.size(eventDetails) == 0) { 26 return; 27 } else { 28 question.slug = eventDetails.slug; 29 } 30 31 Meteor.call('questionCreate', question, function(error, res) { 32 33 }); 34 35 var xml = '<Response><Sms>Thank you for submitting your question!</Sms></Response>'; 36 return [200, {"Content-Type": "text/xml"}, xml]; 37}); 38
1 2Meteor.Router.add('/api/twiml/transcribe', 'POST', function() { 3 var rawIn = this.request.body; 4 if (Object.prototype.toString.call(rawIn) == "[object Object]") { 5 twilioRawIn.insert(rawIn); 6 } 7 8 var question = {}; 9 if (rawIn.Body) { 10 question.inputQuestion = rawIn.Body; 11 question.source = "sms"; 12 } else if (rawIn.TranscriptionText) { 13 question.inputQuestion = rawIn.TranscriptionText; 14 question.source = "voicemail"; 15 } else { 16 return; 17 } 18 question.inputName = rawIn.From; 19 20 var toOrig = rawIn.To; 21 toOrig = toOrig.replace(/\+1/g, ""); 22 var toPretty = '('+toOrig.substr(0,3)+') '+toOrig.substr(3,3)+'-'+toOrig.substr(6,10); 23 var eventDetails = Events.findOne({phone: toPretty}); 24 25 if (_.size(eventDetails) == 0) { 26 return; 27 } else { 28 question.slug = eventDetails.slug; 29 } 30 31 Meteor.call('questionCreate', question, function(error, res) { 32 33 }); 34 35 return [200, {"Content-Type": "application/json"}, "ok"]; 36}); 37
For more examples, check out the official Twilio node.js quickstart section: http://twilio.github.io/twilio-node/#quickstart