简体   繁体   English

禁用 Twilio webhook 响应内容类型错误

[英]Disable Twilio webhook response content type errors

I'm using Twilio webhooks with a third-party vendor called Adafruit IO.我正在与名为 Adafruit IO 的第三方供应商一起使用 Twilio webhook。 Twilio expects an XML response, but Adafruit returns a JSON response and I have no way of modifying the response. Twilio 期望 XML 响应,但 Adafruit 返回 JSON 响应,我无法修改响应。 My project works but I'm getting a lot of error code 12300 emails from Twilio ("Invalid Content-Type: application/json; charset=utf-8 supplied").我的项目有效,但我收到了很多来自 Twilio 的错误代码 12300 电子邮件(“无效的内容类型:应用程序/json;提供的 charset=utf-8”)。 Is there any way to disable the error message emails?有什么方法可以禁用错误消息电子邮件?

I couldn't find any way to ignore the response error or change the response, so I created a Twilio function to handle the webhook.我找不到任何方法来忽略响应错误或更改响应,所以我创建了一个 Twilio function 来处理 webhook。 It turned a trivial task into chore, but it works now, and it also allows me to offload some of the client end processing to Twilio.它将一项琐碎的任务变成了苦差事,但它现在可以工作了,而且它还允许我将一些客户端处理卸载到 Twilio。 Here's the function code if anyone is interested:如果有人感兴趣,这里是 function 代码:

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);
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM