我在控制对话流的beginDialogAction方法上遇到了一些麻烦,希望提供一些建议。我刚接触JavaScript。

我想做的是将关键字的匹配范围限制到主对话框的上下文中。我的理解是beginDialogAction方法是执行此操作的最佳方法。我的问题是,一旦使用beginDialogAction触发了我的对话框,就会将新对话框添加到堆栈中,beginDialogAction会继续侦听另一个匹配项。这意味着,如果我的用户恰巧在流程中与我的触发词相匹配,它将改变对话的主题。

我想要清除的是在开始beginDialogAction中指定的新对话框之前清除对话框堆栈,但是我一直无法弄清楚到目前为止。任何建议将不胜感激!

码:

var bot = new builder.UniversalBot(connector, function (session) {
    session.send("Hi, good to meet you! I'm here to help you request services. \n\n Please select an action from below.");
    session.beginDialog('main');
});
bot.dialog('main', [
    function(session){
    var msg = new builder.Message(session);
    msg.attachmentLayout(builder.AttachmentLayout.carousel)
    msg.attachments([
        new builder.HeroCard(session)
            .title("Request Design Services")
            .subtitle("I can send out a request for any design services you need, right from here!")
            .buttons([
                builder.CardAction.imBack(session, "I'd like to request design services.", "New Request")
            ]),
        new builder.HeroCard(session)
            .title("Project Finacials Request")
            .subtitle("I can pull finance information from any project within your organization.")
            .buttons([
                builder.CardAction.imBack(session, "I'd like to request financials.", "New Request")
            ])
    ]);
    session.send(msg)
}]).beginDialogAction('begindesign', 'design',{ matches: /design/i }).beginDialogAction('beginfinance', 'finance',{ matches: /financials/i });

bot.dialog('design', [
...
]);

bot.dialog('finance', [
...
]);

最佳答案

您可以根据需要使用onSelectAction属性。您可以参考the source code进行描述:


  / **
       *(可选)自定义处理程序,只要触发该操作即可调用。这让你
       *自定义动作的行为。例如,您可以在之前清除对话框堆栈
       *将启动新对话框,更改默认行为,即仅推送新对话框
       *对话到堆栈的末尾。
       *
       *请务必注意,这不是瀑布,如果您要调用next()
       *希望动作默认行为运行。
       * /


并且请考虑以下代码片段:

bot.dialog('mainMenu', [
    (session, args, next) => {
        builder.Prompts.text(session, 'Hi there! What can I do for you today?', {
            retryPrompt: 'Hi there! What can I do for you today?'
        });
    },
    (session, results) => {
        session.endConversation('Goodbye!');
    }
])
.beginDialogAction('sportsAction', 'Sports', {
    matches: /^sports$/i,
})
.beginDialogAction('cookingAction', 'Cooking', {
    matches: /^cooking$/i,
    onSelectAction: (session, args, next) => {
        session.clearDialogStack();
        next();
    }
})
bot.dialog('Sports', [
    (session, args, next) => {
        session.send(`current dialog length: ${session.sessionState.callstack.length}`);
        session.endDialog('Sports here');
    }
]);
bot.dialog('Cooking', [
    (session, args, next) => {
        session.send(`current dialog length: ${session.sessionState.callstack.length}`);
        session.endDialog('Cooking here');
    }
])

09-25 15:46