在适用于NodeJS的Microsoft Bot Framework v4中处理HeroCards响应

问题描述:

我正在使用 Microsoft Bot Framework v4 的NodeJs API.而且我的对话框没有在 ActivityHandler 中进行硬编码,我只是从那里调用.我正在使用瀑布对话框.因此,当我尝试在Messenger上显示 Carousel卡(在Microsoft bot框架上为 HeroCard )时,它成功显示,但是当我单击卡上的任何按钮时,都没有下一个对话框的响应.

I am using NodeJs API of Microsoft Bot Framework v4. And my dialogs are not hardcoded in the ActivityHandler, I just call from there. I am using Waterfall dialogs. So when I try to show Carousel card on messenger (which is HeroCard on Microsoft bot framework), it shows up successfully but when I click any button on cards, there is no response for next dialog.

我试图处理 onMessage 钩子,但是,它只是试图验证响应并抛出错误.

I tried to handle on onMessage hook but, it just tries to validate the response and throw errors.

....

 ListItems extends ComponentDialog {
    constructor(userProfileState, conversionStateAccessor) {
        super(LIST_ITEMS);
        this.userState = userProfileState;
        this.conversionState = conversionStateAccessor;
        this.addDialog(new WaterfallDialog(LIST_ITEMS_DIALOG, [
            this.sendItems.bind(this),
            this.handleItems.bind(this)
        ]
        ));
        this.initialDialogId = LIST_ITEMS_DIALOG;
    }


    async sendItems(step) {
        .......
        await step.context.sendActivity({ attachments: cards }); // this line is working
    }

    async handleItems(step) {
     console.log(step.result) // the response is not in 'step.result.value' if i click a button on cards

}

感谢您的帮助

---- 我添加了更多详细信息 -----

---- I added more detail -----

我正在使用此模板来创建卡片

I am using this template to create cards

const card = CardFactory.heroCard('title', 'subtitle', ['imagelink'], [
    { type: ActionTypes.PostBack,
        title: 'product 1',
        value: 'product_1'
    },
    { type: ActionTypes.PostBack,
        title: 'product 1',
        value: 'product_1' 
    },
]);
  await context.sendActivity({ attachments: [card] }); 

可以成功创建汽车,但是问题是,在那之后,如果用户需要,我会发送提示让用户打开主菜单.

Cars can be created successfully but the problem is, after that, I send a prompt to let the user turn the main menu if the user wants.

所以我这样寄给他们

 await context.sendActivity({ attachments: [card] }); 
 await step.prompt(LIST_ITEMS_MENU_PROMPT, menuPromptPayload);

如果用户单击卡片上的按钮,则会引发错误,因为我认为框架等待提示的答案.无法捕获卡按钮的有效载荷/

And if the user clicks a button on cards, an error is thrown because I think the framework waits for prompt's answers. Couldn't catch the card's button's payload/

您需要指示bot等待 sendItems 中的 sendActivity 后返回响应.同样,如果您不告诉机器人继续进行下去,则在 handleItems 中会遇到错误,因为除了控制台日志记录之外,什么都没有发生.

You need to instruct your bot to wait for and return a response following the sendActivity in sendItems. Similarly, you will encounter an error in the handleItems if you don't tell the bot to continue on since there is nothing happening other than console logging.

async sendItems(step) {
.......
    await step.context.sendActivity({ attachments: cards });

    // Tells the bot to wait. Normally, on `sendActivity`, it continues on to the next step.
    // "DialogTurnStatus" can be found in the `botbuilder-dialogs` package.
    return { status: DialogTurnStatus.waiting }; 
}

async handleItems(step) {
    console.log(step.result);
    return step.next();  // Tells the bot to continue to the next step.
}

希望有帮助!