Code Monkey home page Code Monkey logo

Comments (17)

sjwalter avatar sjwalter commented on September 15, 2024

Awesome idea. Gimme a few hours...

from node-twilio.

clintandrewhall avatar clintandrewhall commented on September 15, 2024

That... is why I love GitHub. :-) Cheers!

Please let me know if I can help...! Great library, btw.

from node-twilio.

sjwalter avatar sjwalter commented on September 15, 2024

If you could test when I have a revision done, that'd help a whole lot! I don't really have an awesome testharness app for this, and it'd save me some time getting one together.

from node-twilio.

sjwalter avatar sjwalter commented on September 15, 2024

Latest push contains the change.

twilio/client now accepts an option called express. You can pass in your already-extant express app as that param, and that'll re-use your express app instead of making a new one. You must still pass in your hostanme (for generating the urls to tell Twilio about), and further, you should make sure you know about the basePath variable (the root that autoprovisioned URIs are generated starting from), because they will pollute your express app's routed URIs, and if you try to double them up (really unlikely, unless your app happens to also use /autoprivision/0 or whatever), bad things will happen.

Change committed to master. Let me know if this is acceptable.

from node-twilio.

clintandrewhall avatar clintandrewhall commented on September 15, 2024

Did some testing and tinkering... just need to change one thing. You don't want to start the listen if the user is managing the express instance: they'll get an exception.

if(opts.express) {
  self.express = opts.express;
}
else {
    self.express = express.createServer(
        express.logger(),
        express.bodyParser()
    );
    self.express.listen(self.port);
}

Other than that, this works on heroku!

from node-twilio.

jwilson avatar jwilson commented on September 15, 2024

This probably isn't the spot for this, but how did you get this working on Heroku? Depending on what method I use (GET or POST) I have different errors on just the initial request from Twilio.

from node-twilio.

Pablo1 avatar Pablo1 commented on September 15, 2024

What kinds of errors are you getting? Here are a few things to check...

  1. Are you creating your own express app and passing it in?
  2. What's the url you're using for your voice url in Twilio? It should be something like http://****/autoprovision/0 unless you've changed the default.
  3. Is your Heroku related setup correct (i.e. Procfile, package.json) ?
  4. Are your twilio credentials correct and are you using them when creating a new Twilio client?
  5. What port is your express listening to? You should be doing something like... var port = process.env.PORT || 3000;
  6. What port is your TwilioClient using? Try specifying port 80 for the 'port' parameter

from node-twilio.

jwilson avatar jwilson commented on September 15, 2024

Thanks for the reply!

  1. Yes it was something along the lines of: create express, make opts, opts.express = myapp, and then pass opts to TwilioClient
  2. The url I was using for the voice app was the default one (autoprovision).
  3. Everything else about Heroku was working except for the routes to TwilioClient
  4. Yes the same code worked on my machine before moving it to Heroku (and making the address/port changes)
  5. It was using the method you have
  6. The issue with supplying 80 as the port is that you need to be root to listen in on 80. Since it seems that if you just use the Heroku port they give you the callback to Twilio includes that port which then modifies your VoiceUrl to include that new port. That port is only accessible once you are behind whatever proxy Heroku has setup. I patched AutoUri (I think) to substitued 80 in instead of the Heroku port, but that didn't seem to accomplish much.

from node-twilio.

clintandrewhall avatar clintandrewhall commented on September 15, 2024

In order for this to work on Heroku or similar, the change I outlined above:

if(opts.express) {
  self.express = opts.express;
}
else {
    self.express = express.createServer(
        express.logger(),
        express.bodyParser()
    );
    self.express.listen(self.port);
}

NEEDS to be implemented in the library. Heroku starts the server... attempting to start it again will cause an error.

I can clone and create a merge request, if you like.

from node-twilio.

clintandrewhall avatar clintandrewhall commented on September 15, 2024

jwilson... if you're using npm, make the change I have above to line 33 of auto-uri.js and see if that fixes your problems?

from node-twilio.

jwilson avatar jwilson commented on September 15, 2024

That was the first change that I did actually. To me it just seems like that once this lib was used as an endpoint with express (I think thats what happens) it stopped serving the node-twilio routes (autoprovision). I haven't yet tried to pass express to node-twilio on amazon (moved my app there for the time being) but I can check it out tonight.

from node-twilio.

clintandrewhall avatar clintandrewhall commented on September 15, 2024

Well, I made that change as was able to deploy to Heroku with no problem:

app = express.createServer();
app.use(express.bodyParser());
app.use(express.cookieParser());

var TwilioClient = require('./lib/node-twilio').Client,
  Twiml = require('./lib/node-twilio').Twiml,
  client = new TwilioClient(config.twilio.sid, config.twilio.token, config.host, {
    "express" : app,
    "port" : config.port
  });

app.get("/" + config.client.name, function(req, res) {
  var out = "hello", phone = client.getPhoneNumber('+1' + config.twilio.number);
  phone.setup(function() {
    phone.sendSms("+1" + config.client.number, out, {}, function(text) {
      logger.debug("Text object: " + sys.inspect(text))
    });
  });
});

app.post("/incoming/sms", function(req, res) {
  logger.debug("Received an SMS.");
  logger.trace(sys.inspect(req.body));
  res.send("<Response><Sms>Thanks!</Sms></Response>");
});

app.listen(process.env.PORT || config.port);

from node-twilio.

jwilson avatar jwilson commented on September 15, 2024

Hmm..well I have no idea why mine wasn't working. I will attempt to reproduce the issue this weekend. It just seemed weird that all I had to do was change the port and hostname and it worked on amazon. Anyways, will report back with what I find.

Another edit. How did you configure it so that the callback didn't send the Heroku supplied port back to Twilio? If I used (process.env.PORT || 3000) then when the callback url was create by node-twilio it would have the Heroku port. Twilio would then update my VoiceURL to use the new port...

from node-twilio.

sprice avatar sprice commented on September 15, 2024

"port" : config.port

I can't get my app to work with this patch. What are you using for config.port?

from node-twilio.

lionstone avatar lionstone commented on September 15, 2024

I had to make one more change to get this to work on Heroku for text messages -

removing self.port from this line: self.baseUri = 'http://' + self.hostname + '/' + self.basePath + '/';

from node-twilio.

jmhnilbog avatar jmhnilbog commented on September 15, 2024

(Coffeescript ahead. Pretend it's pseudo-code if you like.)

This'll fix the problem within your app. Send the port like so: (assuming you've defined PORT and the TWILIO constants)

client = new Client TWILIO.ACCOUNT_SID, TWILIO.AUTH_TOKEN, TWILIO.APP_URL,
    port: process.env.PORT || PORT

Then follow that with an edit to the global autoUri instance:

autoUri = global.autoUri
autoUri.baseUri = 'http://' + autoUri.hostname + '/' + autoUri.basePath + '/'

from node-twilio.

ukd1 avatar ukd1 commented on September 15, 2024

This library is deprecated in favor of the official library - https://github.com/twilio/twilio-node

from node-twilio.

Related Issues (20)

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.