{"id":78178,"date":"2018-04-12T23:37:57","date_gmt":"2018-04-12T23:37:57","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=78178"},"modified":"2021-06-03T16:46:58","modified_gmt":"2021-06-03T16:46:58","slug":"creating-intelligent-bots-using-microsoft-bot-framework","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/cloud\/azure\/creating-intelligent-bots-using-microsoft-bot-framework\/","title":{"rendered":"Creating Intelligent Bots Using the Microsoft Bot Framework"},"content":{"rendered":"<p>Bots may have been known for decades, but they were used mostly in simple applications rather than for serious business cases. However, recently they became more intelligent, complex, and versatile. Nowadays bots can do much more, with multiple communication channel support and features such as natural language understanding or user intention recognition. They are a part of a cloud revolution which brought software to a new level, providing great availability and scalability for low development effort and maintenance cost. This article is focused on the Bot Framework \u2013 Microsoft\u2019s cloud solution for bot development.<\/p>\n<h2>A New Way to Communicate with the User<\/h2>\n<p>We are used to communicating through web or mobile communicators &#8211; millions of people using Facebook Messenger proves that. The good news is that bots can converse with us through some highly anticipated channels. That brings your company communication to a new level. Bot service is available 24 hours per day and can be designed to handle typical questions asked by people, which may reduce the staff needed to maintain such channels. You can provide instant support for your customers, partners and new leads.<\/p>\n<p>Another advantage of this kind of communication interface is the fact that it doesn\u2019t need an expensive and laborious process of bringing up an application UI. A bot usually communicates with the user using plain text through the channel that already provides a user interface. Depending on the channel, it has also a set of built-in components that can be used to display bot responses in a more attractive way. You can use images, videos, audio clips, buttons and lists of items not only to present the content, but also to interact with the user.<\/p>\n<h2>Put a Bot in Charge<\/h2>\n<p>Before starting a new bot project, you need to consider if it really is a solution for your business case. It\u2019s not recommended to start bot development just because it\u2019s a hot topic. However, in some cases, this kind of software can save a lot of time, money and resources. The following list of bot example use cases might help in making the decision:<\/p>\n<ul>\n<li>Answer for typical questions\n<ul>\n<li>A bot can make use of Q&amp;A knowledge to receive user question and provide an appropriate answer.<\/li>\n<li>Questions can be matched to correct answers using a LUIS (language understanding intelligent service) cognitive service.<\/li>\n<li>Reduced time can be spent by help desk staff answering typical questions.<\/li>\n<li>Example use cases are help chat, contact pages and web stores.<\/li>\n<\/ul>\n<\/li>\n<li>Alternative system interface\n<ul>\n<li>By integrating a bot with external systems (e.g. Outlook, Jira, CRM, SharePoint) a bot can become an alternative interface to work with these systems.<\/li>\n<li>A bot can simply ask some questions and gather the answers given by the user to submit data that normally would be filled in on a form.<\/li>\n<li>Example use cases are creating support tickets, uploading SharePoint documents, making calendar appointments, and providing translations.<\/li>\n<\/ul>\n<\/li>\n<li>Entertainment &amp; education\n<ul>\n<li>A bot can be also used to entertain and educate its recipients by sending various kinds of content to the user.<\/li>\n<li>It\u2019s a good idea to use media types like videos, audio, images and links to knowledge base articles.<\/li>\n<li>Example use cases are workout coach, recipes book and product adviser.<\/li>\n<\/ul>\n<\/li>\n<li>Notification bot\n<ul>\n<li>A bot can be scheduled to initialize conversations at appropriate time, notifying the user about some actions or reminding about things he should do.<\/li>\n<li>It\u2019s important to remember that sending proactive messages is not always possible \u2013 it depends on the channel used for communication.<\/li>\n<li>Example use cases are meeting reminders and timesheet reminders.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Why Do You Need a Bot?<\/h2>\n<p>When I joined a team assembled to develop a bot, I was confused \u2013 is it really the kind of software desired by enterprise customers? Then I realized I\u2019m asking the same questions I asked about web applications eight years ago. Since then, a lot of desktop and on-premise systems were migrated to the cloud. Web applications became a crucial part of this process. Maybe it\u2019s also a good idea to give bots a chance?<\/p>\n<p>Bot Framework is cloud-based technology which allows building a bot streamlined with ready-to-use components and services responsible for connectivity, intelligence, and integration. We can then assume that a bot built using this framework has some qualities and features characteristic of cloud-based software:<\/p>\n<ul>\n<li>Cost efficiency \u2013 Azure cloud allows selecting a fitting application service plan, depending on your needs. For applications generating small traffic, the cost is very small or even free.<\/li>\n<li>Scalability and reliability \u2013 you don\u2019t need to bother with what will happen if the traffic rapidly increases. Applications hosted on Azure can scale up to handle higher traffic and scale down when it\u2019s over. The availability of cloud services is also typically much higher compared to in-house IT infrastructure.<\/li>\n<li>Convenience \u2013 by hosting an application on the cloud, we don\u2019t need to care about the infrastructure. This is also significant cost reducer, ruling out the necessity of purchasing, managing and administering a local server room.<\/li>\n<li>Easy provisioning and deployment \u2013 there are tools allowing you to define the details of application services and other resources needed to host it. As an example, you can use ARM templates with Cake build script to build your code, provision resources on target Azure subscription with correct settings, and deploy compiled application from your local command line or build server.<\/li>\n<li>Integration with other systems \u2013 cloud computing makes it easy to connect web application with other systems, sharing the data or events amongst each other. That allows building well-integrated applications with relatively small effort.<\/li>\n<\/ul>\n<p>However, cloud services may also have some disadvantages:<\/p>\n<ul>\n<li>Cold starts \u2013 an application might get expropriated to release resources for other applications running on the server. That typically happens when the app remains idle for a long time. It\u2019s still available, but the first time it\u2019s called after a long idle period, you might experience a longer loading phase. This disadvantage is undesirable especially for bots, where a quick response is a key feature. However, a cold start is typical for lower app service plans and can be fixed by upgrading the plan.<\/li>\n<li>Security \u2013 a typical web application stores some data in the database. Usually the database is also hosted in the cloud, which mean it\u2019s potentially vulnerable.<\/li>\n<li>Localization &#8211; the fact that cloud servers are located in other countries may become a problem for some customers, depending on their security policy.<\/li>\n<\/ul>\n<h2>Bot Framework Architecture Overview<\/h2>\n<p>The Bot Framework divides the bot for some services. This partition allows the developer to focus on bot functionality while the framework handles communication with the user and conversation history storage. The following diagram in Figure 1 provides a general overview of typical bot architecture:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"246\" class=\"wp-image-78201\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-109.png\" \/><\/p>\n<p class=\"caption\">Figure 1. General services architecture of a basic bot<\/p>\n<h3>Bot Service (Bot Channel Registration)<\/h3>\n<p>The Microsoft Bot Framework comes with out-of-the-box support for number of channels, e.g. Skype, Skype for Business, Microsoft Teams, Facebook Messenger. Bot Service allows defining which channels are enabled for a bot and handles communication with these channels. All messages received by this service are getting posted to bot web API, returning the message that is getting sent back to the user.<\/p>\n<h3>Web API Service<\/h3>\n<p>The Web API Service provides an endpoint responsible for answering user activities. It\u2019s the developer\u2019s responsibility to implement this logic. However, the Bot Framework makes this job easier by introducing the concept of bot dialogs and a set of interfaces and types. The Bot Web API is also a service in which integration with external systems and services should be implemented.<\/p>\n<h3>Bot State Service<\/h3>\n<p>The conversation between a bot and a user is the most basic feature the Bot Framework provides. To make it efficient and fast, a conversation state is stored to the Bot State Service each time a new activity is being recorded. It helps to keep the asynchronous nature of Bot Web API and Bot Service. Bot state can be cached in memory or stored in persistent storage. Thanks to the fact that a bot state, kept in a database, is restorable at any point of time, it is possible to develop a bot capable of continuing previously initialized conversations.<\/p>\n<p>Note: In the beginning, Bot Framework shipped with the default State Service which was built into Bot Service. However, it\u2019s going to be deprecated in a future release. It\u2019s recommended to use other storage services like Azure Storage or Cosmos DB for bot state, especially for production bots.<\/p>\n<h2>Analyzing Bot Requirements<\/h2>\n<p>Bot conversation design might seem to be an easy task \u2013 you just need to provide an appropriate answer for predictable questions. However, things are not as simple as they might look, especially for complex projects. A person analyzing the bot requirements needs to think about bot behavior to provide a quality user-friendly experience. The following list presents example scenarios for which the bot should be prepared:<\/p>\n<ul>\n<li>Starting a new conversation<\/li>\n<li>Renewing a previously unfinished conversation<\/li>\n<li>How to react when a new user joins the conversation<\/li>\n<li>How to react when a user leaves the conversation<\/li>\n<li>Human-like reactions, e.g. saying hi, goodbye, telling jokes<\/li>\n<li>Reporting that something went wrong e.g. in case of external system outage<\/li>\n<li>Handling unrecognized user intents, e.g. fallback to a real person<\/li>\n<li>Handling alternative utterances of user input<\/li>\n<li>Processing files uploaded by the user<\/li>\n<\/ul>\n<p>The most intuitive way to define the bot scope is to design its conversation using dialog scenarios as shown in Figure 2:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"391\" height=\"530\" class=\"wp-image-78202\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-110.png\" \/><\/p>\n<p class=\"caption\">Figure 2. Example conversation flow scenario<\/p>\n<h2>Bot Development Challenges<\/h2>\n<p>Microsoft Bot Framework allows development of great bots. However, it\u2019s just a set of tools \u2013 we don\u2019t get a ready-to-use bot right after creating the project. Typically, a bot needs development to bring some functionalities. It\u2019s good to realize what kind of challenges we might be faced with.<\/p>\n<h3>Bot Intelligence<\/h3>\n<p>If the bot is about to converse with the user, it should have a form of intelligence. The most basic intelligence can be implemented programmatically using <strong>if<\/strong> or <strong>switch<\/strong> instructions. However, for more complex scenarios it\u2019s usually not enough. Luckily, Microsoft provides the <a href=\"https:\/\/www.luis.ai\"><em>Language Understanding Intelligent Service<\/em> (<em>LUIS<\/em>)<\/a> \u2013 a powerful cloud service providing natural language understanding features. This service allows anyone to build a custom cognitive language understanding model in an intuitive way \u2013 simply by providing some utterances and matching them with correct intents. Figure 3 shows an example of the design interface.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"535\" class=\"wp-image-78203\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-111.png\" \/><\/p>\n<p class=\"caption\">Figure 3. Defining LUIS intents and utterances.<\/p>\n<p>In addition to that, Microsoft Bot Framework comes with out-of-the box support for LUIS services to provide easy integration. It\u2019s worth keeping in mind that LUIS has a limited quota for a free usage per month, and it might be necessary to upgrade to a paid consumption plan.<\/p>\n<h3>Technical Limits<\/h3>\n<p>The architecture of Bot Framework is based on web services communicating with each other in an asynchronous way. That is the reason why the user does not receive bot responses immediately. The exact response time depends on the channel, but it\u2019s typically up to a few seconds. However, if bot services are configured for free or basic consumption plans, the response time might elevate to tens of seconds during the cold starts.<\/p>\n<h3>Multi-language Support<\/h3>\n<p>A basic bot can be implemented to communicate in any language, as long as messages sent by the user can be recognized by its logic. Things get more complicated for bots using LUIS service \u2013 it has support for a limited set of languages, and only one of them can be selected per each service instance (Figure 4). This means that, for multi-language bots, there may be a need to register and maintain multiple LUIS services, which might increase the costs.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"655\" class=\"wp-image-78204\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-112.png\" \/><\/p>\n<p class=\"caption\">Figure 4. LUIS app registration form with culture selection<\/p>\n<h3>Integration with Other Systems<\/h3>\n<p>In some cases, the functionality of a bot depends on integration with other systems. This is potentially a part of the code that might cause long response times. Therefore, the integration between the bot Web API service and an external service needs to be developed carefully, focusing on good performance. If the integration requires time-consuming queries, it might be a good idea to delegate these tasks to other cloud services like Azure Functions or Azure Web Jobs to cache the results in storage available for the bot.<\/p>\n<h3>Use Channel Features<\/h3>\n<p>It\u2019s worth taking advantage of the features each channel offers to bots, especially because it\u2019s easy to develop. However, you need to be aware that the Bot Framework doesn\u2019t guarantee all components will be available and will look and behave the same way on all available channels. You can check and compare channels features on <a href=\"https:\/\/docs.botframework.com\/en-us\/channel-inspector\/channels\/Skype\/\">Bot Framework Channel Inspector<\/a>.<\/p>\n<h2>Developing a Delivery Bot: from Theory to Practice<\/h2>\n<p>Creating a new intelligent bot was a challenge when the Bot Framework was initially introduced. However, as the framework is being constantly improved by Microsoft, the development became much easier. Let\u2019s see how to create a new bot with the language understanding feature called <em>Delivery bot<\/em>.<\/p>\n<p>To start, login to the Azure Portal and create a new resource of type <em>Web App Bot<\/em>. Alternatively, you can also create a <em>Functions Bot<\/em> \u2013 it would work the same way. For this example, stick to the Web App option shown in Figure 5:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"963\" height=\"611\" class=\"wp-image-78205\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-113.png\" \/><\/p>\n<p class=\"caption\">Figure 5. Creating a new Web App Bot resource<\/p>\n<p>In the next screen, you will fill in some important provisioning details. Let\u2019s go through them (Figure 6):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"728\" class=\"wp-image-78206\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-114.png\" \/><\/p>\n<p class=\"caption\">Figure 6. Bot provisioning details form.<\/p>\n<ul>\n<li><em>Bot name<\/em>: the name of the bot, that will be presented to the user. Note that it must be unique across Azure, so you will need to choose a different name.<\/li>\n<li><em>Subscription<\/em>: Azure subscriptions selector<\/li>\n<li><em>Resource group<\/em>: you can add a bot to an existing Azure resource group or create a new one; in this case, create a new group to have all resources separated from other existing ones<\/li>\n<li><em>Location<\/em>: the region in which the bot will be hosted; it\u2019s recommended to pick a location that is close to the target audience to provide shorter responses times.<\/li>\n<li><em>Pricing tier<\/em>: you can pick either free (<em>F0<\/em>) or paid (<em>S1<\/em>) plan for your bot.<\/li>\n<li><em>App name<\/em>: internal web application name determining the URL address of the bot messaging endpoint<\/li>\n<li><em>Bot template<\/em>: allows you to initialize a bot messaging endpoint with ready-to-work code; for this example, choose <em>Language understanding (C#)<\/em> template that will also initialize a new LUIS application.<\/li>\n<li><em>App service plan\/Location<\/em>: allows to create or select existing service plan, determining what compute resources will be available for Bot Web API Service. It is recommended to choose a service plan located in the same region as the location of bot resources being created.<\/li>\n<li><em>Azure storage<\/em>: the storage that will be used to keep the state of the bot; in this example, create a new one.<\/li>\n<li><em>Application Insights<\/em>: optional analytics for bot, collecting bot telemetry<\/li>\n<\/ul>\n<p>Having defined the bot configuration, you can simply click the <em>Create<\/em> button. After several minutes, the bot is up and running, ready to have a simple chat. You can take a quick look at the new resource group created on you Azure subscription to see the newly created resources shown in Figure 7:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"922\" height=\"577\" class=\"wp-image-78207\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-115.png\" \/><\/p>\n<p class=\"caption\">Figure 7. Bot resources on Azure Portal<\/p>\n<p>The bot consists of the following resources:<\/p>\n<ul>\n<li><em>Storage account<\/em> \u2013 for bot status storage,<\/li>\n<li><em>Web App Bot<\/em> \u2013 bot registration with some advanced settings such as channels configuration,<\/li>\n<li><em>App Service<\/em> \u2013 bot messaging endpoint web application,<\/li>\n<li><em>App Service Plan <\/em>\u2013 a service plan for bot App Service (only enlisted if a new service plan was created),<\/li>\n<li><em>Application Insights<\/em> \u2013 for bot analytics and telemetry.<\/li>\n<\/ul>\n<h2>Teach Your Bot to Understand the Business<\/h2>\n<p>Before starting a new conversation, you must put in some development effort to let bot understand your business cases. In this example, the bot is targeted for a logistics company. That\u2019s why you need to teach it to understand some example questions the customer might ask. To do this, you need to dive deeper into the LUIS service. You might have noticed that this service is not available in resources listed on Azure Portal in the new resources group. That\u2019s because LUIS is a separate service and doesn\u2019t get included as an Azure resource. LUIS applications can be explored on the <a href=\"https:\/\/www.luis.ai\">luis.ai<\/a> portal after signing in using the same credentials as for the Azure Portal. However, as Europe and Australia regions have their own subdomains (for performance reasons), the LUIS service localized in the European region is accessible on <a href=\"http:\/\/eu.luis.ai\">eu.luis.ai<\/a> portal. You can learn more about LUIS regions <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cognitive-services\/luis\/luis-reference-regions\">here<\/a>.<\/p>\n<p>When you sign in to the LUIS portal, you\u2019ll see the service you just created in the <a href=\"https:\/\/www.luis.ai\/applications\">applications<\/a> directory shown in Figure 8. Note that you will need to change the URL based on your region.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"890\" height=\"396\" class=\"wp-image-78208\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-116.png\" \/><\/p>\n<p class=\"caption\">Figure 8. Delivery bot LUIS service listed on applications view<\/p>\n<p>The learning process in LUIS service is based on intents and utterances. Surprisingly, the service provisioned by Azure portal already has some intents \u2013 you can explore them in the <em>Intents<\/em> section after clicking on the app name (Figure 9):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"540\" height=\"391\" class=\"wp-image-78209\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-117.png\" \/><\/p>\n<p class=\"caption\">Figure 9. The intents of the new LUIS service<\/p>\n<p>These intents were predefined to provide some basic bot interactions and are already in use. However, it\u2019s time to enhance the language understanding service with some new intents. New intents can be added using the <em>Create new intent<\/em> button. Add two intents with the following details:<\/p>\n<p>Intent name: <em>SendParcel<\/em> <br \/>\n Utterances:<\/p>\n<ul>\n<li>Send a new package<\/li>\n<li>I want to send a new parcel<\/li>\n<li>How can I send a new parcel?<\/li>\n<li>What should I do to send a parcel?<\/li>\n<\/ul>\n<p>Intent name: <em>TrackParcel<\/em> <br \/>\n Utterances:<\/p>\n<ul>\n<li>Where is my parcel?<\/li>\n<li>I want to track my parcel<\/li>\n<li>What is my parcel delivery status?<\/li>\n<li>When will my parcel be delivered?<\/li>\n<li>When will you deliver my package?<\/li>\n<\/ul>\n<p>Figure 10 shows the interface used to create the intents.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1014\" height=\"689\" class=\"wp-image-78210\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-118.png\" \/><\/p>\n<p class=\"caption\">Figure 10. Creating new LUIS intent with various utterances<\/p>\n<p>Once the two new intents are created, you\u2019ll see them in the Intents list as shown in Figure 11.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"607\" height=\"254\" class=\"wp-image-78211\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-119.png\" \/><\/p>\n<p class=\"caption\">Figure 11. The new intents<\/p>\n<h2>Train and Publish LUIS<\/h2>\n<p>By adding some new intents, you might have noticed the <em>Train<\/em> button, visible in the header of the LUIS web application, changed the status icon color to red. This means that there were some changes since the last time it was trained. By clicking on this button, you start the training process which normally takes a few seconds. After successful training, you may notice that a status icon of <em>Train<\/em> button changed to green. Additionally, the application shows the following message for a few seconds (Figure 12):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"37\" class=\"wp-image-78212\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-120.png\" \/><\/p>\n<p class=\"caption\">Figure 12. Message shown after successful LUIS training<\/p>\n<p>Once the training process is done, you can test it using the test tool available on the LUIS portal by clicking the <em>Test<\/em> button in the header. However, the fact that the service is trained doesn\u2019t mean that changes you made are already available for the bot. Before making use of new intents, you need to publish a new version of LUIS. To do this you need to go to the <em>Publish<\/em> section and click the <em>Publish to production slot<\/em> button.<\/p>\n<h2>Introducing New Intents to the Bot<\/h2>\n<p>Now it\u2019s time to start using the new LUIS intents in your bot. This means you need to make some changes in the bot messaging endpoints code. The easiest way to get access to the code is to open the <em>Build<\/em> section of the <em>Web App Bot<\/em> resource available in the bot resource group on the Azure Portal as shown in Figure 13.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"895\" height=\"603\" class=\"wp-image-78213\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-121.png\" \/><\/p>\n<p class=\"caption\">Figure 13. Bot management build section<\/p>\n<p>As you can see, there are two options here:<\/p>\n<ul>\n<li>Open online code editor<\/li>\n<li>Download zip file<\/li>\n<\/ul>\n<p>If you are planning to work on some advanced features it is recommended to download a zip file. It contains a standard .NET solution, so you can develop your bot features using Visual Studio. In this case that\u2019s not necessary, and you can simply use the online code editor. After opening it and exploring the code, you might recognize that bot functionality is based on a single dialog implemented in the <em>Dialogs<\/em>\/<em>BaseLuisDialog.cs<\/em> file. Modify it with the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">using System;\r\nusing System.Configuration;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.Bot.Builder.Dialogs;\r\nusing Microsoft.Bot.Builder.Luis;\r\nusing Microsoft.Bot.Builder.Luis.Models;\r\n\r\nnamespace Microsoft.Bot.Sample.LuisBot\r\n{\r\n    [Serializable]\r\n    public class BasicLuisDialog : LuisDialog&lt;object&gt;\r\n    {\r\n        public BasicLuisDialog() : base(\r\n            new LuisService(\r\n                new LuisModelAttribute(\r\n                    ConfigurationManager.AppSettings[\"LuisAppId\"], \r\n                    ConfigurationManager.AppSettings[\"LuisAPIKey\"], \r\n                    domain: ConfigurationManager.AppSettings[\"LuisAPIHostName\"])))\r\n        {\r\n        }\r\n        [LuisIntent(\"None\")]\r\n        public async Task NoneIntent(IDialogContext context, LuisResult result)\r\n        {\r\n            await Answer(context, \"Sorry, I couldn't understand you.\");\r\n        }\r\n        [LuisIntent(\"TrackParcel\")]\r\n        public async Task TrackParcelIntent(IDialogContext context, LuisResult result)\r\n        {\r\n            await Answer(context, \r\n                \"Good news! Your package will be delivered in 24 hours!\");\r\n        }\r\n        [LuisIntent(\"SendParcel\")]\r\n        public async Task SendParcelIntent(IDialogContext context, LuisResult result)\r\n        {\r\n            await Answer(context, \"Please visit our website and follow the instructions.\");\r\n        }\r\n        [LuisIntent(\"Greeting\")]\r\n        public async Task GreetingIntent(IDialogContext context, LuisResult result)\r\n        {\r\n            await Answer(context, \"Hi! I'm delivery bot! How can I help you?\");\r\n        }\r\n        [LuisIntent(\"Cancel\")]\r\n        public async Task CancelIntent(IDialogContext context, LuisResult result)\r\n        {\r\n            await Answer(context, \"Cancelled!\");\r\n        }\r\n        [LuisIntent(\"Help\")]\r\n        public async Task HelpIntent(IDialogContext context, LuisResult result)\r\n        {\r\n            await Answer(context,\r\n                \"You can ask me for parcel tracking status or to send a new one :)\");\r\n        }\r\n        private async Task Answer(IDialogContext context, string message) \r\n        {\r\n            await context.PostAsync(message);\r\n            context.Wait(MessageReceived);\r\n        }\r\n    }\r\n}<\/pre>\n<p>This class is implementing by a special kind of dialog by inheriting the <strong>LuisDialog<\/strong> type. This type is provided by the Bot Framework API and allows implementing a bot dialog powered by LUIS. It requires obtaining some details about the LUIS application. Luckily, LUIS application details were stored in application settings by the Azure provisioning process and can be easily retrieved from Application Settings. If you still want to find the details of LUIS application on your own, you should navigate to LUIS portal, choose your application, go to <em>Publish<\/em> tab and check the <em>Resources and Keys<\/em> section for your region. All details you need are available in <em>Endpoint<\/em> value of the existing key:<\/p>\n<ul>\n<li><em>LuisAPIKey<\/em> \u2013 the value (key string) of the existing key (highlighted by red color),<\/li>\n<li><em>LuisAppId<\/em> \u2013 the identifier of the LUIS app (can be also found in <em>Settings<\/em> tab; highlighted by blue color),<\/li>\n<li><em>LuisAPIHostName<\/em> \u2013 the domain name of the server hosting LUIS app (highlighted by green color)<\/li>\n<\/ul>\n<p>Figure 14 shows these details.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1186\" height=\"565\" class=\"wp-image-78214\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-122.png\" \/><\/p>\n<p class=\"caption\">Figure 14. Identifying LUIS app details on LUIS portal<\/p>\n<p>The implementation of the LUIS dialog is based on public methods decorated with <em>LuisIntent<\/em> annotation. This decorator requires an intent name parameter. Once the method is properly decorated, it becomes an event handler of matching LUIS intent in scope of the current dialog.<\/p>\n<p>Under the hood, the Bot Framework is forwarding each message sent by the user to the LUIS service and waits for the response. The Language Understanding service matches the input message with all intents and returns a score table as a response. Finally, the Bot Framework receives the response and triggers the method associated with the best-scoring intent.<\/p>\n<p>Having modified the bot code, you need to build it. To do this, open the console section available in online editor.<\/p>\n<p>You will find the button to open console here as shown in Figure 15:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"336\" height=\"242\" class=\"wp-image-78215\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-123.png\" \/><\/p>\n<p class=\"caption\">Figure 15: The button to open the console<\/p>\n<p>Type in <strong>build.cmd<\/strong> and press<strong> Enter<\/strong>. This command directly rebuilds a bot messaging endpoint app deployed on App Service. Figure 16 shows how the console window should look.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"483\" height=\"314\" class=\"wp-image-78216\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-124.png\" \/><\/p>\n<p class=\"caption\">Figure 16. Building the app<\/p>\n<p>Once the build is complete, you will see a success message at the bottom of the window similar to that shown in Figure 17:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"467\" height=\"279\" class=\"wp-image-78217\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-125.png\" \/><\/p>\n<p class=\"caption\">Figure 17. The successful build<\/p>\n<h2>Testing a Bot<\/h2>\n<p>After these simple steps, the bot is ready to test. You can use a test chat component available in the <em>Test in Web Chat<\/em> section of the <em>Web App Bot<\/em> resource on the Azure Portal to start a test conversation. Figure 18 shows a successful test.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"920\" height=\"711\" class=\"wp-image-78218\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/04\/word-image-126.png\" \/><\/p>\n<p class=\"caption\">Figure 18. Test conversation with Delivery bot<\/p>\n<h2>Next Steps<\/h2>\n<p>The bot you have created is just a mockup. To make it more useful, you would need to integrate it with a real logistics system to allow tracking and sending parcels. The Bot will probably have more complex dialogs, allowing it to ask for some details such as parcel identity number or recipient details. These scenarios can be realized using another LUIS cognitive feature called entities, which allows it to recognize some important input data in the user message. The production-ready bot should also be available on a public channel, configurable in the <em>Channels<\/em> section of <em>Bot Web App<\/em> resource.<\/p>\n<h2>Summary<\/h2>\n<p>Bots turned out to be a totally new way of creating software, and there are many areas in which they can fit. Despite the fact that the Microsoft Bot Framework approach uses well-known technologies such as the web API, the analysis and development approaches are significantly different than in case of classic web applications. That is why bot development is an exciting and challenging job. Additionally, it doesn\u2019t necessarily need expensive tools, as the development can be done completely on Azure Portal using a web browser. In the end, a successfully finished bot project brings a lot of satisfaction for its creators, project owners, and end users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure, Microsoft&#8217;s cloud, provides dozens of services to make cloud development fast without the need to worry about infrastructure such as hardware. In this article, Jakub Kaczmarek explains the benefits and challenges of creating a bot using the Microsoft Bot Framework found on Azure. He also walks you through creating a simple bot.&hellip;<\/p>\n","protected":false},"author":316282,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538,137091],"tags":[],"coauthors":[50410],"class_list":["post-78178","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","category-azure"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/78178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/316282"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=78178"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/78178\/revisions"}],"predecessor-version":[{"id":78220,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/78178\/revisions\/78220"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=78178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=78178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=78178"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=78178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}