I have developed a 2-way messaging flow in Twilio via Studio Flow and Messaging Service.
At the end of the conversation, need to collect all the SMS (outbound/inbound) and send it to our system via REST API as json.
I wrote a classic Twilio function and passed the “widgets” object to it.
By iterating the same I can collect all the messages but they are not is sequence.
Is there a way to get all the messages in sequence?
Alternatively, is there a way to get the widgets collection in the sequence in which they got executed?
This is how function is invoked from flow
Twilio function
exports.handler = function(context, event, callback) {
// const response = new Twilio.Response();
// response.appendHeader('Location', context.HTTP_REDIRECT_URL);
// callback(null, response);
//console.log('context', context.getTwilioClient());
console.log('event', event.all);
let allWidget = JSON.parse(event.all);
for (const key in allWidget) {
let currWidget = allWidget[key];
if (currWidget.outbound) {
console.log (key, '->', currWidget.outbound.To, currWidget.outbound.DateCreated, currWidget.outbound.Body, currWidget.outbound.Status);
}
//if (currWidget.incoming) {
// console.log (key, ':', currWidget.incoming.From, currWidget.incoming.DateCreated, currWidget.incoming.Body);
//}
if (currWidget.inbound) {
console.log (key, '<-', currWidget.inbound.From, currWidget.inbound.DateCreated, currWidget.inbound.Body);
}
}
};
user26321188 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.