其实做起来很简单。
正如@lizziepika 所说,你必须利用函数。
首先,假设您想从这样一个问题中收集数据:
任务示例
{
"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"
}
}
}
}
]
}
完成后,自动驾驶仪将重定向到 Twilio 功能。
可以在此处找到用于自动驾驶仪的 Twilio 功能的一个很好的示例。
https://github.com/twilio/autopilot-templates/blob/master/Functions/simple_response.js
我已经为我们的示例进行了调整。
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);
};
这样做的好处在于,您可以让函数响应任务中出现的任何内容。只要有效负载与 Autopilot 任务的形式相同,您就可以让该函数响应问题、操作、重定向等。