简体   繁体   中英

Disable Twilio webhook response content type errors

I'm using Twilio webhooks with a third-party vendor called Adafruit IO. Twilio expects an XML response, but Adafruit returns a JSON response and I have no way of modifying the response. 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"). 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. 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. Here's the function code if anyone is interested:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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