首页 > 解决方案 > bot-framework v4 (node.js) 中的动态提示选择

问题描述

我有一个短信机器人提示,用户可以在其中做出多项选择。我正在寻找允许我这样做的 ChoicePrompt 模式:

我想避免为每个答案层创建一个带有切换案例的新提示,因为这种模式需要在很多地方实现......

例子:

bot:用户,你做什么放松?

  1. 锻炼
  2. 读一本书
  3. 没有

用户:运动

bot:运动,酷。还有什么?

  1. 读一本书
  2. 没有其他的

用户:看书

bot:好的,你已经完成了一切,所以我们继续前进!

标签: node.jsbotframework

解决方案


botframework 没有我可以看到的 ListPrompt,至少对于 v4。但是,他们确实有您可以使用的建议操作!!!Botbuilder-Samples repo 有一个 Suggested Action 示例,其中显示了三种颜色的列表:

async onTurn(turnContext) {
    // See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
    if (turnContext.activity.type === ActivityTypes.Message) {
        const text = turnContext.activity.text;

        // Create an array with the valid color options.
        const validColors = ['Red', 'Blue', 'Yellow'];

        // If the `text` is in the Array, a valid color was selected and send agreement.
        if (validColors.includes(text)) {
            await turnContext.sendActivity(`I agree, ${ text } is the best color.`);
        } else {
            await turnContext.sendActivity('Please select a color.');
        }

        // After the bot has responded send the suggested actions.
        await this.sendSuggestedActions(turnContext);
    } else if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
        await this.sendWelcomeMessage(turnContext);
    } else {
        await turnContext.sendActivity(`[${ turnContext.activity.type } event detected.]`);
    }
}

一个选项是以编程方式创建数组(在上面的示例中,它是“const validColors”),如果回复在颜色列表中,则在没有选择的选项的情况下根据需要重新创建数组。


推荐阅读