我正在与一家名为Adafuit IO的第三方供应商一起使用Twilio Webhook。Twilio需要一个XML响应,但Adafuite返回一个JSON响应,我无法修改该响应。我的项目有效,但我收到了很多来自Twilio的错误代码12300电子邮件("无效内容类型:application/json;提供了charset=utf-8"(。有什么方法可以禁用错误消息电子邮件吗?
我找不到任何方法来忽略响应错误或更改响应,所以我创建了一个Twilio函数来处理webhook。它将一项琐碎的任务变成了家务,但现在它可以工作了,而且它还允许我将一些客户端处理卸载到Twilio。如果有人感兴趣,下面是功能代码:
const axios = require('axios');
exports.handler = async (context, event, callback) => {
// Create a new message response object
const twiml = new Twilio.twiml.MessagingResponse();
// REST API base URL and any custom headers
const instance = axios.create({
baseURL: 'https://io.adafruit.com/api/v2/webhooks/feed/',
headers: { 'X-Custom-Header': 'Twilio' },
});
try {
const update = await instance.post('/<_place your Adafruit feed ID here_>/', {
value: {smsID: event.smsID,
phoneNumber: event.From,
message: event.Body}
}).catch(function (error) {
// Catch post failure and notify sender
twiml.message(`Something went wrong! ⛔`);
return callback(null, twiml);
});
// Add a message to the response to let the user know that everything worked
twiml.message(
`Message received. ☘️`
);
return callback(null, twiml);
} catch (error) {
// As always with async functions, you need to be sure to handle errors
console.error(error);
return callback(error);
}
};