Twilio SMS Webhook
Moderators: adafruit_support_bill, adafruit
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
- rdagger
- Posts: 88
- Joined: Mon Nov 30, 2015 5:44 pm
Twilio SMS Webhook
I'm working on a CircuitPython project that will be controlled by SMS text messages. I'm using a Twilio phone number to receive the text messages and fire a webhook on an Adafruit IO feed. The project is working and my CircuitPython board is able to receive and display the text messages. The only problem is that I'm getting lots of error message emails from Twilio because of the Adafruit IO webhook response. Adafruit IO is responding with JSON and Twilio expects an XML response. Is there any way to modify the Adafruit IO response?
- jwcooper
- Posts: 1004
- Joined: Tue May 01, 2012 9:08 pm
Re: Twilio SMS Webhook
As of now, we only support json on the feed webhooks.
What is the purpose of the webhook? Is it supposed to be going to twilio? If it is, you may need to process it first, then send it off to twilio as they are expecting a format we don't currently support.
What is the purpose of the webhook? Is it supposed to be going to twilio? If it is, you may need to process it first, then send it off to twilio as they are expecting a format we don't currently support.
- rdagger
- Posts: 88
- Joined: Mon Nov 30, 2015 5:44 pm
Re: Twilio SMS Webhook
Is there a web page where I can submit an Adafruit IO feature request? Or is there a Github repo where I might be able to contribute?
I don't need Adafruit IO to be able to process the XML because I am using the raw feature and parsing it myself. I just would like the option to have Adafruit's response to the webhook post be in XML format. I don't think the content string matters. It could probably just be <Response/>. It's the response that is causing the problem. The webhook response just acknowledges receipt by Adafruit to Twilio.
The purpose of the webhook is to transmit the incoming SMS text messages from Twilio to the Adafruit IO feed. This allows real time control of a CircuitPython project using any SMS app.
- rdagger
- Posts: 88
- Joined: Mon Nov 30, 2015 5:44 pm
Re: Twilio SMS Webhook
I did find a workaround. I created a Twilio function to handle the webhook. The function is a node.js program that is called upon an incoming SMS message and uses the Axios library to post to the Adafruit IO feed. It turned a trivial implementation into a chore but it now works and I was able to offload all the raw parsing to Twilio instead of handling it on the CircuitPython board. It also provides better feedback to the sender. Here's my code:
Code: Select all
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);
}
};
- jwcooper
- Posts: 1004
- Joined: Tue May 01, 2012 9:08 pm
Re: Twilio SMS Webhook
Thank you for posting your solution and the corresponding example code. You are almost certainly helping someone else out that will run into this same issue in the future (at least until we can support XML via webhooks!).
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.