2013-09-27 36 views
2

Twilio有关于如何响应php(https://www.twilio.com/help/faq/sms/how-do-i-build-a-sms-keyword-response-application)和python(https://www.twilio.com/docs/quickstart/python/sms/replying-to-sms-messages)的SMS消息中的关键字的文档/示例。如何获取并回复收到的特定SMS信息?

使用node.js抓取“请求参数”的等价物是什么?因为我希望能够像使用其他示例一样使用短信中收到的信息进行回复。

我现在的想法是,我的回答应该是这样的:

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

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

    twiml.message('Thanks, you said: ' + req.body + ' -- we received your message'); 
    res.writeHead(200, {'Content-Type': 'text/xml'}); 
    res.end(twiml.toString()); 

}).listen(8080); 

但我发现了一个未定义的消息。

** * ** * ** * ** * UPDATE * ** * ** * ** * ** ** 将@hexacyanide的信息合并后...下面返回所有的请求参数(现在我只需要解析它们)。只是觉得我会把这个包括进这个问题的其他人。

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

http.createServer(function (req, res) { 

    var body = ''; 

    req.on('data', function(data) { 
    body += data; 
    }); 

    req.on('end', function() { 
    //Create TwiML response 
    var twiml = new twilio.TwimlResponse(); 

    twiml.message('Thanks, your message of "' + body + '" was received!'); 

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

}).listen(8080); 

回答

1

请求对象是可读流。您必须等待数据:

var body = ''; 
req.on('data', function(data) { 
    body += data; 
}); 
req.on('end', function() { 
    // do something with body 
}); 
相关问题