javascript - 如何在另一个 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();
}
}
阿斯达斯
解决方案
我做出了错误的决定,并且能够及时看到。
我只是这样设置我的上下文。
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');
}
我觉得很傻,解决方案如此简单
推荐阅读
- ruby-on-rails - 我的 params 对象中有太多 ActionController::Parameters 类型的对象
- android - 获取空适配器错误:E/RecyclerView:未连接适配器;跳过布局
- javascript - Angular 9:core.js:6228 错误类型错误:无法读取未定义的属性“名称”
- java - 使用 java 和用户输入制作命令
- typescript - 如何在没有类样式或装饰器语法的情况下获得 mapGetters、mapActions Vuex 和 typescript 的智能感知
- python-3.x - 使用 MIN、MAX 和 SUM 遍历 7 个元素列表
- angular - IIS 找不到角度请求标头
- java - 使用 Spring Integration 的 Java 邮件监听器
- swift - Spritekit 物理未能检测到第二个相邻项目
- c++ - 我是在访问已经释放的内存,还是在这种情况下 DrMemory 报告不正确?