Deploy your bot

APPLIES TO: yesSDK v4 no SDK v3

In this article we will show you how to deploy a basic bot to Azure. We will explain how to prepare your bot for deployment, deploy your bot to Azure, and test your bot in Web Chat. It would be useful to read this article before following the steps, so that you fully understand what is involved in deploying a bot.

Important

Make sure that you are using the updated version of the Azure CLI. For information about the latest release, see Install the Azure CLI.

Prerequisites

Prepare for deployment

When you create a bot using a Visual Studio template, Yeoman template, or Cookiecutter template the source code generated includes a deploymentTemplates folder that contains ARM templates. The deployment process documented here uses one of the ARM templates to provision required resources for the bot in Azure by using the Azure CLI.

Note

With the release of Bot Framework SDK 4.3, we have deprecated the use of a .bot file. Instead, we use a configuration file, such as an appsettings.json or .env file, to manage bot resources. For information on migrating settings from the .bot file to a configuration file, see managing bot resources.
Both .NET Core 2.1 and .NET Core 3.1 versions of the C# templates are available. When creating new bots in Visual Studio 2019, you should use the .NET Core 3.1 templates.
The current bot samples use .NET Core 3.1 templates. You can find the samples that use .NET Core 2.1 templates in the 4.7-archive branch of the BotBuilder-Samples repository. For information about deploying .NET Core 3.1 bots to Azure, see Deploy your bot.

Bot ready to deploy

This article assumes that you have a bot ready to be deployed. If you are deploying a C# bot make sure that it has been built in Release mode.

For information on how to create a simple echo bot, see the quick start C# sample, JavaScript sample or Python sample. You can also use one of the samples provided in the Bot Framework Samples repository.

1. Login to Azure

Once you've created and tested a bot locally, you can deploy it to Azure. Open a command prompt to log in to the Azure portal.

az login

A browser window will open, allowing you to sign in.

Note

If you deploy your bot to a non-Azure cloud such as US Gov, you need to run az cloud set --name <name-of-cloud> before az login, where <name-of-cloud> is the name of a registered cloud, such as AzureUSGovernment. If you want to go back to public cloud, you can run az cloud set --name AzureCloud.

2. Set the subscription

Set the default subscription to use.

az account set --subscription "<azure-subscription>"

If you are not sure which subscription to use for deploying the bot, you can view the list of subscriptions for your account by using az account list command.

3. Create the application registration

In this step you create an Azure Active Directory application, which will allow:

  • The user to interact with the bot via a set of channels such as Web Chat.
  • The definition of OAuth Connection Settings to authenticate a user and to create a token used by the bot to access protected resources on behalf of the user.

To create an Azure Active Directory application, execute the following command:

az ad app create --display-name "displayName" --password "AtLeastSixteenCharacters_0" --available-to-other-tenants
Option Description
display-name The display name of the application. It is listed in the Azure portal in the general resources list and in the resource group it belongs.
password The password, also known as client secret, for the application. This is a password you create for this resource. It must be at least 16 characters long, contain at least 1 upper or lower case alphabetical character, and contain at least 1 special character.
available-to-other-tenants Indicates that the application can be used from any Azure AD tenant. Set this to enable your bot to work with the Azure Bot Service channels.

The above command outputs JSON with the key appId, copy and save it. You are going to use this appId and the password you entered in the ARM deployment step, to assign values to the appId and the appSecret parameters, respectively.

4. Create the bot application service

When creating the bot application service, you can deploy your bot in a new or in an existing resource group. Choose the option that works best for you.

Important

Python bots cannot be deployed to a resource group that contains Windows services/bots. Multiple Python bots can be deployed to the same resource group, but create other services (LUIS, QnA, etc.) in another resource group.

Make sure that you have the correct path to your bot project ARM deployment templates directory DeploymentTemplates, you need it to assign the value to template-file.

Deploy via ARM template (with new Resource Group)

In this step, you create a bot application service which sets the deployment stage for the bot. You use an ARM template, a new service plan and a new resource group.

From the resulting JSON output, copy the numeric value of the id field to use as the value for the registration subscription id in the next step.

Note

This step can take a few minutes to complete.

az deployment create --template-file "<path-to-template-with-new-rg.json" --location <region-location-name> --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" botSku=F0 newAppServicePlanName="<new-service-plan-name>" newWebAppName="<bot-app-service-name>" groupName="<new-group-name>" groupLocation="<region-location-name>" newAppServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"
Option Description
name The display name to use for your bot channels registration. Default is the value of the botId parameter.
template-file The path to the ARM template. Usually, the template-with-new-rg.json file is provided in the deploymentTemplates folder of the bot project. This is a path to an existing template file. It can be an absolute path, or relative to the current directory. All bot templates generate ARM template files.
location Location. Values from: az account list-locations. You can configure the default location using az configure --defaults location=<location>.
parameters Deployment parameters, provided as a list of key=value pairs. Enter the following parameter values:
  • appId - The app id value generated by the previous step.
  • appSecret - The password you provided in the previous step.
  • botId - A name for the Bot Channels Registration resource to create. It must be globally unique. It is used as the immutable bot ID. It is also used as the default display name, which is mutable.
  • botSku - The pricing tier; it can be F0 (Free) or S1 (Standard).
  • newAppServicePlanName - The name of the new application service plan.
  • newWebAppName - A name for the bot application service.
  • groupName - A name for the new resource group.
  • groupLocation - The location of the Azure resource group.
  • newAppServicePlanLocation - The location of the application service plan.

Deploy via ARM template (with existing Resource Group)

In this step, you create a bot application service which sets the deployment stage for the bot. When using an existing resource group, you can either use an existing app service plan or create a new one. Steps for both options are listed below.

From the resulting JSON output, copy the value of the id field to use as the value for the registration subscription id in the next step.

Note

This step can take a few minutes to complete.

Option 1: Existing App Service Plan

In this case, we are using an existing App Service Plan, but creating a new Web App and Bot Channels Registration.

This command below sets the bot's ID and display name. The botId parameter should be globally unique and is used as the immutable bot ID. The bot's display name is mutable.

az group deployment create --resource-group "<name-of-resource-group>" --template-file "<path-to-template-with-preexisting-rg.json>" --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" newWebAppName="<bot-app-service-name>" existingAppServicePlan="<name-of-app-service-plan>" appServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"

Option 2: New App Service Plan

In this case, we are creating App Service Plan, Web App, and Bot Channels Registration.

az group deployment create --resource-group "<name-of-resource-group>" --template-file "<path-to-template-with-preexisting-rg.json>" --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" newWebAppName="<bot-app-service-name>" newAppServicePlanName="<name-of-app-service-plan>" appServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"
Option Description
name The display name to use for your bot channels registration. Default is the value of the botId parameter.
resource-group Name of the azure resource group.
template-file The path to the ARM template. Usually, the template-with-preexisting-rg.json file is provided in the deploymentTemplates folder of the project. This is a path to an existing template file. It can be an absolute path, or relative to the current directory. All bot templates generate ARM template files.
location Location. Values from: az account list-locations. You can configure the default location using az configure --defaults location=<location>.
parameters Deployment parameters, provided as a list of key=value pairs. Enter the following parameter values:
  • appId - The app id value generated by the previous step.
  • appSecret - The password you provided in the previous step.
  • botId - A name for the Bot Channels Registration resource to create. It must be globally unique. It is used as the immutable bot ID. It is also used as the default display name, which is mutable.
  • newWebAppName - A name for the bot application service.
  • newAppServicePlanName - A name for the application service plan resource to create.
  • newAppServicePlanLocation - The location of the application service plan.

5. Prepare your code for deployment

5.1 Retrieve or create necessary IIS/Kudu files

You need to prepare your project files before you can deploy your C#, Javascript, or Typescript bot. If you are deploying a Python bot you can skip this step and continue to step 5.2.

az bot prepare-deploy --lang Csharp --code-dir "." --proj-file-path "MyBot.csproj"

You must provide the path to the .csproj file relative to --code-dir. This can be performed via the --proj-file-path argument. The command would resolve --code-dir and --proj-file-path to "./MyBot.csproj".

This command generates a .deployment file in your bot project folder.

5.2 Zip up the code directory manually

When using the non-configured zip deploy API to deploy your bot's code, Web App/Kudu's behavior is as follows:

Kudu assumes by default that deployments from zip files are ready to run and do not require additional build steps during deployment, such as npm install or dotnet restore/dotnet publish.

It is important to include your built code with all necessary dependencies in the zip file being deployed, otherwise your bot will not work as intended.

Important

Before zipping your project files, make sure that you are in the project folder.

  • For C# bots, it is the folder that has the .csproj file.
  • For JavaScript bots, it is the folder that has the app.js or index.js file.
  • For TypeScript bots, it is the folder that includes the src folder (where the bot.ts and index.ts files are).
  • For Python bots, it is the folder that has the app.py file.

Within the project folder, select all the files and folders you want included in your zip file before running the command to create the zip file, this will create a single zip file containing all selected files and folders. If your root folder location is incorrect, the bot will fail to run in the Azure portal.

Deploy code to Azure

At this point we are ready to deploy the code to the Azure Web App. Run the following command from the command line to perform deployment using the kudu zip push deployment for a web app.

az webapp deployment source config-zip --resource-group "<resource-group-name>" --name "<name-of-web-app>" --src <project-zip-path>
Option Description
resource-group The name of the Azure resource group that contains your bot. (This will be the resource group you used or created when creating the app registration for your bot.)
name Name of the Web App you used earlier.
src The path to the zipped project file you created.

Note

This step can take a few minutes to complete. Also it can take a few more minutes between when the deployment finishes and when your bot is available to test.

Test in Web Chat

  1. In your browser, navigate to the Azure portal.
  2. In the left panel, click Resource groups.
  3. In the right panel, search for your group.
  4. Click on your group name.
  5. Click the link of your Bot Channels Registration.
  6. In the Bot Channels Registration panel, click Test in Web Chat. Alternatively, in the right panel, click the Test box.

For more information about bot channels registration, see Register a bot with Bot Service.

Additional information

Deploying your bot to Azure will involve paying for the services you use. The billing and cost management article helps you understand Azure billing, monitor usage and costs, and manage your account and subscriptions.

Next steps