显示卡片后,如何继续sdk v4.4中的瀑布对话框流程

时间:2019-06-07 05:13:27

标签: node.js botframework

我正在引用示例代码,并尝试了简单的方案来显示卡。 卡显示后如何继续Waterfall_Dialog?

我指的是示例代码05.multi-turn-prompt和06.using-cards-(https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/javascript_nodejs/05.multi-turn-prompt/dialogs/userProfileDialog.js

      ...this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
        this.initialData.bind(this), 
        this.displayCard.bind(this),    
        this.text.bind(this)
    ])); ...

async run(turnContext, accessor) {
   ...}

async initialData(step) {
    return await step.prompt(NAME_PROMPT, `Type anything to display card`);
} 

async displayCard(step) {
   await step.context.sendActivity({ attachments: [this.createAdaptiveCard()]});                
}

async text(step) {
    console.log("step.res"+step.context.activity.text);
    await step.context.sendActivity("Thank you for selecting an option");

}

async displayCard(step) {
   await step.context.sendActivity({ attachments: [this.createAdaptiveCard()]});                
  return await this.text(step);
} 

显示卡片并继续瀑布对话框。

显示卡片后,它应继续执行流程并显示“谢谢您选择选项”,

  1. 但是它将转到Begin_Dialog并询问“输入任何内容以显示卡”

  2. 如果我通过调用下一个对话框尝试其他方法。 我收到“糟糕。出问题了!” “ [onTurnError]:TypeError:无法读取未定义的属性'status'”

2 个答案:

答案 0 :(得分:0)

要进入下一个对话框,您需要在包含卡片的步骤之后调用NextAsync。

例如,

private async Task<DialogTurnResult> StartSelectionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Set the user's age to what they entered in response to the age prompt.
    var userProfile = (UserProfile)stepContext.Values[UserInfo];
    userProfile.Age = (int)stepContext.Result;

    if (userProfile.Age < 25)
    {
        // If they are too young, skip the review selection dialog, and pass an empty list to the next step.
        await stepContext.Context.SendActivityAsync(
            MessageFactory.Text("You must be 25 or older to participate."),
            cancellationToken);
        return await stepContext.NextAsync(new List<string>(), cancellationToken);
    }
    else
    {
        // Otherwise, start the review selection dialog.
        return await stepContext.BeginDialogAsync(nameof(ReviewSelectionDialog), null, cancellationToken);
    }
}

在以上代码段中,如果用户年龄不正确,则会显示一条消息,说明您的年龄。调用return await stepContext.NextAsync(),将对话框前进到下一步。如果用户已成年,则将开始一个新对话框(“ ReviewSelectionDialog”)。摘录来自here上的“使用分支和循环创建高级对话流”文档,您可以参考。

希望有帮助!

答案 1 :(得分:0)

在这里,我附加了多个对话框的代码。您只需需要在对话框集中注册对话框,然后再开始或替换对话框即可。

  

this.flowBuilderDialog this.faqDialog 是对话框对象, DialogsNameList.FLOW_BUILDER_DIALOG DialogsNameList.FAQ_DIALOG 是对话框的名称。

class GreetingDialog extends ComponentDialog {
constructor(dialogId, userInfoPropertyAccessor, flowNextQuestionAccessor, flowBuilderConversationAccessor, flowBuilderDialog, faqDialog) {
    super(dialogId);

    if (!userInfoPropertyAccessor) throw new Error(`[MainDialog]: Missing parameter \'userInfoPropertyAccessor\' is required`);
    if (!flowBuilderDialog) throw new Error(`[MainDialog]: Missing parameter \'flowBuilderDialog\' is required`);
    if (!faqDialog) throw new Error(`[MainDialog]: Missing parameter \'faqDialog\' is required`);
    // Define the greeting dialog and its related components.

    let greetingChoicePrompt = new ChoicePrompt(GREETING_CHOICE_PROMPT);
    greetingChoicePrompt.style = ListStyle.heroCard;

    this.addDialog(greetingChoicePrompt)
        .addDialog(new WaterfallDialog(GREETING_WATERFALL_DIALOG, [
            this.introStep.bind(this),
            this.actStep.bind(this),
            this.finalStep.bind(this)
        ]));

    this.flowBuilderDialog = flowBuilderDialog;
    this.faqDialog = faqDialog;

    this.initialDialogId = GREETING_WATERFALL_DIALOG;
    this.flowNextQuestionAccessor = flowNextQuestionAccessor;
    this.flowBuilderConversationAccessor = flowBuilderConversationAccessor;
}

/**
 * The run method handles the incoming activity (in the form of a TurnContext) and passes it through the dialog system.
 * If no dialog is active, it will start the default dialog.
 * @param {*} turnContext
 * @param {*} accessor
 */

async run(turnContext, accessor) {
    const dialogSet = new DialogSet(accessor);
    dialogSet.add(this);
    dialogSet.add(this.flowBuilderDialog);
    dialogSet.add(this.faqDialog);

    const dialogContext = await dialogSet.createContext(turnContext);

    const results = await dialogContext.continueDialog();
    if (results.status === DialogTurnStatus.empty) {
        await dialogContext.beginDialog(this.id);
    }
}


async introStep(stepContext) {
    return await stepContext.prompt(GREETING_CHOICE_PROMPT, {
        prompt: 'How can I help you today?',
        choices: ChoiceFactory.toChoices(['Flow Builder', 'FAQ'])
    });
}

async actStep(stepContext) {
    console.log("Step: Result", stepContext.result);

    switch (stepContext.result.value) {
        case 'Flow Builder':
            await this.flowNextQuestionAccessor.set(stepContext.context, 0);
            await this.flowBuilderConversationAccessor.set(stepContext.context, '');
            return await stepContext.replaceDialog(DialogsNameList.FLOW_BUILDER_DIALOG);
        case 'FAQ':
            return await stepContext.replaceDialog(DialogsNameList.FAQ_DIALOG);
    }
}

async finalStep(stepContext) {
    return await stepContext.replaceDialog(this.initialDialogId, {restartMsg: 'What else can I do for you?'});
}

}