Creating and managing Bot Channels with Azure Bot Service

Introduction

In recent years, bots have become ubiquitous in our daily lives. They have been enhanced with Artificial Intelligence (AI) technologies and Natural Language Processing (NLP) capabilities, making them capable of performing a variety of tasks like customer support, scheduling appointments, and more. With the increasing adoption of bots in business settings and consumer technology, there has been a great demand for creating and managing bot channels.

Azure Bot Service, an offering by Microsoft, allows developers to build, connect, test, and deploy intelligent bots that interact with users anywhere including email, SMS, Facebook Messenger, Discord, Slack, and others. In this tutorial, we will be discussing how to create and manage bot channels with Azure Bot Service.

Prerequisites

Before we start, you should have the following requirements:

  1. Basic knowledge of programming in C# .NET Core.
  2. Visual Studio 2019.
  3. Azure account with an active subscription.
  4. Basic knowledge of Azure portals such as the Azure portal and Azure DevOps.

Step 1: Creating an Azure Bot Service

The first step is to create an Azure Bot Service instance that can be used to deploy intelligent bots. We can create a bot service instance by following these steps:

  1. Log in to the Azure portal.
  2. Click on “+ Create a resource” on the left-sidebar.
  3. Search for “Bot Channels Registration” in the search bar and click “Create.”
  4. Fill out the required information, such as subscription, resource group, and bot name.
  5. Select the pricing tier which best fits your requirements.
  6. Click “Create” to create the bot service instance.

In a few minutes, the Azure portal will create an Azure Bot Service instance with a Microsoft App ID and App Password which can be used to authenticate with your bot.

Step 2: Creating a Bot

The next step is to create a bot, which can be done using Visual Studio 2019. Here’s how:

  1. Open Visual Studio 2019 on your computer.
  2. Click on “Create a new project” on the home screen.
  3. In the project creation wizard, search for “Bot” and select “Bot Framework Bot” as the project template.
  4. Give your bot a name and click “Create.”

Once you’ve created your bot, it’s time to add its dependencies.

Step 3: Adding Bot Dependencies

In this step, we will be adding NuGet packages that are necessary for the bot to function.

  1. Right-click on the project in the Solution Explorer and click “Manage NuGet Packages.”
  2. In the NuGet Package Manager window search for “Microsoft.Bot.Builder” and install the package.
  3. Search for “Microsoft.Bot.Connector” and install the package.
  4. Search for “Microsoft.Bot.Builder.Azure” and install the package. This package adds support for using Azure Storage for state management.

Now that we’ve added the necessary dependencies, it’s time to configure our bot for Azure.

Step 4: Configuring the Bot for Azure

In this step, we will add the necessary configuration information to our bot to allow it to interact with our bot service.

  1. Open the appsettings.json file in Visual Studio.
  2. Add the following configuration settings:
{
    "MicrosoftAppId": "<your Microsoft App ID>",
    "MicrosoftAppPassword": "<your Microsoft App Password>",
    "BotOpenIdMetadata": "",
    "BotBaseUrl": "https://<your bot name>.azurewebsites.net/api/",
    "StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<your-account-key>"
}

Replace <your Microsoft App ID> and <your Microsoft App Password> with the App ID and App Password that you obtained when you created your Azure Bot Service instance.

Replace <your bot name> with the name that you gave your bot when creating the Azure Bot Service instance.

Replace <your-account-name> and <your-account-key> with your Azure storage account name and key respectively.

With the bot now being configured for Azure, it’s time to write some code to handle user interactions.

Step 5: Writing Bot Code

Now it’s time to write code that will handle the user’s interactions with your bot. We will be using C# as our primary language.

Open the Bot.cs file in your bot project and add the following code:

using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Schema;

namespace <your-bot-namespace>
{
    public class Bot : IBot
    {
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                await turnContext.SendActivityAsync($"You said '{turnContext.Activity.Text}'.");
            }
        }
    }
}

This code will simply send a message back to the user with what they typed in your bot channel. However, you can customize this function to handle interactions based on different types of Activity.

Step 6: Deploying the Bot to Azure

Now that we’ve written the code for our bot, we can deploy it to our Azure Bot Service instance. Here are the steps to do so:

  1. Right-click on your project in Visual Studio and click “Publish.”
  2. In the “Publish” dialog, select “Azure App Service.”
  3. Click “Create New” to create a new Azure App Service instance.
  4. Fill in the necessary information and click “Create.”
  5. In the “Publish” window, select the Azure App Service instance that you just created.
  6. Click “Publish” to deploy your bot.

After deploying the bot, you should see the message “Your application was successfully published.”

Step 7: Testing the Bot Channel

Now it’s time to test our bot in the bot channel that we’ve created. Here’s how:

  1. Log in to the Azure portal.
  2. Go to the Bot Channels Registration that you created earlier.
  3. Click “Test in Web Chat.”
  4. In the Web Chat window, test out your bot by typing different messages and see how it responds.

Congratulations! You’ve successfully created, deployed, and tested your first bot channel using Azure Bot Service.

Conclusion

Azure Bot Service is a powerful tool that allows developers to create and manage bot channels with ease. In this tutorial, we’ve discussed how to create and manage a bot channel with Azure Bot Service, configure and deploy the bot to Azure, and test it in a bot channel. The possibilities of using Azure Bot Service are endless, and we encourage you to explore it further to leverage the full potential of bot development.

Related Post