Cant trigger message after reloadaction - node.js

After I enter "reset", I does not received any message "Welcome Back" and restart the conversation.
bot.dialog('reset', [
function(session) {
session.send("Welcome Back");
session.beginDialog('/');
}]
)
.reloadAction('reset', 'Ok, starting over.', {
matches: /^reset/i,
});

The reloadAction only works for dialogs that have already been added to the stack. In order to use it correctly, the reloadAction needs to be attached to a dialog that has already been called/used previously.
For example, if a user is responding to a series of user profile questions across multiple dialogs (personal info, then address/previous address, followed by education) and the user types 'reset' while in the education dialog, then the reloadAction would trigger having been attached to the first user profile dialog (that collects personal info). Thus, the user would be brought back to the beginning of the user profile dialogs to start over.
If you are intent on calling a new dialog, then you will likely want to use triggerAction. This does clear the dialog stack entirely but also allows you to redirect back to dialog x. In this way, you achieve the same result as in your ask with just a little bit of extra code:
bot.dialog('reset', [
function(session, args, next) {
session.send("Ok, starting over.");
next();
},
function(session) {
session.send("Welcome Back");
session.beginDialog('/');
}]
).triggerAction({
matches: /^reset/i
});
Hope of help!

Related

What's the difference between endDialog() and endConversion() in Bot-Framework (node.js)?

I am confused with endDialog() and endConversion() when using bot-framework.
What's the difference between them?
If I don't invoke them, just using send()? What constraint will be?
When inside a dialog, you can invoke some other dialog. For e.g.
bot.dialog("/", [
function(session, data, next()){
session.send("Hi");
if(session.message.text === "hello"){
// starts a new dialog
session.beginDialog("helloDialog");
next();
} else {
next();
}
}, function(sesion, data){
session.send("end of root dialog");
}
]);
bot.dialog("helloDialog",[
function(session){
session.send("inside the hello dialog");
session.endDialog(); // explicitly ends the dialog
}
])
When user input is hello, output is
Hi
inside the hello dialog
end of root dialog
When user input is anything else, output is
Hi
end of root dialog
session.endDialog ends the current dialog, and resumes the parent dialog.
session.endConversation ends the conversation itself.
In Technical terms, when a dialog is called, the dialog moves into a stack called dialogStack. When another dialog is called from current dialog, that new dialog is placed at the top of dialogStack. When this new dialog completes its operation, this dialog is popped from the stack, and the last dialog resumes.
When session.endConversation is invoked, the dialog stack is emptied right away (this is a behavior am not fully sure though)

Microsoft Bot framework session.endDialog() use

I read the Bot document somewhere people posted if you replace dialog then previous dialog store in stack and stored somewhere.
Now I tried to follow the way to endDialog() then replaceDialog();
callRequest.GetWebAPICall(session, urlData, function (body) {
if(body.statusCode == 200) {
if(body.data == undefined) {
builder.Prompts.choice(session,Want to Select List?", "Yes|No",{listStyle: builder.ListStyle.button});
} else {
session.endDialog();
session.replaceDialog('/Show List');
}
} else {
session.send('Something went wrong. You can use the back or top command.');
session.replaceDialog('/menu');
}
});
For need to know if I replace below lines
session.endDialog();
session.replaceDialog('/Show List');
by
session.endDialog('/Show List');
No. endDialog() doesn't have the functionality to start a new dialog. You can refer to the function definition interface endDialog(message?: TextOrMessageType, ...args: any\[\]): Session;.
In your case, '/Show List' would be sent to the user as a message.
And there is also a misunderstanding about replaceDialog().
Ends the current dialog and starts a new one its place. The parent dialog will not be resumed until the new dialog completes.
If you need to store the previous dialog, you can use beginDialog()

botframework choice invalid response typed

I am using nodejs SDK for creating my bot with MSFT botframework.
The code snippet is as follows:
function(session, args, next){
builder.Prompts.choice(session, "Please select one of the options:", ['AAA', 'BBB','CCC'], {retryPrompt: "Invalid choice, Please pick below listed choices",
listStyle: builder.ListStyle.button,
maxRetries: 1
});
},
function(session,results){
if (results.response) {
//Do something
}
}
I have 2 questions:
I would like to navigate to a different dialog Flow in case the user types anything other then the options("AAA","BBB","CCC"). Is that possible?
I would like to change the retryPrompt everytime lets say pick the utterances from a list. Is that possible?
I would like to navigate to a different dialog Flow in case the user types anything other then the options("AAA","BBB","CCC"). Is that possible?
Yes, it's possible. You can define several dialogs with required waterfall steps related to the choices. Like:
bot.dialog('AAA',[...])
And leverage replaceDialog to redirect your user to new dialog flows:
(session,result)=>{
//result.response.entity should be one of string `AAA`,`BBB`,`CCC`
session.replaceDialog(result.response.entity);
}
I would like to change the retryPrompt everytime lets say pick the utterances from a list. Is that possible?
Yes, it's possible. According the choice definition, we can set options extends IPromptChoiceOptions which extends [IPromptOptions][2], we can find that retryPrompt?: TextOrMessageType;, dig into the source code for the definition of TextOrMessageType:
/**
* Flexible range of possible prompts that can be sent to a user.
* * _{string}_ - A simple message to send the user.
* * _{string[]}_ - Array of possible messages to send the user. One will be chosen at random.
...
...
*/
export type TextOrMessageType = string|string[]|IMessage|IIsMessage;
So we can set a string list for retryPrompt, bot builder will pick one randomly. Please try following:
builder.Prompts.choice(session, "Please select one of the options:", ['AAA', 'BBB', 'CCC'], {
listStyle: builder.ListStyle.button,
maxRetries: 1,
retryPrompt:['utterance 1','utterance 2','utterance 3','utterance 4']
});
As you can call a function in retryPrompt you can do both of them:
builder.Prompts.choice(
session,
'This is just a question?',
'Yes|No',
{ retryPrompt: particularRetry() }
);
In the above, you can do what you want in particularRetry function.
For example for the second question, you can call the new propmt with new choices.
You can see more details in this post.

Multiple buttons in HeroCard

I'd like to have multiple buttons on HeroCard
and be able to press all buttons one after another
but when I press click button program jumps to next function in waterfall
and expects next action instead of button action again
what should I do in this case?
bot.dialog("/showCards", [
(session) => {
const msg = new Message(session)
.textFormat(TextFormat.xml)
.attachmentLayout(AttachmentLayout.carousel)
.attachments([{
title: "title",
url: "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png"
}].map(obj =>
new HeroCard(session)
.title(obj.title)
.images([
CardImage.create(session, obj.url)
.tap(CardAction.showImage(session, obj.url)),
])
.buttons([
CardAction.openUrl(session, obj.url),
CardAction.imBack(session, `click`, "Click"),
CardAction.imBack(session, `clack`, "Clack")
])
));
Prompts.choice(session, msg, ["click", "clack"]);
},
(session, results) => {
// todo use results.response.entity
}
]);
You could also use CardAction.dialogAction and link every button to a beginDialogAction.
let card = new builder.HeroCard(session)
.title(title)
.subtitle(subtitle)
.buttons([builder.CardAction.dialogAction(session, 'dialogAAction', 'dataYouNeedInDialogA', 'ButtonTitleA'), builder.CardAction.dialogAction(session, 'dialogBAction', 'dataYouNeedInDialogA', 'ButtonTitleB')]);
let msg = new builder.Message(session)
.attachments([card])
session.endDialog(msg);
// use one of these two to either end the dialog and start a new one or to stay in the current dialog and wait for user input
session.send(msg);
// don't forget to add the dialogs to your bot / library later in your code (outside your current dialog)
bot.dialog('dialogA', dialogA); // initialized somewhere in your code
bot.dialog('dialogB', dialogB);
bot.beginDialogAction('dialogAAction', 'dialogA');
bot.beginDialogAction('dialogBAction', 'dialogB', {
onSelectAction: (session, args, next) => {
// you might want to clear the dialogStack if the button is pressed. Otherwise, if the button is pressed multiple times, instances of dialogB are pilled up on the dialog stack.
session.clearDialogStack();
next();
}
});
In my opinion, this is the best way to achieve the behaviour you described so far. All buttons work whenever the user presses them, even if they scroll back in the conversation and press the same button again. The only trade-off is that you have to pass data to the new dialog and can not use dialogData throughout the whole flow. Nevertheless, I think it's worth it because ensures consistent UX throughout the usage of the bot.
Hope this helps. You can build click and clack dialogs, link them to actions and pass the data that you need. The user would be able to press click, clack, click and the bot would still work. :)
Use a switch-case in the ResumeAfter function, in the default case send the user to the previous function.

How to add additional dialog in bot framework

How can I have 2 conversations going concurrently? I'm currently using TextBot and LuisDialog to build a bot. I start off by having a conversation with the user to obtain data. Then while doing some processing in a different method, I discover that I need additional information from the user. How can I create a new conversation with the user just to get that additional information? I have some code below that attempts to show what I want to do. Thanks for your suggestions.
File 1: foo.js
var dialog = new builder.LuisDialog(model);
var sonnyBot = new builder.TextBot();
sonnyBot.add('/', dialog);
dialog.on('intent_1', [
function(session, args, next) {
name = builder.Prompts.text(session,"What is your name?");
},
function(session, result) {
session.dialogData.name= results.response;
getFamilyTree(session.dialogData.name);
}
]);
File 2: getFamilyTree.js
function getFamilyTree(name) {
find family tree for name
if (need place of birth) {
begin new dialog
prompt user place of birth
get place of birth from user
end dialog
}
finish getting the family tree
}
i guess you could pass session object and then use that object to start a new dialog .
Edit 1
can't you use some thing like
session.beginDialog('/getFamilyTree',{name:result.response});
and then you can access name like
args.name
inside 'getFamilyTree' dialog
I posted the same question on GitHub and received the answer from Steven Ickman, who is involved in the development of the node.js SDK. The link to the answer is https://github.com/Microsoft/BotBuilder/issues/394#issuecomment-223127365

Resources