很遗憾,您的代码存在一些问题。但是,希望这将有助于消除它们。我运行以下代码没有问题。
第一,仔细检查您安装了哪个版本的 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);
}
}
希望有帮助!