Bot框架:多步骤瀑布

时间:2016-05-09 03:33:24

标签: node.js bots botframework

我想发送类似于此的机器人

"[Person] wants to meet at [Place] at [Date]"
然而,如果这个人错过了一些信息,我希望机器人一块一块地要求它。

例如,如果一个人写道:

"Let's meet!"

机器人会询问一系列问题以满足所有数据要求。类似的东西:

  1. 我将会见谁?
  2. 他们应该在哪里见面?
  3. 什么日期和时间?
  4. 如果这个人问的是:

    "Alex would like to meet tomorrow"
    

    然后机器人只会问

    "Where should they meet?"
    

    完成所有必需的数据后,它会发送如下响应:

    "Great, I will meet [Person] in [Location] at [DateTime]"
    

    我一直在尝试这样的方法,运气不佳,并且对会话进行了太多的调用.EndDialog()"错误:

    dialog.on('Meeting', [
        function (session, results, next) {
            var person = builder.EntityRecognizer.findEntity(results.entities, 'Person');
            if(!person){
                builder.Prompts.text(session, prompts.meetingPersonMissing); 
            } else {
                next({ response: {
                        person: person.entity
                    }
                });
            }
        },
        function (session, results, next) {
            var location = builder.EntityRecognizer.findEntity(results.entities, 'location');
            if(!location){
                builder.Prompts.text(session, prompts.meetingLocationMissing); 
            } else {
                // Pass title to next step.
                next({ response: {
                        person: results.person,
                        location: location.entity
                    }
                });
            }
        },
        function (session, results, next) {
            var time = builder.EntityRecognizer.findEntity(results.entities, 'builtin.datetime.date');
            if(!time){
                builder.Prompts.text(session, prompts.meetingTimeMissing); 
            } else {
                next({ response: {
                        person: results.person,
                        location: results.location,
                        time: time.entity
                    }
                });
            }
        },
        function (session, results) {
            if (results.response) {
               session.send(prompts.meetingSubmited);
            } else {
                session.send(prompts.canceled);
            }
        }
    ]);
    

    尝试失败#2(不再传递来自' next()')的数据。这导致相同的EndDialog错误

    dialog.on('Meeting', [
        function (session, results, next) {
            var person = builder.EntityRecognizer.findEntity(results.entities, 'Person');
            var location = builder.EntityRecognizer.findEntity(results.entities, 'location');
            var time = builder.EntityRecognizer.findEntity(results.entities, 'builtin.datetime');
    
            session.messageData = {
                person: person || null,
                location: location || null,
                time: time || null
            }
    
            if(!session.messageData.person){
                builder.Prompts.text(session.messageData.person, prompts.meetingPersonMissing); 
            } else {
                next();
            }
        },
        function (session, results, next) {    
            if(!session.messageData.location){
                builder.Prompts.text(session.messageData.location, prompts.meetingLocationMissing); 
            } else {
                next();
            }
        },
        function (session, results, next) {
            if(!session.messageData.time){
                builder.Prompts.text(session.messageData.time, prompts.meetingTimeMissing); 
            } else {
                next();
            }
        },
        function (session, results) {
            debugger;
            if (results.response) {
               session.send('Meeting scheduled');
            } else {
                session.send(prompts.canceled);
            }
        }
    ]);
    

    更新#3:在此版本中,如果话语不包含任何相关实体,则效果很好。例如"我们可以见面吗?"工作正常。

    这个版本的问题是当我尝试"我们能明天见面吗?"。明天被成功识别为日期时间实体,但是一旦它到达此块中的next()

     function getDate(session, results){
        session.dialogData.topic = results.response;
    
        if(session.dialogData.date){
            next();   
         }else{
            builder.Prompts.time(session, "What date would you like to meet?");   
         }
    
    }
    

    它失败并给了我相同的Too many calls to to session.EndDialog()个问题

    dialog.on('Meeting', [meetingQuery, getDate, getTime, respondMeeting]);
    
    function meetingQuery(session, results){
    
        if (results.response) {
            session.dialogData.date = builder.EntityRecognizer.resolveTime([results.response]);
        }
    
    
        session.userData = {
            person: session.message.from.name
        }
    
        builder.Prompts.text(session, "Hi "+ session.userData.person +"!\n\n What is the meeting in reference too?");
    
    }
    
    function getDate(session, results){
        session.dialogData.topic = results.response;
    
        if(session.dialogData.date){
            next();   
         }else{
            builder.Prompts.time(session, "What date would you like to meet?");   
         }
    
    }
    
    function getTime(session, results){
        if (results.response) {
            session.dialogData.date = builder.EntityRecognizer.resolveTime([results.response]);
        }
    
    
         if(session.dialogData.time){
            next();   
         }else{
            builder.Prompts.choice(session, "Your professor has the follow times availeble?", ["1pm", "2pm", "3pm"]);   
         }
    }
    
    function respondMeeting(session, results){
        var time = results.response;
        session.send("Your meeting has been schedueld for " + session.dialogData.date + " at " + time.entity + ". Check your email for the invite.");
    }
    

3 个答案:

答案 0 :(得分:1)

试试这个,它适用于我的机器人。

dialog.on('QuieroQueMeLlamen', [
    function (session, args) {
    	var Telefono = builder.EntityRecognizer.findEntity(args.entities, 'Usuario::Telefono');
        
        if(!Telefono){
        	builder.Prompts.number(session, "Dame un número telefónico donde pueda llamarte una de nuestras enfermeras (sin espacios ni caracteres: ej: 3206907529)");
        }      
        
    },
    function (session, results) {
        if (results.response) {
        	//IF STRLEN es un celular entonces:
            session.send("Te estamos llamando al número %s, por favor contesta", results.response);
        } 
    }
]);

答案 1 :(得分:0)

您不应将参数传递给next()函数,而是使用session.YOUVARIABLES通过瀑布存储数据。

类似的东西:

function (session, results, next) {
    var person = builder.EntityRecognizer.findEntity(results.entities, 'Person');
    if(!person){
        builder.Prompts.text(session, prompts.meetingPersonMissing); 
    } else {
        session.person = person.entity
        next();
    }
}

答案 2 :(得分:0)

我认为你应该访问

results.response.person 

而不是

results.person 

同样适用于此瀑布的其他闭包。

相关问题