I'm trying to create a web form where users can input their phone number to initiate an SMS conversation run through Twilio's AutoPilot. I'm getting hung up between tutorials on Webhooks and tutorials on fetch-node and could use some help.
Goal is to:
- Accept phone number from web form
- Pass into Twilio Function
- Parse phone number
- Redirect to Twilio Autopilot to start SMS conversation
The web form is on a website hosted by Duda, their API documentation on getting form submissions is here: https://developer.duda.co/reference#get-contact-form-data
What I can't figure out:
I created a Function (code below) and put that webhook into the UI of my form - is that correct?
I'm getting no errors in the Twilio error log, which I'm taking to mean I'm not connected or passing data through to Twilio at all. Correct assumption?
The code. It's hacked together from several different places, so take it easy!
exports.handler = function(context, event, callback) {
const fetch = require('node-fetch');
fetch('https://api.duda.co/api/sites/multiscreen/get-forms/e5712b84', {
method: 'GET',
headers: {
'Authorization': 'Basic ZGZmOTIzMTA3Yzp3Rk9jMnRDTjRHbk8='
}
})
.then(response => response.json())
.then(json => {
console.log("Phone Number:");
console.log(json[0].Phone);
})
let actions = [];
let end = {
"redirect":{
"method": "POST",
"uri": "task://greeting"
}
};
actions.push(end);
let respObj = {
'actions': actions
};
callback(null,respObj);
};
Error Log from recommended solution in comments:
UnhandledPromiseRejectionWarning: Unhandled promise rejection: TypeError: Cannot read property 'fieldsData' of undefined
at Object.exports.handler (/var/task/handlers/ZNbaee490d934cb9ca27e0f14907c04089.js:2:33)
at Object.exports.handler (/var/task/node_modules/runtime-handler/index.js:310:10)
at Runtime.exports.handler (/var/task/runtime-handler.js:17:17)
at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)