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/