Getting Started with Dialog using Microsoft Bot framework
Bot Builder SDK introduced Dialogs, Users allow to model conversations and manage conversation flow. Dialogs can contain waterfall steps and prompts
In this article, we will learn about the condition inside
Prerequisite:
I have explained about Bot framework Installation, deployment and implementation the below article- Getting Started with
Chatbot Using Azure Bot Service - Getting Started with Bots Using Visual Studio 2017
- Deploying A Bot to Azure Using Visual Studio 2017
- How to Create ChatBot In Xamarin
Create New Bot Service:
Let startThe Bot application template was created with all the components and all required NuGet references installed in the solutions
In this Solution, we have two main class MessagesController
RootDialog Class:
Step 1:
You can create / Edit the RootDialog class, create a class that is marked with the [Serializable] attribute (so the dialog can be serialized to state) and implement the[Serializable]
{
}
Step 2:
{
}
Step 3:
You can wait for a message from the conversation, call context
[Serializable]
{
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
}
}
Step 4:
Let start edit MessageReceivedAsync method as per your requirement or create new async method. The following code is welcome message conversation dialog.[Serializable]
{
static string username;
int count = 0;
{
await context.PostAsync("Hello, I am Microsoft Bot");
context.Wait(MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var message = await result as Activity;
if(count >0)
username = message.Text;
if (string.IsNullOrEmpty(username))
{
await context.PostAsync("what is your good name");
count++;
}
{
await context.PostAsync($"Hello {username} , How may help You");
}
context.Wait(MessageReceivedAsync);
}
}
IDialogContext have following Public Member Functions
Context.Call< R > (IDialog< R > child, ResumeAfter< R > resume)
Call a child dialog and add it to the top of the stack.
Context.Done< R > (R value)
Complete the current dialog and return a result to the parent dialog.
Context .Fail (Exception error)
Fail the current dialog and return an exception to the parent dialog.
Context .Forward< R, T > (IDialog< R > child, ResumeAfter< R > resume, T item, CancellationToken token)
Call a child dialog, add it to the top of the stack and post the item to the child dialog.
Context .Post< E > (E @event, ResumeAfter< E > resume)
Post an internal event to the queue.
Context .Reset ()
Resets the stack
Context .Wait< R > (ResumeAfter< R > resume)
Suspend the current dialog until an external event has been sent to the bot.
MessagesController Class :
The RootDialog class is added to the conversation in the MessageController class via the Post() method. In the Post() method, the call to Conversation.SendAsync() creates an instance of the RootDialog, adds it to the dialog stack to make it the active dialog, calling the RootDialog.StartAsync() from Post method[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
}
Run Bot Application:
The emulator is a desktop application that lets we test and debug our bot on- Test Application on Bot Emulator
- You can follow the below steps to test your bot application.
- Open Bot Emulator.
- Copy the above
localhost url and paste it in emulator e.g. - http://localHost:3979 - You can append the /
api /messages in the above url; e.g. - http://localHost:3979/api/messages. - You won't need to specify Microsoft App ID and Microsoft App Password for
localhost testing, so click on "Connect".
0 Comments