Whenever I talk about Bots, I always think of those Sci-fi movies where you can have those super complex and intelligent digital beings. When I heard that the Bot Framework is out, I wanted to try it and it wasn’t that easy to learn and develop. I know I am a bit late in this blog, so the words might be new but the technology is not that recent.
First off, you need to follow this post to set up the environment and the template for the bot. So, when you create a new bot project, you will be given some template files on which you can work directly. This is what we are going to do. I will directly use those templates and change them to more meaningful and understandable code.
If you see the template generated files, you will observe that the code is divided into two main parts - Controller and Dialog. The Controller will send the message that will come from the user via the bot interface and it will send it to the dialog. The dialog will process the message and will reply accordingly. We can have multiple dialogs which you can consider as routes to a website.
How we will work.
MessageController
No change here. So, we will use the same file as the template generated.
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using Microsoft.Bot.Builder.Dialogs;
- using Microsoft.Bot.Connector;
- using Microsoft.AspNetCore.Mvc;
- namespace BotTry1.Controllers
- {
- [Route("api/[controller]")]
- [BotAuthentication]
- public class MessagesController : Controller
- {
- /// <summary>
- /// POST: api/Messages
- /// Receive a message from a user and reply to it
- /// </summary>
- [HttpPost]
- public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
- {
- if (activity?.Type == ActivityTypes.Message)
- {
- await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
- }
- else
- {
- HandleSystemMessage(activity);
- }
- return new HttpResponseMessage(HttpStatusCode.OK);
- }
- private Activity HandleSystemMessage(Activity message)
- {
- if (message.Type == ActivityTypes.DeleteUserData)
- {
- // Implement user deletion here
- // If we handle user deletion, return a real message
- }
- else if (message.Type == ActivityTypes.ConversationUpdate)
- {
- // Handle conversation state changes, like members being added and removed
- // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
- // Not available in all channels
- }
- else if (message.Type == ActivityTypes.ContactRelationUpdate)
- {
- // Handle add/remove from contact lists
- // Activity.From + Activity.Action represent what happened
- }
- else if (message.Type == ActivityTypes.Typing)
- {
- // Handle knowing tha the user is typing
- }
- else if (message.Type == ActivityTypes.Ping)
- {
- }
- return null;
- }
- }
- }
RootDialog
Here, we will instruct the dialog to get user profile information. Do remember that this class needs to be serializable. This class is called to ensure the user profile- either by loading an existing profile or creating a new one if stored. And then, return the context back to the root dialog.

Join the conversation! Your thoughts help the community grow.