Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Add Conversational Capabilities to Your Microsoft Bot

DZone's Guide to

Add Conversational Capabilities to Your Microsoft Bot

Find out how to create your own bot with this easy to follow tutorial.

· AI Zone ·
Free Resource

Insight for I&O leaders on deploying AIOps platforms to enhance performance monitoring today. Read the Guide.

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 use 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”Image title
  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 = "9462";
    
            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” and 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.

TrueSight is an AIOps platform, powered by machine learning and analytics, that elevates IT operations to address multi-cloud complexity and the speed of digital transformation.

Topics:
cortana ,amazon alexa ,google home ,tutorial ,artificial intelligence ,machine learning ,bot

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}