如何在自动驾驶仪Twilio中重置流量?

如何在自动驾驶仪Twilio中重置流量?,twilio,chatbot,whatsapp,twilio-programmable-chat,Twilio,Chatbot,Whatsapp,Twilio Programmable Chat,我正在尝试取消重置流,同时回答收集问题,如果客户选择了错误选项并希望取消,这是必要的,但现在我刚刚退出收集添加验证以收集,当完成最大值尝试时,我重定向到主 我想写取消并重定向到Main,我尝试了一个函数,但是collect上的验证不允许我将重定向从函数发送到Main任务 有什么想法吗 非常感谢 这是我的职责: exports.handler = function(context, event, callback) { var got = require('got'); var value =

我正在尝试取消重置流,同时回答收集问题,如果客户选择了错误选项并希望取消,这是必要的,但现在我刚刚退出收集添加验证以收集,当完成最大值尝试时,我重定向到主

我想写取消并重定向到Main,我尝试了一个函数,但是collect上的验证不允许我将重定向从函数发送到Main任务

有什么想法吗

非常感谢

这是我的职责:

exports.handler = function(context, event, callback) {
var got = require('got');

var value = event.CurrentInput;

if(true){
    let responseObject = {
        "actions": [
            {
                "redirect": "task://greetings"
            }]
    };
    callback(null, responseObject); 
}};
后来我在一个webhook中调用了它,使用了onCollectAttempt,但它没有发生任何事情,只是继续收集流


这里是Twilio开发者福音传道者

这不是一个简单的方法,因为
Collect
函数的当前JSON bin不允许“取消”或“重置”,但这里有一个潜在的解决方法,可用于动态
Collect
(您使用一个将其写入Node.js中,而不是将其写入自动驾驶仪控制台的JSON bin中

我们有: 1.我们需要的收集问题集(为了涵盖单个函数中的许多收集,我们需要一些额外的东西,比如从数据库中提取/动态传递) 2.我们可以使用静态单词检查的用例的代理关键字,例如,指示用户在任何时候键入“代理”以指向人类

//code courtesy of Evangelos Resvanis, Senior Solutions Engineer at Twilio 
const questions = [
    {
        "question": "You want to book appointment?",
        "name": "book"
    },
    {
        "question": "When do you need the appointment for?",
        "name": "appointment_date"
    },
    {
        "question": "And what time?",
        "name": "appointment_time"
    }
];
const agent_keywords = ["agent", "i want to speak to agent", "let me talk to someone"];
var stringSimilarity = require('string-similarity');
const COLLECT_TASK = "collect_task";
/*************************** Main Function ********************/
/*
This function generates the collect questions in steps. Each step we enter the function again and check the user input. We keep a counter to exit when 
the collect questions are exhausted.
Two main use cases examined (any use case can be built)
- We want to break the collect on specific user input/keywords e.g. "agent". We use string comparison (Use Case 1)
- We want to send the user input to the Queries API to see if they provided fields. We use the API to avoid the Collect validate, as we can't break the Collect
from there (Use Case 2)
Uncomment/comment each use case to examine the results.
*/
exports.handler = function(context, event, callback) {
    let count;
    //If event.count is set, then we are at least in the first answer from the collect list
    if(event.count) {
        count = parseInt(event.count);
        //Use case 1
        /*var matches = stringSimilarity.findBestMatch(event.CurrentInput, agent_keywords);
        if (matches.bestMatch.rating > 0.5){
            callback(null, {"actions": [{"say": "Sure, handing over to one of our agents now..."}]});
        }
        else buildCollect(count);*/
        //Use case 2
        getFieldsOrNextTask(event.CurrentInput, count);
    }
    else {count = 1;buildCollect(count, null);}
    /*
    Send the user input to Queries endpoint
    - If there are fields and confidence is high, assume user input is ok, remember the value and continue to next question (improvement: check the exact type of field found)
    - If there are no fields, check the results of the task and confidence, assuming the user wants to break the collect. Choose the target task based on rules you test, excluding the collect task
    */
    async function getFieldsOrNextTask(input, count){
        const client = require('twilio')(context.ACCOUNT_SID, context.AUTH_TOKEN);
        let fields_result = await client.autopilot.assistants(context.AUTOPILOT_SID).queries.create({language: 'en-US', query: input, tasks: COLLECT_TASK});
        if(fields_result.results.task_confidence < 0.4 || !fields_result.results.fields.length){
            let all_tasks_string = "";
            let tasks = await client.autopilot.assistants(context.AUTOPILOT_SID).tasks.list();
            for(let i = 0; i < tasks.length; i++){
                if (tasks[i].uniqueName === COLLECT_TASK)continue;
                let name = tasks[i].uniqueName;
                let unique_name = i === tasks.length-1 ? name : name + ",";
                all_tasks_string += unique_name;
            }
            console.log("All tasks without the Collect task: " + all_tasks_string);
            let task_result = await client.autopilot.assistants(context.AUTOPILOT_SID).queries.create({language: 'en-US', query: event.CurrentInput, tasks: all_tasks_string});
            let task_confidence = task_result.results.task_confidence;
            let chosen_task = parseFloat(task_confidence) < 0.4 && !task_result.results.next_best_task ? "fallback": task_result.results.task;
            console.log("Chosen new task to break Collect is: " + chosen_task);
            callback(null, {"actions": [{"redirect": "task://" + chosen_task}]});        
        }
        else{
            console.log("seemed a valid field of the Collect task, remember it and move to next question");
            buildCollect(count, event.CurrentInput);
        }  
    }
    function buildCollect(count, userInputToRemember){
        console.log("Current count: " + count);
        if(count > questions.length){
            callback(null, {"actions": [{"say": "Thank you, that is the end of our questions, moving to the next part of the flow"}]});
        }
        else{
            //Create the current collect object and increase the count in on_complete for the next iteration
            let name = "collect_" + count;
            let on_complete = {
                    "redirect": "https://" + context.DOMAIN_NAME + "/dynamic-collect?count=" + (count+1)
            };
            let collect_object = {};
            collect_object.name = name;
            collect_object.questions = [];
            collect_object.questions[0] = questions[count-1];
            collect_object.on_complete = on_complete;
            console.log("Current Collect object: " + JSON.stringify(collect_object));
            if(userInputToRemember){
                let remember_var = questions[count-1].name;
                console.log(remember_var);
                callback(null, {"actions": [{"remember": {remember_var: userInputToRemember}},{"collect": collect_object}]});
            }
            else{
                callback(null, {"actions": [{"collect": collect_object}]});
            }
        }
    }
};
//代码由Twilio高级解决方案工程师Evangelos Resvanis提供
常量问题=[
{
“问题”:“您想预约吗?”,
“名称”:“书”
},
{
“问题”:“你什么时候需要预约?”,
“姓名”:“约会日期”
},
{
“问题”:“什么时候?”,
“姓名”:“预约时间”
}
];
const agent_keywords=[“agent”,“我想和agent说话”,“让我和某人说话”];
var stringSimilarity=require('string-similarity');
const COLLECT_TASK=“COLLECT_TASK”;
/***************************主要功能********************/
/*
此函数分步骤生成收集问题。每一步我们都会再次进入该函数并检查用户输入。我们会保留一个计数器,以便在
收集的问题已经穷尽了。
检查了两个主要用例(可以构建任何用例)
-我们希望打破对特定用户输入/关键字的收集,例如“代理”。我们使用字符串比较(用例1)
-我们希望将用户输入发送到查询API,以查看它们是否提供了字段。我们使用API来避免Collect验证,因为我们不能破坏Collect验证
从那里开始(用例2)
取消注释/注释每个用例以检查结果。
*/
exports.handler=函数(上下文、事件、回调){
让我们数一数;
//如果设置了event.count,那么我们至少在收集列表的第一个答案中
if(event.count){
count=parseInt(event.count);
//用例1
/*var matches=stringSimilarity.findBestMatch(event.CurrentInput,agent_关键字);
如果(matches.bestMatch.rating>0.5){
回调(null,{“actions”:[{“say”:“当然,现在就交给我们的一个代理…”}]});
}
否则,收集(计数)*/
//用例2
getFieldsOrNextTask(event.CurrentInput,count);
}
else{count=1;buildCollect(count,null);}
/*
将用户输入发送到查询端点
-如果有字段且置信度高,则假设用户输入正常,记住值并继续下一个问题(改进:检查找到的字段的确切类型)
-如果没有字段,请检查任务结果和置信度,假设用户希望中断收集。根据您测试的规则选择目标任务,不包括收集任务
*/
异步函数getFieldsOrNextTask(输入,计数){
const client=require('twilio')(context.ACCOUNT\u SID,context.AUTH\u令牌);
让fields_result=wait client.autopilot.assistants(context.autopilot_SID).querys.create({language:'en-US',query:input,tasks:COLLECT_TASK});
if(字段_result.results.task _置信度<0.4 | |!字段_result.results.fields.length){
让所有_任务_string=“”;
让tasks=wait client.autopilot.assistants(context.autopilot_SID).tasks.list();
for(设i=0;i问题长度){
回调(null,{“actions”:[{“say”:“谢谢,我们的问题到此结束,转到流程的下一部分”}]});
}
否则{
//创建当前collect对象,并增加下一次迭代完成时的计数
让name=“collect”+计数;
让你完成={
“重定向”:“https://”+context.DOMAIN\u NAME+“/dynamic collect?co