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

时间:2013-09-27 14:52:13

标签: node.js twilio

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消息中响应关键字的文档/示例。

使用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的信息后,它可以工作......以下命令返回所有请求参数(现在我只需要解析它们)。我以为我会把这个包含在遇到这个问题的其他人身上。

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 个答案:

答案 0 :(得分:1)

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

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