Reputation: 1
I'm current doing a thesis about chatbot survey (Created through code and able to gather information through dialog on facebook/slack). Currently I'm having a problem about conversation flow in Twilio.
To simplify the conversation:
Upvotes: 0
Views: 952
Reputation: 21
It's actually pretty simple to do.
As @lizziepika mentioned, you have to utilize functions.
First, let's say you want to collect data from a question like so: TASKS Example
{
"actions": [
{
"collect": {
"name": "contact",
"questions": [
{
"question": "Are you a human?",
"name": "contact_human",
"type": "Twilio.YES_NO"
}
],
"on_complete": {
"redirect": {
"method": "POST",
"uri": "https://Your_Twilio_Function_Domain.twil.io/Function_Name"
}
}
}
}
]
}
On complete, the Autopilot will redirect to a Twilio Function.
A great example of a Twilio function for autopilot can be found here. https://github.com/twilio/autopilot-templates/blob/master/Functions/simple_response.js
I have tweaked it for our example.
exports.handler = function(context, event, callback) {
//we get the Memory from the answered questions.
let memory = JSON.parse(event.Memory);
//set up an array of object "actions" for the autopilot to continue.
let actions = [];
let responseItem;
//get the answer from Memory
let human = Memory.twilio.collected_data.contact.answers.contact_human.answer; //Yes or No
if(human === "Yes"){
responseItem = {
"say": "ANSWER YES - You are human"
};
actions.push(responseItem);
}else{
responseItem = {
"say": "ANSWER No - You are NOT human"
};
actions.push(responseItem);
}
responseItem = {
"redirect": {
"method": "POST",
"uri": "task://next_task"
}
};
actions.push(responseItem);
let respObj = {
"actions": actions
};
callback(null, respObj);
};
The beauty of this is that you can have the function respond with anything that would be present in a task. You can have the function respond with questions, actions, redirects, etc, as long as the payload is the same form as Autopilot Tasks.
Upvotes: 1
Reputation: 3300
Twilio developer evangelist here.
You can do if/else
using Autopilot with Twilio Functions, our serverless environment tool.
This Node.js quickstart has the first few steps you'd need to make a voice bot with Autopilot.
You could have one Autopilot task and then use a conditional in the Twilio Function the task redirects to to check what the user said. The Twilio Autopilot task could include this JSON:
{
"actions": [
{
"redirect": {
"uri": "https://YOUR-TWILIO-FUNCTION-URL.twil.io/actions",
"method": "POST"
}
}
]
}
And then the Function may include
exports.handler = function(context, event, callback) {
let responseObject = {};
let memory = JSON.parse(event.Memory);
console.log(memory.twilio.collected_data);
let userInput = memory.twilio.collected_data.your_collect_function_name.answers.your_question_name.answer;
console.log(num); //collected data from memory
if(userInput == "pie") {
//do whatever
}
else if (userInput == "cake") {
// do something else
}
else {
//do something else
}
};
Let me know if this helps at all!
Upvotes: 1