1. Home
  2. Docs
  3. Tutorials
  4. Add Conversational capabilities to your Microsoft Bot

Add Conversational capabilities to your Microsoft Bot

Microsoft Bot Framework can help you to build, connect, deploy, and manage intelligent bots to naturally interact with your users on a website, app, Cortana, Microsoft Teams, Skype, Slack, Facebook Messenger, and more. In this tutorial, we will demonstrate how to connect the Microsoft Bot Framework to your conversation.one omni-channel conversational application, and reuse the code and data created for your Amazon Alexa skills, and Google Home actions to other channels supported by the Microsoft Bot Framework.

Create a conversational app

A Conversation.one application represents your conversational interface through Amazon Alexa, Google Home or a chatbot bot. To create an app:

  1. Log in to the Conversation.one dashboard. If you don’t already have an account, sign up for free.
  2. Follow the steps in the tutorials “Building Your First Conversational App” and “Building a Dynamic Conversational App Using Web APIs”
For the purpose of this tutorial you can a ready-made conversational app

Create a Microsoft Bot Framework chatbot

  1. Log in to Microsoft Azure. If you don’t already have an account, sign up for free.
  2. Click “Create a resource”
    Microsoft Bot Framework Tutorial
  3. Search for “bot” and select “Web App Bot”
    Microsoft Bot Framework Tutorial
  4. Click “Create”
    Microsoft Bot Framework Tutorial
  5. Select a unique bot name “WhatHappenedToday” with some random number in the end.
    Click “Create”
    Microsoft Bot Framework Tutorial
  6. Wait a couple of minutes, until you the “Deployment Succeeded” message.
    Click “Go to resource”
    Microsoft Bot Framework Tutorial
  7. Select “Build” and click “Open online code editor”
    Microsoft Bot Framework Tutorial
  8. Open “EchoDialog.cs” and click “Open online code editor”
    Replace the code with the code below

    using System;
    using System.Threading.Tasks;
    
    using Microsoft.Bot.Connector;
    using Microsoft.Bot.Builder.Dialogs;
    using System.Net.Http;
    using System.Net;
    using System.IO;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System.Text.RegularExpressions;
    
    namespace Microsoft.Bot.Sample.SimpleEchoBot
    {
        
        [Serializable]
        public class EchoDialog : IDialog<object>
        {
            const string AppID = "9461";
    
            public async Task StartAsync(IDialogContext context)
            {
                context.Wait(MessageReceivedAsync);
            }
    
            public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
            {
                try
                {
                    var message = await argument;
                              
                    string sessionID = "";
                    
                    context.UserData.TryGetValue("SessionID", out sessionID);
                    
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://engine.conversation.one/chatbot/"+AppID);
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Method = "POST";
                    
                    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        string json = "{\"sessionID\":\""+sessionID+"\",\"userInput\": {\"text\": \""+message.Text.Trim()+"\"},\"type\": \"input.text\"}";
                    
                        streamWriter.Write(json);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                     
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    var streamReader = new StreamReader(httpResponse.GetResponseStream());
                    var result = streamReader.ReadToEnd();
                    dynamic jsonResponse = JObject.Parse(result);
                    
                    context.UserData.SetValue("SessionID", jsonResponse["sessionID"]);
                    
                    var outMessage = context.MakeMessage();
                    
                    var output = jsonResponse["output"][0]["content"].ToString(Formatting.None);
                    
                    int index = output.IndexOf('"');
                    output = output.Remove(index, 1);
                    index = output.LastIndexOf('"');
                    output = output.Remove(index, 1);    
                    
                    outMessage.Speak = output;
            
                    outMessage.Text = Regex.Replace(output, @"<[^>]*>", String.Empty);
    
                    await context.PostAsync(outMessage);
                    context.Wait(MessageReceivedAsync);
                } 
                catch (Exception e)
                {
                    await context.PostAsync("An error occurred: "+e);
                     context.Wait(MessageReceivedAsync);
                }         
            }
        }
    }
    You should replace “AppID” with your own conversation.one app id.
    Microsoft Bot Framework Tutorial
  9. Select “Open Console”, type “build.cmd” and click “Enter”
    Microsoft Bot Framework Tutorial
    Wait for the build process to finish
    Microsoft Bot Framework Tutorial

Try it out

Now that your Microsoft bot is connected to your conversational flow, try out what you have so far.

  1. Go back to the Microsoft Azure dashboard, and select “Test in Web Chat”
    Microsoft Bot Framework Tutorial
  2. Send the message “What happened today”, the bot will respond with an interesting event from the past.
    Microsoft Bot Framework Tutorial

What’s next?

You can connect your Microsoft bot to other channels and services such as Microsoft Cortana. To do that follow the tutorial here.
You can learn how to further develop and enhance your conversational app, and use it for Amazon Alexa, Google Home, Facebook, and Cortana, by following the tutorials here.

Was this article helpful to you? Yes No

How can we help?