Skip to content

Instantly share code, notes, and snippets.

@vernig
Created May 28, 2020 08:09
Show Gist options
  • Save vernig/6562a4035265f32e9c98721dea5f4578 to your computer and use it in GitHub Desktop.
Save vernig/6562a4035265f32e9c98721dea5f4578 to your computer and use it in GitHub Desktop.
class StudioResponsesParser {
constructor(twilioAccountSid, twilioAuthToken, studioFlowSid) {
this.twilioClient = require('twilio')(twilioAccountSid, twilioAuthToken);
this.setFlowSid(studioFlowSid);
}
setFlowSid(studioFlowSid) {
this.studioFlowSid = studioFlowSid;
this.studioFlow = this.twilioClient.studio.v1.flows(studioFlowSid);
}
_extractResponse(executionSid) {
let response = {};
return this.studioFlow
.executions(executionSid)
.executionContext()
.fetch()
.then((execution) => {
// Extract responses from widgets. Each "send & wait" widget has
// an `inbound` property that contain the response to that widget.
// To extract the response, use the `Body` property of `inbound`
const widgets = execution.context.widgets;
for (const key in widgets) {
if (widgets[key].inbound) {
response[key] = widgets[key].inbound.Body;
}
}
return Promise.resolve(response);
})
.catch((err) => {
console.log('Error in _extractResponse:' + err);
});
}
getResponses(fromDate, toDate) {
let responses = [];
// Fetch all Studio executions
return this.studioFlow.executions
.list()
.then((executions) => {
return Promise.all(
// Extract responses
executions.map((execution) => this._extractResponse(execution.sid))
);
})
.catch((error) => {
console.log(error);
});
}
}
module.exports = StudioResponsesParser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment