Repeating a dialog step based on validation

帅比萌擦擦* 提交于 2021-02-20 01:53:38

问题


I'm currently building a provisioning bot using v4 of the Bot Framework and I've integrated it with the Microsoft Graph.

The Microsoft Graph is being used to validate user inputs, so in this scenario, it's checking to see if the group name already exists. However, the issue I'm running into is getting the bot to repeat the previous step if the validation finds the group exists.

I've read through the forum and seen a number of solutions, particularly, I have come across the step.activeDialog.state['stepIndex']-2 approach, but have been unable to get it to work. Is this a viable solution for going back a step in NodeJS or should I be looking at another approach?

async nameStep(step) {
        // User selected a group type and now is required to enter the name of the group
        step.values.sitetype = step.result.value;
        return await step.prompt(NAME_PROMPT, 'What do you want to name it');
    }

    async ownerStep(step) {
        // Retrieve the value from the previous step and check against the Microsoft Graph to see if the name has been used previously
        step.values.name = step.result;
        const getToken =
            await axios.post(TOKEN_ENDPOINT, qs.stringify(postData))
            .then(response => {
                return {
                    headers: {
                        'Authorization': 'Bearer ' + response.data.access_token
                    }
                }
            })
            .catch(error => {
                console.log(error);
            });

        const graphCall =
            await axios.get("https://graph.microsoft.com/v1.0/groups?$filter=startswith(displayName,'" + `${step.result}` + "')", getToken)
            .then((response) => {
                if (response.data.value[0] != null) {
                    return true;
                }
            })
            .catch((error) => {
                console.log(error);
            })

        if (!graphCall) {
            return await step.prompt(NAME_PROMPT, 'What is your email address');
        } else {
            await step.context.sendActivity("Group already exists");
            return await step.activeDialog.state['stepIndex']-2
        }
    }

Thanking you in advance


回答1:


You can achieve this by use of a component dialog. Essentially, you extrapolate the steps you would like to repeat into a separate dialog that is called only from within the current (parent) dialog. In the parent, you institute your checks. When a check fails, the component dialog is called again. If it succeeds, the parent dialog continues on.

In the code below, my parent dialog immediately calls the component dialog for a first pass thru presenting the user with two options. Each will send a pre-determined text value which is checked to see if a LUIS intent exists for it.

The first option, "Hello", will succeed with an intent having been found. It then restarts the parent dialog. The parent dialog starts with the text "You have a choice to make in life..." which will re-display as the parent dialog begins again.

The second option will fail and returns the user to the component dialog to try again. The component dialog starts with "Text me something! I'll see if my maker setup a LUIS intent for it." This text will display when either button is clicked because the component dialog is run in both instances. However, only this text will display when LUIS fails to find an intent and restarts the component dialog.

Side note - the parent dialog in this example is, in fact, a component dialog to my main dialog which is why it is exported at the end. So, yes, you can have component dialogs within component dialogs.

Parent Dialog:

const { ComponentDialog, WaterfallDialog } = require('botbuilder-dialogs');
const { LuisRecognizer } = require('botbuilder-ai');

const { ChoiceDialogSub, CHOICE_DIALOG_SUB } = require('./choiceDialog_Sub');
const CHOICE_DIALOG = 'choiceDialog';

class ChoiceDialog extends ComponentDialog {
    constructor(id) {
        super(id);

        this.addDialog(new ChoiceDialogSub(CHOICE_DIALOG_SUB));
        this.addDialog(new WaterfallDialog(CHOICE_DIALOG, [
            this.welcomeStep.bind(this),
            this.choiceLuisStep.bind(this)
        ]));

        this.initialDialogId = CHOICE_DIALOG;

        try {
            this.recognizer = new LuisRecognizer({
                applicationId: process.env.LuisAppId,
                endpointKey: process.env.LuisAPIKey,
                endpoint: `https://${ process.env.LuisAPIHostName }`
            }, {}, true);
        } catch (err) {
            console.warn(`LUIS Exception: ${ err } Check your LUIS configuration`);
        }
    }

    async welcomeStep(stepContext) {
        await stepContext.context.sendActivity('You have a choice to make in life...');
        return await stepContext.beginDialog(CHOICE_DIALOG_SUB);
    }

    async choiceLuisStep(stepContext) {
        if (stepContext.context.activity.text) {
            const stepResults = stepContext.context.activity.text;
            const recognizerResult = await this.recognizer.recognize(stepContext.context);
            const intent = await LuisRecognizer.topIntent(recognizerResult);

            if (intent === 'Greeting') {
                await stepContext.context.sendActivity(`'${ stepResults }' identified in the {${ intent }} intent.`);
                return await stepContext.beginDialog(CHOICE_DIALOG);
            } else {
                await stepContext.context.sendActivity(`No LUIS intent was found for '${ stepResults }'.`);
                return await stepContext.beginDialog(CHOICE_DIALOG_SUB);
            }
        } else {
            await stepContext.context.sendActivity('I need text, fool!');
            return await stepContext.next();
        }
    }
}

module.exports.ChoiceDialog = ChoiceDialog;
module.exports.CHOICE_DIALOG = CHOICE_DIALOG;

Component Dialog:

const { ChoicePrompt, ChoiceFactory, ComponentDialog, WaterfallDialog } = require('botbuilder-dialogs');

const CHOICE_DIALOG_SUB = 'choiceDialogSub';
const CHOICE_DIALOG_SUB_PROMPT = 'choicePromptSub';

class ChoiceDialogSub extends ComponentDialog {
    constructor(id) {
        super(id);

        this.addDialog(new ChoicePrompt(CHOICE_DIALOG_SUB_PROMPT))
            .addDialog(new WaterfallDialog(CHOICE_DIALOG_SUB, [
                this.choiceStep.bind(this)
            ]));

        this.initialDialogId = CHOICE_DIALOG_SUB;
    }

    async choiceStep(stepContext) {
        const choices = ['Hello', 'No soup for you!'];
        return await stepContext.prompt(CHOICE_DIALOG_SUB_PROMPT, {
            prompt: "Text me something! I'll see if my maker setup a LUIS intent for it.",
            choices: ChoiceFactory.toChoices(choices)
        });
    }
}

module.exports.ChoiceDialogSub = ChoiceDialogSub;
module.exports.CHOICE_DIALOG_SUB = CHOICE_DIALOG_SUB;

Hope of help!



来源:https://stackoverflow.com/questions/61601734/repeating-a-dialog-step-based-on-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!