Let's test the Python version of the Bot Framework SDK by building a simple bot that returns a short weather description whenever you enter a city. Additional steps include Azure deployment and Slack integration.
๐บ A video-version of this tutorial can be found on YouTube.
- Use Python 3.7, because Oryx (the build system) uses Python 3.7.9
- Sign up and get an API key on OpenWeatherMap, store that key in a .env file at the workspace root
- Install Bot Framework Emulator v4
- Move to the
weather_bot
folder - Install all requirements:
python -m pip install -r requirements.txt
- Check that the
cookiecutter
package was installed correctly by runningcookiecutter --help
- Replace the placeholder OpenWeatherMap API key value in the .env file
- Run
python app.py
and remember the URL at which it's running - Start the Bot Framework Emulator
- If no option to open a bot shows up, go to "File > Open Bot"
- Enter the bot URL and append
/api/messages
, should be something along the lines ofhttp://localhost:3978/api/messages
. - Click "Connect" and you're set:
These steps will deploy the bot to a new resource group. If you want to deploy to an existing resource group, refer to the documentation.
- Have the Azure CLI installed
- Login to Azure:
az login
- Save your Azure subscription id from the output of the login command, or use
az account list
- Set the subscription to use for deploying the bot:
az account set --subscription "<azure-subscription-id>"
- Create an application registration:
az ad app create --display-name "displayName" --password "AtLeastSixteenCharacters_0" --available-to-other-tenants
- ๐ Save the password and the
appId
(from the JSON output) somewhere - Run the first big scary command that will set up a bot application service on Azure for you:
az deployment sub create --template-file "<path/to/deploymentTemplates/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>"
Notes:
- F0 is the free tier plan
- Path to the template file: Absolute or relative to current directory
- Location: See
az account list-locations
and use the value of thename
key (for examplewestus2
)
While the setup command is running:
- Create a zip file of your bot: Select all the files under the folder that contains the
app.py
file and package them. This also includes the .env file. Oryx will read therequirements.txt
file and install the appropriate dependencies. - Run the not-so-big but still scary deployment command:
az webapp deployment source config-zip --resource-group "<resource-group-name>" --name "<name-of-web-app>" --src "<project-zip-path>"
- If you want to check the deployment logs, run
az webapp log deployment show -n <name-of-web-app> -g <resource-group-name>
and click on the URL in the resulting JSON
- Find the Bot Channels Registration entry for your bot
- In the side blade, select "Test using Web Chat" under the Settings category
- The interface is similar to the Bot Framework Emulator, which means that if the bot was deployed correctly, it should greet you, and reply to you whenever you enter a city:
The Microsoft Docs tutorial can be found here.
There are 2 ways to connect your bot to Slack:
- Via the Azure Bot Service Portal (no code)
- With a Slack adapter (doesn't require tinkering with the Azure Portal, but can only be written in C# or JS)
We will go with the Azure Bot Service Portal solution.
Make sure you have permissions to deploy an app to the Slack workspace of your choice, check out this Slack help center article if you or your workspace owners need instructions on how to do so.
The steps pretty much follow the tutorial linked above:
- Create a Slack app on https://api.slack.com/apps
- Add https://slack.botframework.com as a redirect URL under "OAuth and Permissions"
- Go to "Event Subscriptions" and enable event subscription
- Add
https://slack.botframework.com/api/events/<your-bot-handle>
as the request URL and replace with your bot handle, which can be found in your bot profile in the Azure Portal (Bot Channels Registration > Bot Profile) - Subscribe to bot events, the only ones we need here are
message.channels
andmessage.im
- Remember to click on "Save Changes" โ
- Add
- Get your Slack app credentials under "Basic Information" > "App Credentials"
- Go to the Azure Portal, create a Slack channel (might take a couple of retries if the page times out), paste the credentials
- It will open a pop-up window, accept your fate and authorize everything
- Bot is now in your workspace, you and anybody else in this workspace can talk to it in "Apps":
Note that this is for development purposes, and doesn't cover distributing the app in the Slack App Directory.