我在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控制台中将其视为正文,它具有我需要的响应但无法作为消息发送..
Twilio开发者传播者在这里。
从查看代码并自己尝试,我看不出任何一般错误。这里有两个潜在的失败点:
1)我看不到你的isFromTwilio
功能。如果那个失败,可能会导致错误,然后在错误处理程序上返回JSON而不是XML。我不知道为什么它会用JSON中的TwiML回复。
2)我可以重现的其他行为(除了TwiML没有在响应体中发送)是我在中间件链中不包含body-parser
的时候。这将导致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) {
我希望这有帮助。
干杯,
多米尼克