I have 4 Dialogs in my Project the First one is a
RootDialog
with some enum.
This is my root dialog so If user select the first option then I want to send him to the
CreateContact
Dialog which I added below and if user selcts
GetContact
or
SendMail
then I will send him to the appropriate Dialog but I am unable to call anothe form dialog on completion or selection of first Dialog.
[Serializable]
public enum RootOptions
{
CreateContact = 1, GetContact = 2, SendMail
};
[Serializable]
public class RootForm
{
public RootOptions? Option;
public static IForm<RootForm> BuildForm()
{
return new FormBuilder<RootForm>()
.Message("Welcome to TEST BOT!")
.OnCompletion(async (context, rootoption) =>
{
switch(rootoption.Option.Value.ToString()) "")
{
case "CreateContact":
//How to call Contact Dialog
break;
case "GetContact":
//Call Get Contact Dialog
break;
case "SendMail":
//Call Send Mail Dialog
break;
}
})
.Build();
}
}
Create Contact Dialog
[Serializable]
public class ContactForm
{
[Prompt("What is the name? {||}")]
public string Name;
[Prompt("What is the Mobile No? {||}")]
public string Mobile;
[Prompt("What is the Email? {||}")]
public string Email;
public static IForm<ContactForm> BuildForm()
{
return new FormBuilder<ContactForm>()
.Message("Let's create a contact")
.OnCompletion(async (context, profileForm) =>
{
await context.PostAsync("Your contact has been created.");
})
.Build();
}
}
Send Mail Dialog
[Serializable]
public class MailForm
{
[Prompt("What is the Email Id? {||}")]
public string Email;
[Prompt("What is the Subject? {||}")]
public string subject;
[Prompt("What is the Message? {||}")]
public string Message;
public static IForm<MailForm> BuildForm()
{
return new FormBuilder<MailForm>()
.Message("Let's Send a Mail")
.OnCompletion(async (context, mailForm) =>
{
await context.PostAsync("Mail Sent.");
})
.Build();
}
}
To call a dialog, you need to use context.Call as explained in this post. However, I'm not fully sure if this will work in the OnCompletion event of a Form.
If it doesn't work, my recommendation would be to encapsulate the RootForm into a IDialog<object> dialog and use that dialog as the starting point for the Conversation.SendAsync of the controller.
You can use below line of code to call multiple dialogs:
await Task.Run(() => context.Call(new OptionDialog(),
this.OptionDialogResumeAfter)).ConfigureAwait(false);
Related
I would like to intercept what the user writes if he doesn't like any option in the list. My code is the following, but the validate function works only if the user chooses an option.
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BotApplication.App_Code
{
public enum MainOptions { AccessoAreaRiservata = 1, AcquistoNuovaPolizza, RinnovoPolizza, Documenti, StatoPratica, AltroArgomento }
[Serializable]
public class MainReq
{
[Prompt("Indicare la tipologia della richiesta? {||}")]
public MainOptions? MainOption;
public static IForm<MainReq> BuildForm()
{
var form = (new FormBuilder<MainReq>()
.Field(nameof(MainOption),validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true };
{
string risposta = (response.ToString());
if (risposta == "AltroArgomento")
{
result.Feedback = "it works only if user choose an option";
result.IsValid = true;
}
return result;
}
})
.Build());
return form;
}
}
}
There are a few possible workarounds for you to consider. Normally if you want to account for situations where a user wants to ask a question or say something unrelated to the form, you'd have them cancel the form using the Quit command. If you want your bot to be smart enough to interpret when users change the subject in the middle of a form, that's a bit more advanced.
If you want to keep using a validate method, you can change your MainOption field to a string instead of a MainOptions? so that all user input gets sent to the validate method, but then you'd need to generate the list of choices yourself.
My recommendation is to use a custom prompter instead of a validate method. I've written a blog post that details how to make such a prompter. First you would provide a NotUnderstood template to indicate to your prompter when a message isn't a valid option in FormFlow. Then in the prompter you would call your QnAMaker dialog or do whatever you want with the message.
// Define your NotUnderstood template
[Serializable, Template(TemplateUsage.NotUnderstood, NOT_UNDERSTOOD)]
public class MainReq
{
public const string NOT_UNDERSTOOD = "Not-understood message";
[Prompt("Indicare la tipologia della richiesta? {||}")]
public MainOptions? MainOption;
public static IForm<MainReq> BuildForm()
{
var form = (new FormBuilder<MainReq>()
.Prompter(PromptAsync) // Build your form with a custom prompter
.Build());
return form;
}
private static async Task<FormPrompt> PromptAsync(IDialogContext context, FormPrompt prompt, MainReq state, IField<MainReq> field)
{
var preamble = context.MakeMessage();
var promptMessage = context.MakeMessage();
if (prompt.GenerateMessages(preamble, promptMessage))
{
await context.PostAsync(preamble);
}
// Here is where we've made a change to the default prompter.
if (promptMessage.Text == NOT_UNDERSTOOD)
{
// Access the message the user typed with context.Activity
await context.PostAsync($"Do what you want with the message: {context.Activity.AsMessageActivity()?.Text}");
}
else
{
await context.PostAsync(promptMessage);
}
return prompt;
}
}
I am working on a multi dialog form flow chat bot using bot framework.
Below is one of my dialog code where I want to get the confirmation from the user and if necessary, needs to alter customer selection/ parameters provided.
below is the code I'm working on
Dialog
namespace FormBot.Dialogs
{
[Serializable]
public class HardwareDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Welcome to the Hardware solution helpdesk!");
var HardwareFormDialog = FormDialog.FromForm(this.BuildHardwareForm, FormOptions.PromptInStart);
context.Call(HardwareFormDialog, this.ResumeAfterHardwareFormDialog);
}
private IForm<HardwareQuery> BuildHardwareForm()
{
OnCompletionAsyncDelegate<HardwareQuery> HardwareRequest = async (context, state) =>
{
string message = string.Empty;
await context.PostAsync($"Ok. {message}. Once we resolve it; we will get back to you....");
};
return new FormBuilder<HardwareQuery>()
.Field(nameof(HardwareQuery.Hardware))
.Message($"We are Creating Ticket your request ...")
.AddRemainingFields()
.OnCompletion(HardwareRequest)
.Build();
}
private async Task ResumeAfterHardwareFormDialog(IDialogContext context, IAwaitable<HardwareQuery> result)
{
try
{
}
catch (FormCanceledException ex)
{
string reply;
if (ex.InnerException == null)
{
reply = "You have canceled the operation. Quitting from the HardwareDialog";
}
else
{
reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
}
await context.PostAsync(reply);
}
finally
{
context.Done<object>(null);
}
}
public static IForm<HardwareDialog> BuildForm()
{
return new FormBuilder<HardwareDialog>()
.Message("Welcome to Service Ticket Bot!")
.Build();
}
}
}
Query builder
public enum HardwareOptions
{
Desktop, KeyBoard, Laptop, Monitor, Mouse, Printer, Scanner, Server, Tablet
};
[Serializable]
public class HardwareQuery
{
[Prompt("Choose your {&} ? {||}")]
public HardwareOptions? Hardware;
[Prompt("Please enter {&}")]
[Pattern(Utility.Phone)]
public string PhoneNumber { get; set; }
[Prompt("Please enter {&} ")]
[Pattern(Utility.Email)]
public string Email { get; set; }
[Prompt("Please provide your business need / {&} below")]
public string Justification { get; set; }
public static IForm<HardwareQuery> BuildForm()
{
OnCompletionAsyncDelegate<ServiceTicket> processOrder = async (context, state) =>
{
await context.PostAsync($"Once we resolve it; we will get back to you....");
};
return new FormBuilder<HardwareQuery>()
.Message("Welcome !")
.Build();
}
}
}
Expected result
Asking for confirmation
Updating the result set
To ask for a confirmation of the selected values in FormFlow, you could use the .Confirm() while building the form. In case you want to prompt for a customized message, you could use .Confirm("Do you confirm the chosen hardware - {Hardware} and your email - {Email} and phone number {PhoneNumber}"). For detailed approaches, have a look at the official documentation. Based on the choice of the confirmation, bot framework's form flow will automatically handle the posting in the questions to change and updating the results.
P.S. On a side note, I am not sure why you are trying to use FormDialog.FromForm and the other unnecessary code, you could simplify the HardwareDialog as follows:
namespace FormBot.Dialogs
{
[Serializable]
public class HardwareDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Welcome to the Hardware solution helpdesk!");
var HardwareFormDialog = new FormDialog<HardwareQuery>(new HardwareQuery(), HardwareQuery.BuildForm, FormOptions.PromptInStart);
context.Call(HardwareFormDialog, this.ResumeAfterHardwareFormDialog);
}
private async Task ResumeAfterHardwareFormDialog(IDialogContext context, IAwaitable<HardwareQuery> result)
{
try
{
}
catch (FormCanceledException ex)
{
string reply;
if (ex.InnerException == null)
{
reply = "You have canceled the operation. Quitting from the HardwareDialog";
}
else
{
reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
}
await context.PostAsync(reply);
}
finally
{
context.Done<object>(null);
}
}
I want to send real-time notification in ASP.NET Boilerplate. Notification is saving successfully in Abp.NotificationSubscription table on subscription. When I publish the notification, the notification got saved in the Abp.Notification table but it is not displayed to the user in real-time.
My server-side code:
public async Task<RegisterOutput> Register(RegisterInput input)
{
public async Task<RegisterOutput> Register(RegisterInput input)
{
var user = await _userRegistrationManager.RegisterAsync(
input.Name,
input.Surname,
input.EmailAddress,
input.UserName,
input.Password,
true
);
_notificationSubscriptionManager.SubscribeToAllAvailableNotifications(user.ToUserIdentifier());
await _appNotifier.WelcomeToTheApplicationAsync(user);
var notification = _userNotificationManager.GetUserNotifications(user.ToUserIdentifier());
await _realTimeNotifier.SendNotificationsAsync(notification.ToArray());
// ...
}
}
_appNotifier implements WelcomeToTheApplicationAsync(user):
public async Task WelcomeToTheApplicationAsync(User user)
{
await _notificationPublisher.PublishAsync(
AppNotificationName.WelcomeToTheApplication,
new SendNotificationData("Naeem", "Hello I have sended this notification to you"),
severity: NotificationSeverity.Success,
userIds: new[] { user.ToUserIdentifier() }
);
}
SendNotificationData inherits from NotificationData:
public class SendNotificationData : NotificationData
{
public string name { get; set; }
public string message { get; set; }
public SendNotificationData(string _name, string _message)
{
name = _name;
message = _message;
}
}
I want to sent welcome notification to the user when he registers itself by using the register link on the login page. In the CloudbaseLine.Application project ... we have AccountAppService which contain the code for registering the user and sending notification to it after successful registration but the problem is notification go saved in Abp.NotificationSubscriptionTable and UserNotificationTable but user cannot received them.
public async Task<RegisterOutput> Register(RegisterInput input)
{
var user = await _userRegistrationManager.RegisterAsync(
input.Name,
input.Surname,
input.EmailAddress,
input.UserName,
input.Password,
true
);
_notificationSubscriptionManager.SubscribeToAllAvailableNotifications(user.ToUserIdentifier());
await _appNotifier.WelcomeToTheApplicationAsync(user);
var notification = _userNotificationManager.GetUserNotifications(user.ToUserIdentifier());
await _realTimeNotifier.SendNotificationsAsync(notification.ToArray());
// ...
}
public async Task WelcomeToTheApplicationAsync(User user)
{
await _notificationPublisher.PublishAsync(
AppNotificationName.WelcomeToTheApplication,
new SendNotificationData("Naeem", "Hello I have sended this notification to you"),
severity: NotificationSeverity.Success,
userIds: new[] { user.ToUserIdentifier() }
);
}
The user is not connected to the SignalR hub in the Register method.
One way to handle that is to enqueue a background job:
await _backgroundJobManager.EnqueueAsync<WelcomeNotificationJob, UserIdentifier>(
user.ToUserIdentifier(),
delay: TimeSpan.FromSeconds(5)
);
public class WelcomeNotificationJob : BackgroundJob<UserIdentifier>, ITransientDependency
{
private readonly IRealTimeNotifier _realTimeNotifier;
private readonly IUserNotificationManager _userNotificationManager;
public WelcomeNotificationJob(
IRealTimeNotifier realTimeNotifier,
IUserNotificationManager userNotificationManager)
{
_realTimeNotifier = realTimeNotifier;
_userNotificationManager = userNotificationManager;
}
[UnitOfWork]
public override void Execute(UserIdentifier args)
{
var notifications = _userNotificationManager.GetUserNotifications(args);
AsyncHelper.RunSync(() => _realTimeNotifier.SendNotificationsAsync(notifications.ToArray()));
}
}
Don't forget to register data formatters for custom notification data types, on the client side:
abp.notifications.messageFormatters['CloudBaseLine.Notification.SendNotificationData'] = function (userNotification) {
return userNotification.notification.data.message;
}
My bot receives some user info like order no and few other details based on that it searches the orders. If more that 1 order is returned then it asks user to provide more info so asks for 2 additional details.
Now my problem is that as I have already build the form using FormBuilder and on completion I ask user to provide more info so that should be the approach to ask for additional info. Should I create another form and request info or is there any way to add the additional fields to the same form in the completion method.
I want to ask for more user inputs after initial search is complete and msg for more than 1 order found is displayed.
As my properties SubOrderNumber & SubOrderVersion are in OrderQuery class so Should i build new form for OrderQuery or is there anyother way to add these two to existing Order form.
using System;
using Microsoft.Bot.Builder.FormFlow;
namespace Bot
{
[Serializable]
public class OrderSearchQuery
{
[Prompt("Please enter {&}")]
public string OrderNumber { get; set; }
[Prompt("Please enter {&}?")]
public String Location { get; set; }
[Prompt("Please provide last status {&}?")]
public string Status{ get; set; }
[Prompt("Please enter {&}?")]
public string SubOrderNumber { get; set; }
[Prompt("Please enter {&}?")]
public string SubOrderVersion { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Connector;
namespace Bot.Dialogs
{
[Serializable]
public class OrderDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Welcome to the Order helper!");
var orderFormDialog = FormDialog.FromForm(this.BuildOrderForm, FormOptions.PromptInStart);
context.Call(orderFormDialog, this.ResumeAfterOrdersFormDialog);
}
private IForm<OrderSearchQuery> BuildOrderForm()
{
OnCompletionAsyncDelegate<OrderSearchQuery> processOrderSearch = async (context, state) =>
{
await context.PostAsync($"Ok. Searching for Orders with Number: {state.OrderNumber}...");
};
return new FormBuilder<OrderSearchQuery>()
.Field(nameof(OrderSearchQuery.OrderNumber))
.AddRemainingFields(new string[] { nameof(RequiredDWPSearchQuery.SubOrderNumber), nameof(RequiredDWPSearchQuery.SubOrderVersion) })
.OnCompletion(processOrderSearch)
.Build();
}
private async Task ResumeAfterOrdersFormDialog(IDialogContext context, IAwaitable<OrderSearchQuery> result)
{
try
{
var searchQuery = await result;
await context.PostAsync($"I found total of 100 Ordes");
await context.PostAsync($"To get Order details, you will need to provide more info...");
}
catch (FormCanceledException ex)
{
string reply;
if (ex.InnerException == null)
{
reply = "You have canceled the operation. Quitting from the Required DWP Search";
}
else
{
reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
}
await context.PostAsync(reply);
}
finally
{
context.Done<object>(null);
}
}
}
}
While there are conditional parameters for fields and the possibility of doing logic in validation, I don't believe that would be the cleanest way to go. I would take a two dialog approach.
Here's the original form, with additional parameters removed:
using Microsoft.Bot.Builder.FormFlow;
using System;
namespace TestChatbot.Dialogs
{
[Serializable]
public class OrderSearchQuery
{
[Prompt("Please enter {&}")]
public string OrderNumber { get; set; }
[Prompt("Please enter {&}?")]
public String Location { get; set; }
[Prompt("Please provide last status {&}?")]
public string Status { get; set; }
}
}
Instead, I moved the additional properties to a new form:
using Microsoft.Bot.Builder.FormFlow;
using System;
namespace TestChatbot.Dialogs
{
[Serializable]
public class OrderFollowUp
{
[Prompt("Please enter {&}?")]
public string SubOrderNumber { get; set; }
[Prompt("Please enter {&}?")]
public string SubOrderVersion { get; set; }
}
}
Then I added logic to handle the second form, depending on the results of the search:
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
namespace TestChatbot.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Welcome to the Order helper!");
var orderFormDialog = FormDialog.FromForm(this.BuildOrderForm, FormOptions.PromptInStart);
context.Call(orderFormDialog, this.ResumeAfterOrdersFormDialog);
}
private IForm<OrderSearchQuery> BuildOrderForm()
{
return new FormBuilder<OrderSearchQuery>()
.Build();
}
private IForm<OrderFollowUp> BuildFollowUpForm()
{
return new FormBuilder<OrderFollowUp>()
.Build();
}
private async Task ResumeAfterOrdersFormDialog(IDialogContext context, IAwaitable<OrderSearchQuery> result)
{
try
{
var searchQuery = await result;
await context.PostAsync($"Ok. Searching for Orders with Number: {searchQuery.OrderNumber}...");
await context.PostAsync($"I found total of 100 Ordes");
int searchResultCount = 2;
if (searchResultCount > 1)
{
await context.PostAsync($"To get Order details, you will need to provide more info...");
var followUpDialog = FormDialog.FromForm(this.BuildFollowUpForm, FormOptions.PromptInStart);
context.Call(followUpDialog, this.ResumeAfterFollowUpDialog);
}
}
catch (FormCanceledException ex)
{
string reply;
if (ex.InnerException == null)
{
reply = "You have canceled the operation. Quitting from the Required DWP Search";
}
else
{
reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
}
await context.PostAsync(reply);
}
}
private async Task ResumeAfterFollowUpDialog(IDialogContext context, IAwaitable<OrderFollowUp> result)
{
await context.PostAsync($"Order complete.");
context.Done<object>(null);
}
}
}
The difference here is that I removed the OnCompletion call and handled that logic on the resumption handler, after the first dialog completes. Based on the search result, you can call the second dialog, much like you did the first to extract additional information.
I create simple bot with Microsoft BotFramework and i use FormFlow.
So:
[Serializable]
public class Test
{
public String Name {get;set;}
public uint Age {get;set; }
}
internal static IDialog<Test> MakeRootDialog()
{
return Chain.From(() => FormDialog.FromForm(Test.BuildForm));
}
And:
public async Task<Message> Post([FromBody]Message message)
{
if (message.Type == "Message")
{
return await Conversation.SendAsync(message, MakeRootDialog);
}
else
{
return HandleSystemMessage(message);
}
}
So, Micrisoft BotEmulator works well (and bot) and ask me Name and Age of Person.
But, how to get result of this choise to use it?
And how to know what user type it? Should i use ConversationId?
P.S. i mean how can i get result from user name and user age?
I try to use:
var name= result.GetBotPerUserInConversationData<Test>("Name");
But it return null;
P.P.S: i use Bot Emulator: and get json responce like this:
GetBotPerUserInConversationData:DialogState { some binary data }
So, i use
var name= result.GetBotPerUserInConversationData<Test>("DialogState");
But get an error:
"exceptionMessage": "Error converting value System.Byte[] to type 'Test'. Path ''.",
"exceptionType": "Newtonsoft.Json.JsonSerializationException"
Hi since you are building the form, you can get the result in FormFlowComplete callback method as below
private async Task yourFormFlowComplete(IDialogContext context, IAwaitable<yourclass> result)
{
var res = await result;//res will contain the result set, if you build the form with a class
}
You can just 'chain' a Do call to the Chain.From
internal static IDialog<Test> MakeRootDialog()
{
return Chain.From(() => FormDialog.FromForm(Test.BuildForm))
.Do(async (context, formResult) =>
{
var completed = await formResult;
//your logic
}
}
'completed' will have the result of the form with the entries from the user.
You can refer to the AnnotatedSandwichBot where they are doing exactly what you need here.