我想在我的机器人中使用建议的动作 - 所以用户快速回复回答问题,但也是输入字段。
所以Bot问的是:
“你喜欢披萨吗?” - > 是啊! - > 否
用户可能不会使用快速回复,而是写道:“是的,汉堡”。
现在我需要“汉堡”作为实体 - Example on GitHub对我来说没有意义,因为他们将快速回复拉到了选择提示 - 当用户仅从建议的操作中选择时,这是好的 - 但是当他们在输入字段中键入他们自己的答案时则没有。
bot.dialog('/', [
function (session) {
var msg = new builder.Message(session)
.text("Hi! What is your favorite color?")
.suggestedActions(
builder.SuggestedActions.create(
session,[
builder.CardAction.imBack(session, "green", "green"),
builder.CardAction.imBack(session, "blue", "blue"),
builder.CardAction.imBack(session, "red", "red")
]
)
);
builder.Prompts.choice(session, msg, ["green", "blue", "red"]);
},
function(session, results) {
session.send('I like ' + results.response.entity + ' too!');
}]);
有解决方案吗?
答案 0 :(得分:1)
您也可以使用builder.Prompts.text
来获取用户的输入,同时还可以构建SuggestedActions
卡片以创建响应基线。使用builder.Prompts.choice
会将您限制在传递给声明的选项中。所以你可以让你的机器人问“你喜欢披萨吗?”并弹出“是啊!”和不。”按钮,但也允许用户输入另一个响应。
使用您的问题示例,您可以进行如下对话框。这也显示了一些正则表达式逻辑,用于接受可能的答案添加,例如有人输入“是的,还有汉堡!”
示例:
bot.dialog('/', [
function (session) {
var msg = new builder.Message(session)
.text("Do you like Pizza?")
.suggestedActions(
builder.SuggestedActions.create(
session,[
builder.CardAction.imBack(session, "Yeah!", "Yeah!"),
builder.CardAction.imBack(session, "No.", "No.")
]
)
);
builder.Prompts.text(session, msg);
},
function(session, results) {
let regex = /yeah|yes|sure|of course|i do\!|affirmative|positive/gi;
if (regex.test(results.response)) {
session.beginDialog("LikesPizza");
} else {
session.beginDialog("DoesNotLikePizza");
}
}]);
bot.dialog("LikesPizza", function(session) {
let yesAndMoreRegex = /and|also/gi
if (yesAndMoreRegex.test(session.message.text)) {
session.endDialog("You like other foods too? Awesome! But pizza is the best!");
} else {
session.endDialog("Who doesn't like pizza?!");
}
});
bot.dialog("DoesNotLikePizza", function(session) {
let noButRegex = /but|although|better/gi;
if (noButRegex.test(session.message.text)) {
session.endDialog("True, there's foods other than pizza. There's something for everyone!");
} else {
session.endDialog("Well, pizza's not for everyone, I guess...");
}
});