Friday 27 July 2018

C# Bot as an Enabler—Azure Web App Bot (Quick Start)

This article introduces the Live Assist for Microsoft Dynamics 365 Bot as an Enabler Bot C# SDK, running within the Azure Web App Bot Framework.
The examples contained here do not provide a detailed guide for developing bots within the Microsoft Azure framework. We assume that the developer has experience of C# and the Microsoft Azure Framework, and that they are able to use this quick start guide as a starting point to become familar with the Live Assist SDK.
Note: The Microsoft Web App Bot Framework is currently in development. You may need to make some changes in the steps, or the code, for your development effort. We recommend that you develop your application using Microsoft Visual Studio Professional—do not use the App Service Editor provided in the Azure web interface.
This bot receives chats initiated with Microsoft Web App Bot Service. The bot echoes any messages that a vistor sends, sending their original message back to them. When the visitor sends a message containing 'help', the Live Assist Bot SDK is invoked to escalate the call to a Live Assist Skill Group for processing.
The bot, by using the Live Assist SDK, proxies messages between the Azure Web Chat service and the Agent using Live Assist.
See also: BotBuilder samples repository  

Step 1—Creating a C# basic Web App bot

  1. In Azure, create a new C# Web App Bot.
  2. Set the appropriate Storage and Billing requirements for your application.
    The significant parts of this process are:
    • Selecting a name which is unique within Azure
    • Selecting the C# (Basic) Application
    2018-05-02 16_57_25-Web App Bot - Microsoft Azure.png
    This process may take Azure a few minutes to build.

Step 2—Downloading the Project Code from Azure

  • When the Web App Bot has been created, you can download the Project code:
    2018-04-13 16_10_22-Choose how to work with your code - Microsoft Azure.png
    Azure prepares the source code to download—you need to unzip the archive.

Step 3—Loading the code into Visual Studio, and importing the Live Assist Reference

  1. The unzipped archive contains the follow Solution file:
    Microsoft.Bot.Sample.SimpleEchoBot.sln
    Open this file in Visual Studio Professional.
    Note: We used Visual Studio 2017 for this example. Other IDEs may appear differently.
  2. Import the CaféX Live Assist Reference file from NuGet.
    From the Solution Explorer, right-click References and click Manage Nuget Packages
    2018-04-13 16_18_38-Microsoft.Bot.Sample.SimpleEchoBot - Microsoft Visual Studio  (Administrator).png
    You may see a warning from NuGet saying that packages are missing. Click Restore.
  3. Under Browsesearch for Live Assist and install the LiveAssistBotSDK package.2018-04-13 16_22_58-Microsoft.Bot.Sample.SimpleEchoBot - Microsoft Visual Studio  (Administrator).png
  4. When the NuGet package is installed, you can begin development.

Step 4—Developing your application

Based on the Microsoft SimpleEchoBot sample bot, make the following changes to the EchoDialog.cs file:
  1. Managing Imports:
    The following packages are required:
    using Microsoft.Bot.Builder.ConnectorEx;
    using Newtonsoft.Json;
    using System.Timers;
    using Cafex.LiveAssist.Bot;
  2. Declaring your static variables of the EchoDialog:
    private static Sdk sdk;
    private static ChatContext chatContext;
    private static string conversationRef;
    private static Timer timer;
  3. Load the SDK in the StartAsync method
    Make sure that you define your Account Number (Find your Account Number )
                sdk = sdk ?? new Sdk(new SdkConfiguration()
                {
                    AccountNumber = "__CHANGE_ME__"
                });
  4. The beginning of the if block needs to check if the chat has already been escalated, so the echo doesn't occur
                if (chatContext != null) 
                {
                  // As chatContext is not null we already have an escalated chat.
                  // Post the incoming message line to the escalated chat
                  await sdk.PostLine(activity.Text, chatContext);
                }else if (activity.Text == "reset")
          
  5. Add an extra trigger to handle 'help' in the MessageReceivedAsyc method
                else if (activity.Text.Contains("help"))
                {
                    // "help" within the message is our escalation trigger.
                    await context.PostAsync("Escalating to agent");
                    await Escalate(activity); // Implemented in next step.
                }
  6. Define the Escalate method
    Make sure that you specify an Agent Skill group to target.
            private async Task Escalate(Activity activity)
            {
                // This is our reference to the upstream conversation
                conversationRef = JsonConvert.SerializeObject(activity.ToConversationReference());
    
                var chatSpec = new ChatSpec()
                {
                    // Set Agent skill to target
                    Skill = "__CHANGE_ME__",
                    VisitorName = activity.From.Name
                };
    
                // Start timer to poll for Live Assist chat events
                if (timer == null)
                {
                    timer = timer ?? new Timer(5000);
                    // OnTimedEvent is implemented in the next step
                    timer.Elapsed += (sender, e) => OnTimedEvent(sender, e);
                    timer.Start();
                }
    
                // Request a chat via the Sdk    
                chatContext = await sdk.RequestChat(chatSpec);
            }
  7. Define the Enabler Logic
    When the OnTimedEvent fires, the messages are passed between the Visitor and the Bot, and the Agent and the Bot.
            async void OnTimedEvent(Object source, ElapsedEventArgs eea)
            {
                if (chatContext != null)
                {
                    // Create an upstream reply
                    var reply = JsonConvert.DeserializeObject<ConversationReference>(conversationRef)
                        .GetPostToBotMessage().CreateReply();
    
                    // Create upstream connection on which to send reply 
                    var client = new ConnectorClient(new Uri(reply.ServiceUrl));
    
                    // Poll Live Assist for events
                    var chatInfo = await sdk.Poll(chatContext);
    
                    if (chatInfo != null)
                    {
                        // ChatInfo.ChatEvents will contain events since last call to poll.
                        if (chatInfo.ChatEvents != null && chatInfo.ChatEvents.Count > 0)
                        {
                            foreach (ChatEvent e in chatInfo.ChatEvents)
                            {
                                switch (e.Type)
                                {
                                    // type is either "state" or "line".
                                    case "line":
                                        // Source is either: "system", "agent" or "visitor"
                                        if (e.Source.Equals("system"))
                                        {
                                            reply.From.Name = "system";
                                        }
                                        else if (e.Source.Equals("agent"))
                                        {
                                            reply.From.Name = chatInfo.AgentName;
    
                                        }
                                        else
                                        {
                                            break;
                                        }
    
                                        reply.Type = "message";
                                        reply.Text = e.Text;
                                        client.Conversations.ReplyToActivity(reply);
                                        break;
    
                                    case "state":
                                        // State changes
                                        // Valid values: "waiting", "chatting", "ended"
                                        if (chatInfo.State.Equals("ended"))
                                        {
                                            chatContext = null;
                                        }
                                        break;
                                }
                            }
                        }
                    }
                }
            }

    A completed example of the EchoDialog.cs is available from our GitHub repository.

Step 5—Publishing your bot

Step 6—Testing your bot

  • When you have published your bot, you can quickly test the new Chat Bot from Microsoft Azure > Test Web Chat tab.
    Important: You need an Agent logged into LiveAssist for Microsoft Dynamics 365, under the correct skill group, to receive the escalated chat.
    2018-04-16 12_53_41-MyCafexBot - Microsoft Azure.pngimage (25).png

Step 7—Debugging your bot

There are numerous ways to debug your bot application.
The following Microsoft documents may assist you with this.

No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...