[Node.js SDK] Root dialog keeps repeating
- 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
Expected Behavior
The below image without the middle dialog which is the root.
Actual Results

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.


can you please post actual code and not screenshots
I didnt quite understand. Shall i upload the whole code of the dialogs?
just copy and paste your code as text into a comment
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);
}
});
}
]);
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');
}
]);
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?
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();
}
]);
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.

Hmm, in the second step from my last message, can you console.log() the response object to see what it contains?
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.
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")
]
)
);
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.
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.
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?
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.