首页 > 解决方案 > 如何在另一个 Dialogio 中获取 UserProfile

问题描述

我正在使用微软的机器人构建器实现一个聊天机器人。

当我实现了对话框的替代路由时,我丢失了用户的配置文件引用。

我的主要对话

import { StatePropertyAccessor, UserState, TurnContext } from 'botbuilder';
import { ChoiceFactory, ChoicePrompt, ComponentDialog, ConfirmPrompt, DialogSet, DialogTurnStatus, NumberPrompt, PromptValidatorContext, TextPrompt, WaterfallDialog, WaterfallStepContext } from 'botbuilder-dialogs';
import { UserProfile } from './UserprofileModel';
import {OtherDialog, OTHER_DIALOG} from './OtherDialogs'

const USER_PROFILE = 'USER_PROFILE';
const NAME_PROMPT = 'NAME_PROMPT';
const INPUT_PROMPT = 'INPUT_PROMPT';
const WATERFALL_DIALOG = 'WATERFALL_DIALOG';
const CONFIRM_PROMP = 'CONFIRM_PROMP';
const NUMBER_PROMPT = 'NUMBER_PROMPT';
const CHOISE_PROMPT = 'CHOISE_PROMPT';
export class UserProfileData extends ComponentDialog {
  private userProfile: StatePropertyAccessor<UserProfile>;

  constructor(userState: UserState) {
    super('userProfileData');

    this.userProfile = userState.createProperty(USER_PROFILE);

    this.addDialog(new TextPrompt(NAME_PROMPT))
    this.addDialog(new TextPrompt(INPUT_PROMPT))
    this.addDialog(new ConfirmPrompt(CONFIRM_PROMP))
    this.addDialog(new ChoicePrompt(CHOISE_PROMPT))

    this.addDialog(new OtherDialog(userState));

    this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
      this.nameStep.bind(this),
      this.nameConfirmStep.bind(this),
      this.typeInteraction.bind(this),
      this.finshStep.bind(this)
    ]));

    this.initialDialogId = WATERFALL_DIALOG;

  }

  public async run(turnContext: TurnContext, accessor: StatePropertyAccessor) {
    const dialogSet = new DialogSet(accessor);
    dialogSet.add(this);

    const dialogContext = await dialogSet.createContext(turnContext);
    const result = await dialogContext.continueDialog();
    if (result.status === DialogTurnStatus.empty) {
      await dialogContext.beginDialog(this.id);
    }
  }

  private async nameStep(stepContext: WaterfallStepContext<UserProfile>) {
    return await stepContext.prompt(NAME_PROMPT, 'What is your name?');
  }

  private async nameConfirmStep(stepContext: WaterfallStepContext<UserProfile>) {
    stepContext.options.name = stepContext.result;

    await stepContext.context.sendActivity(`Olá ${stepContext.result}!`);
    
    return await stepContext.prompt(INPUT_PROMPT, 'choice one number, 1, 2 or 3');
  }

  private async typeInteraction(stepContext: WaterfallStepContext<UserProfile>) {
    stepContext.options.type = stepContext.result;

    if (stepContext.options.type !== '1' && '2' && '3') {
      await stepContext.context.sendActivity(`ok`);
      await stepContext.context.sendActivity(`say something to the user.`);

      const userProfile = await this.userProfile.get(stepContext.context, new UserProfile());
      const stepContextOptions = stepContext.options;
      userProfile.name = stepContextOptions.name;
      console.log('------------- NAME ------------')
      console.log(userProfile.name)

      return await stepContext.beginDialog(OTHER_DIALOG)
    } else {
      await stepContext.context.sendActivity(`You choice ${stepContext.options.type}, So, go start.`);
    } 

    return stepContext.next();
  }

  private async finshStep(stepContext: WaterfallStepContext<UserProfile>) {

    await stepContext.context.sendActivity(`end of chat`);
    return await stepContext.endDialog();
  }

}

阿斯达斯

我的辅助对话框,它应该显示用户的个人资料。我正在尝试在以下函数 private async oneStep(stepContext: WaterfallStepContext)中为名称提供 console.log

import { StatePropertyAccessor, UserState, TurnContext } from 'botbuilder';
import { ChoiceFactory, ChoicePrompt, ComponentDialog, ConfirmPrompt, DialogSet, DialogTurnStatus, NumberPrompt, PromptValidatorContext, TextPrompt, WaterfallDialog, WaterfallStepContext } from 'botbuilder-dialogs';
import { UserProfile } from './UserprofileModel';
import { UserProfileData } from './userProfileDataCustom'

export const OTHER_DIALOG = 'OTHER_DIALOG';

const USER_PROFILE = 'USER_PROFILE';
const INPUT_PROMPT = 'INPUT_PROMPT';
const WATERFALL_DIALOG = 'WATERFALL_DIALOG';
export class OtherDialog extends ComponentDialog {
  private userProfile: StatePropertyAccessor<UserProfile>;

  constructor(userState: UserState) {
    super(OTHER_DIALOG);

    this.userProfile = userState.createProperty(USER_PROFILE);

    this.addDialog(new TextPrompt(INPUT_PROMPT))

    this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
      this.oneStep.bind(this),
      this.twoStep.bind(this),
      this.treeStep.bind(this),
      this.fourStep.bind(this),
      this.finshStepOther.bind(this)
    ]));

    this.initialDialogId = WATERFALL_DIALOG;

  }

  private async oneStep(stepContext: WaterfallStepContext<UserProfile>) {

    console.log('------ NAME 2 --------');
    const userProfile = await this.userProfile.get(stepContext.context, new UserProfile());
    const stepContextOptions = stepContext.options;
    userProfile.name = stepContextOptions.name;
    console.log(userProfile.name)

    const name = 'SEM NOME'
    await stepContext.context.sendActivity(`Olá ${name}`);
    return await stepContext.prompt(INPUT_PROMPT, 'Input 1');
  }

  private async twoStep(stepContext: WaterfallStepContext<UserProfile>) {
    return await stepContext.prompt(INPUT_PROMPT, 'Input 2');
  }

  private async treeStep(stepContext: WaterfallStepContext<UserProfile>) {
    return await stepContext.prompt(INPUT_PROMPT, 'Input 3');
  }

  private async fourStep(stepContext: WaterfallStepContext<UserProfile>) {
    return await stepContext.prompt(INPUT_PROMPT, 'Input 4');
  }

  private async finshStepOther(stepContext: WaterfallStepContext<UserProfile>) {
    await stepContext.context.sendActivity(`ending other dialog`);
    return await stepContext.endDialog();
  }

}

阿斯达斯

标签: javascriptnode.jstypescriptbotframeworkchatbot

解决方案


我做出了错误的决定,并且能够及时看到。

我只是这样设置我的上下文。

private async oneStep(stepContext: WaterfallStepContext<UserProfile>) {

    console.log('------ NAME 2 --------');
    const userProfile = await this.userProfile.get(stepContext.context, new UserProfile());
    // const stepContextOptions = stepContext.options;
    // userProfile.name = stepContextOptions.name;
    console.log(userProfile.name)

    const name = userProfile.name
    await stepContext.context.sendActivity(`Olá ${name}`);
    return await stepContext.prompt(INPUT_PROMPT, 'Input 1');
  }

我觉得很傻,解决方案如此简单


推荐阅读