1

I am currently working with the Express platform, the Twilio Node.js SMS API and obviously javascript to send text messages to my users. Problem is, I don't know what I should do in order to send data through my GET variables on the front-end and capture those values with node.js on the back-end.

For testing purposes, I created a simple button that sends a text message to a fixed number when clicked.

Here is the javascript side:

function sms() {
   xmlhttp = new XMLHttpRequest();
   xmlhttp.open("GET","http://localhost:5001", true);
   xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
           alert(xmlhttp.responseText);
        } 
   }
   xmlhttp.send();
}

Here is the node.js side:

var accountSid = 'ACCOUNT_SID';
var authToken = 'ACCOUNT_TOKEN';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken); 

var express = require("express");
var app = express();

app.get('/',function(request,response){
    
    var to = "TO";
    var from = "FROM";
    client.messages.create({
        to: to,
        from: from,
        body: 'Another message from Twilio!',
    }, function (err, message) {
        console.log("message sent");
        
    }); 

});
app.listen(5001); 

I have came across two ways to send a responseText from Node.js, but can't manage to make them work first one using response.send("Hello World"); or the second one response.write("Hello again"); response.end();

So just to sum it up, I want to send variables (to, from, message, etc.) through my http request, capture them in node.js and send a responseText! As a heads up, I'm very comfortable with AJAX requests between JS and PHP, but Node.js is new to me.

Thanks in advance

Felix
  • 11
  • 2
  • Both the bits of code you provided should work, but you haven't shown them in the server side code. What's the problem? Where did you put them in the server side code? What happens when you make the request? Have you used the developer tools in your browser to examine the request? Does it get a 200 OK response? Does it contain the data you expect? Have you looked in the console for error messages? (I'd expect to see a cross origin exception which is really easy to stick into Google to find a solution for) – Quentin Aug 05 '16 at 12:31
  • 2
    A small note, you're not sending anything back to the front end to notify it of success. To do this, a simple response.send() or response.send('Received the data'); would tell the front end 200. – Brant Aug 05 '16 at 12:32
  • @Quentin When I make the request in the browser, I do get a 200 OK response, but when I evaluate it in the JS, I'm not able to alert the responseText. There are no alarming error messages in the console logs. – Felix Aug 05 '16 at 12:41
  • @Brant I added what you said to my personal code, but I can't alert it on the JS side using xmlhttp.responseText? How come? – Felix Aug 05 '16 at 12:43
  • You're looking at the Console in the browser's developer tools? Not the Node.JS console? And you're doing when you load the URL via XMLHttpRequest? – Quentin Aug 05 '16 at 12:44
  • @Quentin Yes, I'm looking at my browser's console when loading and pressing the button. – Felix Aug 05 '16 at 12:46
  • @Felix, not sure. You should be able to just view the response in the network inspector and see it coming through. From there, you'll see what key to pull from the response body that holds the text. – Brant Aug 05 '16 at 12:49
  • @Brant Do you think I need to setup a Proxy for my Node.js port 5001 to connect to my Apache port 80? Would that be the issue? – Felix Aug 05 '16 at 13:02

1 Answers1

0

I think the new Guides will help you out here with How to receive and reply to SMS in Node.js:

https://www.twilio.com/docs/guides/sms/how-to-receive-and-reply-in-node-js

The CodeRail along the right hand side will walk you through it step-by-step but you should pay attention particularly to the section titled "Generate a dynamic TwiML Message".

var http = require('http'),
    express = require('express'),
    twilio = require('twilio'),
    bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true })); 

app.post('/', function(req, res) {
    var twilio = require('twilio');
    var twiml = new twilio.TwimlResponse();
    if (req.body.Body == 'hello') {
        twiml.message('Hi!');
    } else if(req.body.Body == 'bye') {
        twiml.message('Goodbye');
    } else {
        twiml.message('No Body param match, Twilio sends this in the request to your server.');
    }
    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
});

http.createServer(app).listen(1337, function () {
    console.log("Express server listening on port 1337");
});
Megan Speir
  • 3,745
  • 1
  • 15
  • 25