botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Node.js SDK] Root dialog keeps repeating

Open KwstasBol opened this issue 9 years ago • 17 comments

  • SDK Platform: Node.js

Issue Description

The bot is about to show compared to the current date the open pharmacies and i run it locally.Firstly i code session.begin('chooseDay'), representing the first dialog. In this one, i send to the user one HeroCard with two buttons. When i press one button, i begin another dialog but before the results of the new dialog, the ones of the first(root) come up again. What is going wrong here?

Code Example

a1 a2 a3

Expected Behavior

The below image without the middle dialog which is the root.

Actual Results

image

KwstasBol avatar Sep 25 '17 09:09 KwstasBol

Can you provide the complete code for the 'chooseDay' dialog instead of screenshots? It'll help in debugging your code to see the entire dialog. Similarly, the code for your root dialog as well.

stevengum avatar Sep 25 '17 14:09 stevengum

image

image

KwstasBol avatar Sep 25 '17 17:09 KwstasBol

can you please post actual code and not screenshots

JasonSowers avatar Sep 25 '17 17:09 JasonSowers

I didnt quite understand. Shall i upload the whole code of the dialogs?

KwstasBol avatar Sep 25 '17 18:09 KwstasBol

just copy and paste your code as text into a comment

JasonSowers avatar Sep 25 '17 18:09 JasonSowers

var bot = new builder.UniversalBot(connector, [
  function(session) {
    session.beginDialog('chooseDay');
  }
]);

bot.dialog('chooseDay', [
    //Step one  
    function(session) {
        var msg = new builder.Message(session);
        msg.attachmentLayout(builder.AttachmentLayout.carousel)
        msg.attachments([
            new builder.HeroCard(session)
                .title("Επιλογή Ημέρας")
                .buttons([builder.CardAction.postBack(session,"Σήμερα","Σήμερα"),builder.CardAction.postBack(session,"Αύριο","Αύριο")])
        ])
     
        session.send(msg);
        
        if (session.message.text=="Σήμερα") {
           session.beginDialog('pharmacyToday');
        }
        
        else if (session.message.text=="Αύριο") {
            session.beginDialog('pharmacyTomorrow');
        }
        
        //session.endDialog();
    }
]);

bot.dialog('pharmacyToday', [
    function (session) {
        Pharmacy.findOne({'dates':today},function(err,found){
            if(err){
                console.log(err);
            }
            else{
                var msg=new builder.Message(session);
                msg.attachmentLayout(builder.AttachmentLayout.carousel)
                msg.attachments([
                    new builder.HeroCard(session)
                        .title("Εφημερεύον Φαρμακείο")
                        .subtitle(found.name)
                ])
                session.endDialog(msg);
            }


        });
       
    }
]);

bot.dialog('pharmacyTomorrow',[
    
   
    function(session){
        
        Pharmacy.findOne({'dates':today+1},function(err,found){
            if(err){
                console.log(err);
            }
            else{
                var msg=new builder.Message(session);
                msg.attachmentLayout(builder.AttachmentLayout.carousel)
                msg.attachments([
                    new builder.HeroCard(session)
                        .title("Εφημερεύον Φαρμακείο")
                        .subtitle(found.name)
                ])
                session.endDialog(msg);
            }


        });
        
    }
]);

KwstasBol avatar Sep 25 '17 18:09 KwstasBol

First you need to either use session.replaceDialog() or create another waterfall step in your root dialog and call session.endDialog(). Otherwise after you finish all of your command you will end up here over and over again. Below I'm using session.replaceDialog().

var bot = new builder.UniversalBot(connector, [
  function (session) {
    session.replaceDialog('chooseDay');
  }
]);

stevengum avatar Sep 25 '17 18:09 stevengum

1st solution var bot = new builder.UniversalBot(connector, [ function (session) { session.beginDialog('chooseDay'); }, function(session,results){ session.endDialog(); } ]); 2nd solution var bot = new builder.UniversalBot(connector, [ function (session) { session.replaceDialog('chooseDay'); } ]); Thanks for the help Steven, but neither works and the results are the same..Am i doing something wrong?

KwstasBol avatar Sep 25 '17 18:09 KwstasBol

Sorry I planned on writing a multi-part answer. The next part is that you need to separate your logic in your 'chooseDay' dialog. You're sending a card to your user, but in the same step you're expecting a response to that card. The logic that handles the user's response needs to be in a separate waterfall step.

bot.dialog('chooseDay', [
    //Step one  
    function (session) {
      var msg = new builder.Message(session);
      msg.attachmentLayout(builder.AttachmentLayout.carousel)
      msg.attachments([
        new builder.HeroCard(session)
        .title("Επιλογή Ημέρας")
        .buttons([builder.CardAction.postBack(session,"Σήμερα","Σήμερα"),
          builder.CardAction.postBack(session,"Αύριο","Αύριο")])
      ])
      session.send(msg);
    },
    // New step that handles the user's response
    function (session, response) {
      if (session.message.text=="Σήμερα") {
        session.beginDialog('pharmacyToday');
      } else if (session.message.text=="Αύριο") {
        session.beginDialog('pharmacyTomorrow');
      }
    },
    // New step that closes this dialog after the dialog started above ends
    function (session, response) {
      session.endDialog();
    }
]);

stevengum avatar Sep 25 '17 19:09 stevengum

Sorry for putting you in so much trouble Steven. I modified these with having the root dialog changed as you told me(with two waterfall steps), and now i have the result below.So it does not enter the pharmacy dialog. I think that session.message.text doesnt contain the same value of the previous step.In the last image, you can see the waterfall flow. image image

KwstasBol avatar Sep 25 '17 20:09 KwstasBol

Hmm, in the second step from my last message, can you console.log() the response object to see what it contains?

stevengum avatar Sep 25 '17 20:09 stevengum

Nothing appears. As you said i console.log(response) in the second step and i do not get anything..Weird things happen in this world.

KwstasBol avatar Sep 25 '17 20:09 KwstasBol

It appears that you're not constructing your postBacks properly, have you tried using the following code's format?

  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")
              ]
          )
      );

stevengum avatar Sep 25 '17 21:09 stevengum

The imback will be shown on chat. I just used the next() at the first step of the chooseDay dialog and i had the result,but the default dialog still repeats itself.

KwstasBol avatar Sep 25 '17 21:09 KwstasBol

The default dialog repeats itself after selecting your postBack?

I recommend sending the message as a prompt so that the bot waits for a user's input, otherwise your bot is still moving through the dialog and then ending itself as specified in the code.

stevengum avatar Sep 25 '17 22:09 stevengum

Steven it worked with Prompts, thank you very much. I will keep it this way. So basically with a rich card is this going to happen again, even if i try anything?

KwstasBol avatar Sep 26 '17 05:09 KwstasBol

Glad it worked out for you! 😄

The difference is that session.send() doesn't wait for a response, whereas by using a Prompt you're explicitly waiting for a user's response.

If you send a rich card in a Prompt you should be fine.

Closing issue.

stevengum avatar Sep 26 '17 17:09 stevengum