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

标签 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 ) 短信中的关键字的文档/示例。

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

最佳答案

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

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

关于node.js - 如何获取并回复收到的特定短信信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053786/

相关文章:

javascript - Twilio 可编程视频聊天室

javascript - 如何在 twilio-programmable-chat 的 channel 上进行分页?

ios - 手动将 Twilio 导入 Swift

twilio - 使用 Twilio.TwimlResponse() 发送彩信

javascript - 机器人必须重新启动才能将用户输入传递给请求功能

angularjs - 如何使用用户名查询 Mongoose

node.js - 在主要过程中进行大量计算时出现 Electron 打cup

javascript - 从 AS3 客户端运行 MongoDB 查询?

javascript - 如何更好地保存书籍等数据?

javascript - 创建可导出对象或模块以使用 CommonJS/NodeJS javascript 包装第三方库