I want to use suggested Actions in my Bot - so the User has a quick Reply to answer the Question, but also an input field.
So the Bot asks something like:
"Do you like Pizza?" -> Yeah! -> No.
And the user may not use the Quick Replies and instead writes: "Yes, and Burger too".
Now I need "Burger" as an Entity too - the Example on GitHub doesn't make sense for me, because they pull the Quick Replies to a Choice Prompt - which is fine, when the user only chooses from the suggested Actions - but not when they type their own answer into the input field.
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!');
}]);
Is there a solution?
You can use builder.Prompts.text
to get the input from the user as well, while also building the SuggestedActions
card to create a baseline of responses. Using builder.Prompts.choice
will constrain you to the choices passed into the declaration. So you could have your bot ask "Do you like pizza?" and pop up "Yeah!" and "No." buttons, but also allow the user to type in another response.
Using your question example, you can make a dialog like the one below. This also shows some regex logic for taking in possible additions to answers, such as someone typing "Yes, and also burgers!"
Example:
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...");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With