Hi there, I'm Kim-Adeline Miguel from the Python team
at Microsoft and in this video I will demonstrate a
few Azure bot service capabilities.
By using a small Python,
bot that takes a city as an input and returns
a short weather description for it,
I will show you how to test your
bot locally, how to deploy it to Azure, and how to
add it to a Slack workspace as an application.
First, let's get our local development environment ready.
Make sure you're using Python 3.7 because Oryx, the build
tool on Azure, uses Python 3.7.9.
Then, in order to test the bot locally,
download the latest bot framework emulator release on GitHub.
Also, this bot retrieves weather data from Openweathermap,
so sign up for an API key on openweathermap.org under
current weather data.
Once that's done, clone the bot code at aka.ms/simple-weather-bot
Then create a virtual environment and install all dependencies from
the requirements.txt file in the weather_bot folder
and replace the placeholder API key value in the .env
file with the key you obtain from openweathermap.
Alright, let's dive in and test the
bot locally first. Run the app.py viewer file that's inside
the weather_bot folder and keep an eye on the
URL shown in the terminal output.
Then start the bug framework emulator and in the open
bot window enter the URL from before and append
API/messages to it.
Click on the Connect button and you're set.
The bot should greet you and for every city you
enter,
the bot will fetch weather data and reply to you
with a short description of the current weather.
Next we will deploy the bot to Azure.
All necessary resources will be created through the command line,
so make sure that you have the Azure CLI installed
and of course, that you have an active Azure subscription.
Some of the commands are pretty lengthy,
so have the repo readme nearby for easy copy and pasting.
Alright, in your terminal, log into Azure with the az
login command and save the subscription ID that appears. Set
this subscription to use for the bot with az account
set command where you will need the subscription ID and
after that we need to create an app for the
bot and register it.
This will allow users to interact with the bot in
multiple ways,
for example via web chat or a direct integration with
Teams or Slack or without the tools using a Direct
Line adapter.
Decide on a display name for the bot, choose a
password with at least 16 characters,
including uppercase and lowercase letters,
digits, and special characters, and run the az ad app create
command from the README.
Once the ad registration command returns successfully save the password
and the app ID from the command output,
we will need them for the next step,
which is setting up a bot application service on Azure.
This will set up the deployment stage for the bot
using a template already provided in the code,
and it will also create a new resource group and
a new service at plan for the bot's Web app.
So if you want to reuse existing resources,
the deployment tutorial at the Microsoft Docs covers these use
cases.
But here copy the az deployment sub create command from
the README and replace all the arguments with their appropriate
values. While waiting for this command to finish,
let's prepare the bot code for deployment.
Zip up everything inside the weather bot folder that will
be needed for the bot to work which includes the .env file
Oryx needs the requirements.txt
file to be able to install dependencies
So make sure you bundle it too. Once we're done
with the zip file in the setup command,
it's time to do the thing and deploy the bot
to Azure. Run the az Web deployment command from the
README.
Wait for it to succeed,
and you're done. Test the bot in
the Azure portal by selecting the Bot Registrations general entry
in your Azure resources and select the test in Web
Chat option.
If everything went correctly, you should be able to interact
with the bot.
Alright, now that we have our bot up and running in
Azure,
let's try connecting it to a Slack workspace so that
others can use it.
We are going to set up the bot app on
Slack and copy the credentials over to Azure,
so make sure you have the Slack page for apps
open at api.slack.com/apps and the bot Channels registration page open
on the Azure portal.
First we're going to create an app in Slack.
Pick a descriptive name and select a workspace for the
bot.
We then need to add in a new redirect URL.
So go to the OAuth and permissions section, add
https://slackbotframework.com as a new redirect URL and save it.
After that we need to allow the bot to subscribe to Slack events
Go to the event subscription section and toggle
the event switch at the top. The request URL is
https:slack.botframework.com/api/Events/{your bot handle}.
You can find your bot handle in
the Azure portal
under the bot profile section of the bot channels registration
resource.
Once you've entered your bot handle,
make sure the request URL header has a verified check
mark right next to it and then add user events
for the bot to subscribe to.
So in the case of our weather bot,
we only need the messages.channels and messages.im
events. Remember to click on save changes
at the bottom of the screen when you're done.
We're almost there! Now let's add the Slack app credentials
for our
bot on the Azure side,
first still on Slack set page,
opened the basic Information section and scroll to the content
under app credentials.
Now switch to the Azure Portal tab and go to
the channel section of the bot channels registration resource.
Here click on the Slack icon and paste in the
client ID,
client secret and signing secret from the app credentials section
of the Slack app.
Click on save and after you authorize the app in
the pop up window,
it should appear in your workspace under apps and will
be available to everyone in the workspace.
And with that, we're done.
Thank you for watching this walkthrough.
If you have any questions or comments,
feel free to reach out on the Python at Microsoft
Discord at aka.ms/python-discord
or on Twitter at
@pythonvscode