node.js - bot-framework v4 (node.js) 中的动态提示选择
问题描述
我有一个短信机器人提示,用户可以在其中做出多项选择。我正在寻找允许我这样做的 ChoicePrompt 模式:
- 显示多项选择
- 然后用户选择并回答后,重新提示他们再次回答
- 删除他们之前的选择并添加“退出”选项以继续
- 如果他们选择了所有内容,则自动结束该步骤。
我想避免为每个答案层创建一个带有切换案例的新提示,因为这种模式需要在很多地方实现......
例子:
bot:用户,你做什么放松?
- 锻炼
- 读一本书
- 没有
用户:运动
bot:运动,酷。还有什么?
- 读一本书
- 没有其他的
用户:看书
bot:好的,你已经完成了一切,所以我们继续前进!
解决方案
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”),如果回复在颜色列表中,则在没有选择的选项的情况下根据需要重新创建数组。
推荐阅读
- python - 有没有一种内存有效的方法来使用元组来迭代非常大的 os.scandir() 对象?
- python - 在半对数子图上设置纵横比
- android - Android分页库3如何使用RemoteMediator先从房间加载而不是等待远程响应
- opendaylight - 在 Opendaylight 中安装自定义功能或模块?
- python - 在批处理脚本中从 cmd 调用时找不到 git 命令
- css - 带边框和图像的六边形网格
- python - 将键值对添加到现有的 pandas 系列
- blazor - Blazor Timer 调用异步 API 任务来更新 UI
- c - char 数组和 putchar 的 strlen
- python - TypeError:'float' 对象不可调用。这是什么意思?