Lyndra
Lyndra

Reputation: 107

Prompt not waiting for user input

I am currently trying to write a Chatbot with Microsofts Bot Framework. I want to use a choice-prompt but it does not wait for me/the user to choose an option. Not even the example-code from here worked for me. See this working example:

var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 

});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata 
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var bot = new builder.UniversalBot(connector);
bot.set('storage', new builder.MemoryBotStorage()); 

var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || 'westus.api.cognitive.microsoft.com';

var LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v2.0/apps/' + `enter code here`luisAppId + '?subscription-key=' + luisAPIKey;

/**
 * RECOGNIZERS
 */

var recognizer = new builder.LuisRecognizer(LuisModelUrl);
bot.recognizer(recognizer);


/**
 * DIALOG SECTION
 */
 var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);



   intents.onDefault([
        function(session){
            session.send('I am sorry but I don`t know that.');
        }
    ]);


intents.matches('City' ,[
     function(session, args) {

        session.beginDialog('getLocationDialog');
        session.endDialog();
    } 
]);

 var locationLabels = {
     City1: 'City 1',
     City2: 'City 2',
     City3: 'City 3'
 };

 bot.dialog('getLocationDialog', [
    function (session) {
       builder.Prompts.choice(session, "Choose a city", locationLabels
           , { listStyle: builder.ListStyle.button },{
                maxRetries: 3,
                retryPrompt: 'This is not a valid option. Choose one of the options listed above.' }); 
       },
    function (session, results) {
        console.log(results.response.entity);
        if (results.response){
            var location = locationLabels[results.response.entity];
            session.send('You are interested in %s', location);
            session.endDialog();
        }
        else {
           session.send('Okay.');
           session.endDialog(); 
        }
    }
]);  

The dialog with the prompt is this part (the intent is matched using LUIS):

intents.matches('City' ,[
     function(session, args) {
   session.beginDialog('getLocationDialog');
   session.endDialog();
   } 
]);

 var locationLabels = {
     City1: 'City 1',
     City2: 'City 2',
     City3: 'City 3'
 };

 bot.dialog('getLocationDialog', [
    function (session) {
       builder.Prompts.choice(session, "Choose a city", locationLabels
           , { listStyle: builder.ListStyle.button },{
                maxRetries: 3,
                retryPrompt: 'This is not a valid option. Choose one of the options listed above.' }); 
       },
    function (session, results) {
        console.log(results.response.entity);
        if (results.response){
            var location = locationLabels[results.response.entity];
            session.send('You are interested in %s', location);
            session.endDialog();
        }
        else {
           session.send('Okay.');
           session.endDialog(); 
        }
    }
]);  

See this code in action in action

The bot does not wait for me to choose an option. Can somebody tell where the problem is?

Upvotes: 5

Views: 1037

Answers (1)

Steven Kanberg
Steven Kanberg

Reputation: 6418

Please try the code listed below. I made a few changes that had it working on my end. First, remove the session.EndDialog() call in the 'City' intent match. That call was causing the dialog to start over before the original dialog could complete.

Second, you had the locationLabels variable set as a key/value pair. This needs to be a simple array. If you need to use a key/value pair, consider using iChoice.

Third, your location variable only needs to be assigned results.response.entity. As locationLabels is a simple array, you no longer need to try and match on it.

Hope this helps.

Steve.

var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);

intents.onDefault([
    function(session){
        session.send('I am sorry but I don`t know that.');
    }
]);

intents.matches('City' ,[
    function(session, args) {
        session.beginDialog('getLocationDialog');
        // session.endDialog();
    } 
]);

var locationLabels = ['City 1', 'City 2', 'City 3'];

bot.dialog('getLocationDialog', [
    function (session) {
        builder.Prompts.choice(session, "Choose a city", locationLabels, { listStyle: builder.ListStyle.button }); 
        },
    function (session, results) {
        console.log(results.response.entity);
        var location = results.response.entity;
        if (results.response){
            session.send('You are interested in %s', location);
            session.endDialog();
        }
        else {
           session.send('Okay.');
           session.endDialog(); 
        }
    }
]);

Upvotes: 3

Related Questions