I've been working on the Twilio projects from Radical Skills, and I've been having trouble responding to incoming text messages using JavaScript. I'm able to use TwiML, which is XML, but I am unable to respond in JavaScript like the example shows.
My message request URL in Twilio is /incoming/handle-incoming-text.xml.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Redirect method="GET">incoming/sms.js</Redirect>
</Response>
When my Twilio application receives a text message, it redirects to sms.js. I've copied this code directly from the example.
var twilio = require('twilio');
var resp = new twilio.TwimlResponse();
if( req.Body == 'hello'){
resp.message('Hi!');
}else if( req.Body == 'bye'){
resp.message('Goodbye');
}
console.log(resp.toString());
When I send a text message saying "hello" from my phone to my Twilio number, I get the following responses.
GET /incoming/handle-incoming-text.xml 200 1ms - 240b
GET /incoming/sms.js 200 39ms - 251b
However, I do not get a text back from my Twilio number saying "Hi!" In fact, I do not get a response at all.
When I checked the error logs, it gave me a 12300 Invalid Content-Type error with the following message.
Twilio attempted to retrieve content from the URL but was unable to process the application/javascript Content-Type header in the response. Please see the Twilio XML Markup Documentation for more details on valid Content-Type settings.
One of the headers in my response is "Content-Type application/javascript." According to the twilio-node helper library documentation, it should output XML. However, it does not appear to be working properly.
I've spent several hours trying to figure this out, and I still haven't been able to make it work.