Coder Social home page Coder Social logo

twilio-meteor's Introduction

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

  twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN);
  twilio.sendSms({
    to:'+16515556677', // Any number Twilio can deliver to
    from: '+14506667788', // A number you bought from Twilio and can use for outbound communication
    body: 'word to your mother.' // body of the SMS message
  }, function(err, responseData) { //this function is executed when a response is received from Twilio
    if (!err) { // "err" is an error received during the request, if any
      // "responseData" is a JavaScript object containing data received from Twilio.
      // A sample response from sending an SMS message is here (click "JSON" to see how the data appears in JavaScript):
      // http://www.twilio.com/docs/api/rest/sending-sms#example-1
      console.log(responseData.from); // outputs "+14506667788"
      console.log(responseData.body); // outputs "word to your mother."
    }
});

####Place a phone call, and respond with TwiML instructions from the given URL

  twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN);
  twilio.makeCall({
    to:'+16515556677', // Any number Twilio can call
    from: '+14506667788', // A number you bought from Twilio and can use for outbound communication
    url: 'http://www.example.com/twiml.xml' // A URL that produces an XML document (TwiML) which contains instructions for the call
  }, function(err, responseData) {
    //executed when the call has been initiated.
    console.log(responseData.from); // outputs "+14506667788"
  });

####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

twilio-meteor's People

Contributors

andreioprisan avatar html5cat avatar alanning avatar

Stargazers

Dovydas avatar Jelena Smiljkovic avatar Sangmi Lee avatar  avatar Carlos Mario Mejia avatar RK avatar  avatar Gayan avatar Mick Stevens avatar Rick avatar martin. avatar Andrey avatar Ruud Erie avatar Ben Venker avatar Cory Trimm avatar Manuel Mateus avatar Martin Eboh avatar Henrique Zambon avatar  avatar Joe avatar Juri Kern avatar Christian Genco avatar Anson Poon avatar Nikita Anisimov avatar Angus avatar Chris Aloi avatar Johnie Hjelm avatar Andrew Linfoot avatar Randell S. Hynes avatar Peter Liu avatar Z Zeleznick avatar Ryan Yeske avatar Michael Ghobrial avatar Yaşar İÇLİ avatar Nina Lu avatar  avatar Ian Serlin avatar dillon avatar Elie Labeca avatar  avatar Hugo O'Connor avatar chnl avatar Tarun Chaudhry avatar Jonathan Barratt avatar Jidé avatar Nam Myoho Renge Kyo avatar Brian Levine avatar

Watchers

Christie Ewen avatar James Cloos avatar  avatar Martin Eboh avatar  avatar

twilio-meteor's Issues

add api.export to support Meteor 0.6.5+

Please add this to package.js for Meteor 0.6.5 support:

Package.on_use(function(api) {
  if (api.export) api.export('Twilio', 'server');
  api.add_files('twilio_npm.js', 'server');
});

update

Any update to the package planned?

How do you receive incoming calls?

I am working through the Hello Monkey example in the Twilio docs and want to use Meteor to respond to incoming calls. I set my initial voice URL to a path I defined in meteor-router but it is not clear what I have to do next.

I found this from http://twilio.github.io/twilio-node/#twimlResponse:

var http = require('http'),
      twilio = require('twilio');

http.createServer(function (req, res) {
    //Create TwiML response
    var twiml = new twilio.TwimlResponse();
    twiml.say('Hello World!');

    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());

}).listen(1337, '127.0.0.1');

But that will not work in Meteor. Have you been able to respond to incoming calls in Meteor?

Twilio not defined

Hi!

Thanks for your work on this. I'm looking forward to using it but it doesn't appear to be working for me. I have 'moment' installed.

I'll see if I can debug but thought I'd check if there were known pitfalls.

Twilio simply isn't in the dom for me.

Thanks,
Wilson

Add license

I didn't see a license file. What license are you using?

Capability and TwimlResponse not defined?

Hello,

It seems Capability and TwimlResponse are not available? When I try to do

twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN);
var resp = new twilio.TwimlResponse();

I get the error:

TypeError: undefined is not a function

Same error for when I try
twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN);

var capability = new twilio.Capability(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.