我如何获取并回复正在收到的特定短信息?

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 )。

使用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); 

但是我收到了一个未定义的消息。

** * ** * ** * ** *更新* ** * ** * ** * **** @ hexacyanide的信息合并后…下面返回所有的请求参数(现在我只需要parsing它们)。 只是以为我会把这个包括进去的问题的其他人。

 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); 

请求对象是一个可读的stream。 你必须等待数据:

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