Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bot Framework suggested Actions

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?

like image 755
Sebastian Felix Schwarz Avatar asked Nov 07 '22 07:11

Sebastian Felix Schwarz


1 Answers

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...");
    }    
});
like image 128
Abandoned Avatar answered Nov 15 '22 07:11

Abandoned