我已经在nodejs中使用Twilio编写了一个可编程的SMS功能。我有一条消息,它有选择的选项,当用户发回任何响应时,我希望使用twilio发送自动响应。
我已经完成了所有,除了在处理来自用户的响应后,我的自动响应没有被交付给用户。

我一直在我的twilio仪表板上看到上面的东西。
下面是我的响应处理程序代码。
app.post('/ui/sms',function(req, res) {
//req.headers['Content-type'] = 'text/xml';
//req.headers['Accept'] = 'text/xml';
try {
console.log('Processing Response', req.headers);
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const twiml = new MessagingResponse();
const fromTwilio = isFromTwilio(req);
console.log('isFromTwilio: ', fromTwilio);
if (fromTwilio) {
let msg = req.body.Body||'';
if (msg.indexOf('1')>-1) {
twiml.message('Thanks for confirming your appointment.');
} else if (msg.indexOf('2')>-1) {
twiml.message('Please call 408-xxx-xxxx to reschedule.');
} else if (msg.indexOf('3')>-1) {
twiml.message('We will call you to follow up.');
} else {
twiml.message(
'Unknown option, please call 408-xxx-xxxx to talk with us.'
);
}
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(twiml.toString());
}
else {
// we don't expect these
res.status(500).json({ error: 'Cannot process your request.' });
}
/*processSMSResponse(req, function(response) {
res.json(response);
});*/
} catch(e) {
res.json(e);
}
});
function isFromTwilio(req) {
console.log('REQ HEADER:::::::::\n', req);
// Get twilio-node from twilio.com/docs/libraries/node
const client = require('twilio');
// Your Auth Token from twilio.com/console
const authToken = 'xxxxxxxxxxxxxxxxx';
// The Twilio request URL
//const url = 'https://mycompany.com/myapp.php?foo=1&bar=2';
const url = 'https://xxxx.com/ui/sms';
var reqUrl = 'https://xxxx.com/ui/sms'
// The post variables in Twilio's request
//const params = {
//CallSid: 'CA1234567890ABCDE',
//Caller: '+14158675310',
//Digits: '1234',
//From: '+14158675310',
//To: '+18005551212',
//};
const params = req.body;
console.log('post params: ', params);
// The X-Twilio-Signature header attached to the request
try{
Object.keys(params).sort().forEach(function(key) {
reqUrl = reqUrl + key + params[key];
});
var twilioSignature = crypto.createHmac('sha1', authToken).update(Buffer.from(reqUrl, 'utf-8')).digest('base64');
//const twilioSignature = req.header('HTTP_X_TWILIO_SIGNATURE');
console.log('twilioSignature: ', twilioSignature);
} catch(e){
console.log(e);
}
return client.validateRequest(
authToken,
twilioSignature,
url,
params
);
}我已经显式地尝试了设置头文件,但是没有用。我不知道twilio希望我做什么,也不知道如何修改头文件。
{
"status": "Error",
"error": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Message>Please call 408-xxx-xxxx to reschedule.</Message></Response>"
}我将其视为Twilio控制台中的正文,它有我需要的响应,但无法作为消息发送。
发布于 2018-09-11 17:55:50
Twilio开发者布道者在这里。
通过查看您的代码并亲自尝试,我看不出它有任何普遍的错误。不过,这里有两个潜在的缺点:
1)我看不到你的isFromTwilio函数。如果失败,可能会导致错误,然后在错误处理程序上返回JSON而不是XML。我不知道为什么它会用JSON中的TwiML进行回复。
2)当我没有在中间件链中包含body-parser时,我可以重现的另一个行为(除了在响应正文中没有发送TwiML之外)。这将导致req.body是未定义的,因此req.body.Body将抛出一个错误,然后捕获该错误并返回JSON。
您是否包含body-parser并将其作为中间件正确包含?你可以这样做:
const { urlencoded } = require('body-parser');
app.use(urlencoded({ extended: false });或者,如果您只想将其用于这一个端点,则可以将urlencoded({ extended: false })作为参数添加到请求处理程序之前:
app.post('/ui/sms', urlencoded({ extended: false }), function(req, res) {我希望这能帮到你。
干杯,
Dominik
https://stackoverflow.com/questions/52269344
复制相似问题