Deploy a Basic Python Bot to Azure | Microsoft Bot Framework

Deploy a basic python bot created using Bot Framework SDK to Azure. We will deploy an Echo that we created in our last blog using Python.

Prerequisites

  1. Valid Azure Subscription
  2. Python Basic Bot created locally
  3. Azure CLI (Latest version)

Prepare the Bot for Deployment

When we created the Python bot locally in our last blog using the Cookiecutter template, it contained the deployment templates as well along with the bot source code. We will make use of these templates for deployment. These are the ARM templates used for managing Azure resources.

Check the Azure CLI version by running the below command in any command-line tool. I will be showing the demo in Windows OS and will be working on Command Prompt.

az --version
check Azure CLI version for Python bot deployment

Make sure the Azure CLI version is above 2.2.0, else you might face some errors. We will log in to Azure using the Azure CLI by running the below command.

az login

This command will launch a login page in the browser prompting you to log in to Azure. Once the log-in is successful, your Azure subscription details will be shown in the command prompt/command line.

Login to Azure using the CLI, the subscription details are shown on success login

You might have multiple Azure subscriptions connected to your account. Set the default subscription using the below command.

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

The azure-subscription-id value can be found from the above screenshot having the id parameter. You can also find this value from the Azure Portal -> Subscription -> Subscription ID.

To connect the bot to various channels, we need an Azure application registration created. Use the below command to create an app in Azure AD.

az ad app create --display-name "displayName" --password "AtLeastSixteenCharacters_0" --available-to-other-tenants

The above command has the following parameters –

  1. display-name: Name of your app that will be listed in Azure AD.
  2. password: This is a client secret. It must be at least sixteen characters long, contain at least one upper- or lower-case alphabetical character, and contain at least one special character.
  3. available-to-other-tenants: Enable it to get this app available to other tenants and work with Azure Bot Channels.
created Azure application registration

Make a note of appId that is generated and the client secret/password you provided when creating the app registration. This is required for deployment.

Deploy Python Bot using Azure Resource Manager (ARM) Template [Create Azure Resources]

If you see the deploymentTemplates folder in your project directory, there you will find two templates. The first one is used for deploying to a new resource group. The second one is for deploying to an existing resource group.

deployment templates folder having two ARM templates

An ARM template is a JSON file that declaratively defines one or more Azure resources and that defines dependencies between the deployed resources.

We will make use of 1st template that creates the new resource group, and new app service plan. Use the below command to start the process.

az deployment sub 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>"

The above command has the following options –

  • name: Name of the deployment. I will use Python-Echo-Bot-Deployment.
  • template-file: Path of the template-with-new-rg.json file present inside the deploymentTemplates folder. My path is D:\Chatbot\echo-bot\deploymentTemplates\template-with-new-rg.json.
  • location: Region where you want to deploy your bot. You can get the list of location using the below command.
az account list-locations
list of all Azure locations

The red arrows are pointed to the location short name that is to be provided in the deployment command. I will use eastus as the location.

  • parameters: These are the deployment parameters having below values –
  1. appId: The appId that was generated from the app registration output.
  2. appSecret: The value you provided to create the app registration.
  3. botId: Global unique value. I will use python-echo-bot-jd-bots.
  4. botSku: The pricing tier; it can be F0 (Free) or S1 (Standard). I will use the free pricing tier.
  5. newAppServicePlanName: The name of the new application service plan. I will use python-echo-bot-asp.
  6. newWebAppName: A name for the bot application service. I will use python-echo-bot-dev-app.
  7. groupName: A name for the new resource group. I will use PythonBot-RG.
  8. groupLocation: The location of the Azure resource group. I will use the same location that I used in deployment option, i.e., eastus.
  9. newAppServicePlanLocation: The location of the application service plan. I will use eastus as the location.

Below is my final command with all the values that I used for deployment.

az deployment sub create --template-file "D:\Chatbot\echo-bot\deploymentTemplates\template-with-new-rg.json" --location eastus --parameters appId="808c125e-********" appSecret="*********" botId="python-echo-bot-jd-bots" botSku=F0 newAppServicePlanName="python-echo-bot-asp" newWebAppName="python-echo-bot-dev-app" groupName="PythonBot-RG" groupLocation="eastus" newAppServicePlanLocation="eastus" --name "Python-Echo-Bot-Deployment"

You can check the Azure portal and see if the resource group is created along with 3 resources in it.

resources created inside the resource group using ARM templates

Prepare your Bot source Code for Deployment

Till now, we have created all the resources that are required to host our bot application. Now, we will package the source code into a zip file and deploy it.

The folder you have to zip is your root project folder where app.py file is present. Use the below command to create a zip file. Make sure you are in the root directory of your project. My root directory is D:\Chatbot\echo-bot.

Compress-Archive -Path * -DestinationPath pythonEchoBot.zip
create a zip folder for your python echo bot

Use the PowerShell to run this command, because the command prompt will not recognize the command you run. The command created the zip file inside the root directory of my project.

Zip folder created for Python Basic Bot

Deploy the Python Basic Echo Bot to Azure

Now, let us deploy our code to Azure using the below command.

az webapp deployment source config-zip --resource-group "<resource-group-name>" --name "<name-of-web-app>" --src "<project-zip-path>"

My command looks like this as shown below.

az webapp deployment source config-zip --resource-group "PythonBot-RG" --name "python-echo-bot-dev-app" --src "D:\Chatbot\echo-bot\pythonEchoBot.zip"

This will take some time for the deployment to complete and few more minutes before you can test your bot.

Python basic echo bot deployment to Azure completed using Azure Bot Service

Test Python Basic Echo Bot in Web Chat

Log in to Azure Portal. Click on Resource Groups from the home page.

Click on Resource Groups from the Azure Home Page

Click on the resource group that was created using the deployment template.

click on the PythonBot-RG resource group

Open the resource of type Azure Bot.

Open the Azure Bot Resource

Click on Test in Web Chat.

Test in Web Chat

Wait for the Welcome message to come and then start chatting with the bot. It took around 5-10 minutes for the bot to work after deployment.

Testing Python Basic Echo bot created using Microsoft Bot Framework

For the regular deployments, you don’t have to follow all the steps as above. Just start from Prepare your Bot Source Code for Deployment.

Thank you All!!! Hope you find this useful.


Up ↑

%d bloggers like this: