【问题标题】:Botframework with NodeJS while prompt system doesn't read what user input the value带有NodeJS的Botframework,而提示系统不读取用户输入的值
【发布时间】:2019-04-11 23:07:26
【问题描述】:

Azure 机器人框架 SDK4 NodeJS

在对话状态下,我在异步函数中使用“return await step.prompt”。但是,一旦用户输入该值,它就不会将用户输入值视为提示输入,而是将 luisrecognizer 用于匹配。

我已经为不同的对话框编写了类似的代码,它可以正常工作。

恳请您提供一些有价值的意见。

Azure 机器人框架 SDK4 NodeJS

this.addDialog(new WaterfallDialog('OrderStatusDialog', [
   async function(step) {
     return await step.context.sendActivity(reply);
   },
   async function(step) {
     var cardValue = step.context.activity.value;
     if (cardValue) {
        if (cardValue.action == "Order_Number") {
           return await step.prompt('textPrompt', 'enter order number');
        } else if (cardValue.action == "Serial_Number") {
           return await step.prompt('textPrompt', 'enter serial number');
        } else {
        }
     }
     return await step.next();
     // return await step.endDialog();
  },
  async function (step) {
     var cardValue = step.context.activity;
     console.log("****** cardValue");
     console.log(cardValue);
     console.log("****** step After");
     console.log(step);
     return await step.endDialog();
  }
]));

在提示步骤它应该读取用户输入的值。当我控制步骤时,堆栈也是空的..

【问题讨论】:

    标签: node.js botframework


    【解决方案1】:

    很遗憾,您的代码存在一些问题。但是,希望这将有助于消除它们。我运行以下代码没有问题。

    第一,仔细检查您安装了哪个版本的 Botbuilder。至少有一个我不熟悉的调用('this.addDialog'),似乎不是当前 SDK 的一部分,并且在测试时对我不起作用。

    二,如下配置您的机器人。从技术上讲,您应该能够传递问题中的各个步骤。但是,您的某些东西不起作用,我不知道是什么。但是,以下设置确实有效,并且符合已接受的做法。

    三,你的第一步调用'step.context.sendActivity(reply)'。在下一步中,您将尝试读取该调用的返回值。这是行不通的,因为 sendActivity 只是从机器人向用户发送一条语句(例如,“欢迎使用我的机器人!”)。您需要执行提示以捕获用户的输入响应(见下文)。

    您似乎正在尝试从卡中读取值。由于您没有提供那段代码,因此我通过用户的文本提示提供了“Order_Number”和“Serial_Number”来伪造值。

    您还应该利用机器人状态选项。与其使用变量“cardValue”,不如考虑使用 UserState 或 DialogState 来存储对对话很重要的用户值。

    最后,在这个简单的示例中,如果您多次通过,订单号和序列号将相互覆盖。

    const START_DIALOG = 'starting_dialog';
    const DIALOG_STATE_PROPERTY = 'dialogState';
    const USER_PROFILE_PROPERTY = 'user';
    
    const ACTION_PROMPT = 'action_prompt';
    const ORDER_PROMPT= 'order_prompt';
    const SERIAL_PROMPT= 'serial_prompt';
    ...
    
    class ExampleBot {
        constructor(conversationState, userState) {
            this.conversationState = conversationState;
            this.userState = userState;
    
            this.dialogState = this.conversationState.createProperty(DIALOG_STATE_PROPERTY);
    
            this.userData = this.userState.createProperty(USER_PROFILE_PROPERTY);
    
            this.dialogs = new DialogSet(this.dialogState);
    
            this.dialogs
                .add(new TextPrompt(ACTION_PROMPT))
                .add(new TextPrompt(ORDER_PROMPT))
                .add(new TextPrompt(SERIAL_PROMPT));
    
            this.dialogs.add(new WaterfallDialog(START_DIALOG, [
                this.promptForAction.bind(this),
                this.promptForNumber.bind(this),
                this.consoleLogResult.bind(this)
            ]));
        }
    
        async promptForAction(step) {
            return await step.prompt(ACTION_PROMPT, `Action?`,
                {
                    retryPrompt: 'Try again. Action??'
                }
            );
        }
    
        async promptForNumber(step) {
            const user = await this.userData.get(step.context, {});
            user.action = step.result;
            if (user) {
                if (user.action == 'Order_Number') {
                    return await step.prompt(ORDER_PROMPT, 'enter order number');
                } else if (user.action == 'Serial_Number') {
                    return await step.prompt(SERIAL_PROMPT, 'enter serial number');
                } else {
                }
            }
            return await step.next();
        }
    
        async consoleLogResult(step) {
            const user = await this.userData.get(step.context, {});
            user.orderNumber = step.result;
            console.log('****** cardValue');
            console.log(user);
            console.log('****** step After');
            console.log(step);
            return await step.endDialog();
        }
    
        async onTurn(turnContext) {
    
        ... //Other code
    
        // Save changes to the user state.
        await this.userState.saveChanges(turnContext);
    
        // End this turn by saving changes to the conversation state.
        await this.conversationState.saveChanges(turnContext);
    
        }
    }
    
    

    希望有帮助!

    【讨论】:

      【解决方案2】:

      在我替换对话框的地方保存对话状态后,它以某种方式对我有用。

      await this.conversationState.saveChanges(context);
      

      【讨论】:

      • 您不必在每次替换对话框操作后保存对话状态。您这样做的事实表明您的代码结构不正确。您是否尝试过像我提供的示例那样构建代码?
      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多