Power Virtual Agents: Build a Weather Bot using Azure Maps
Introduction
Power Virtual Agents helps us create intelligent conversational chat bots without any code implementation. With these bots we can engage with customers and employees in multiple languages across websites, mobile apps, Facebook, Microsoft Teams, or any channel supported by the Azure Bot Framework
In this article we will see how to create a basic Weather Bot that picks weather details from Microsoft Azure Maps.
Business Use Case
We have a requirement where we need to have an automated weather enquiry system that will pick the weather information for a specific zip code location.
To achieve this, we will use Azure Maps which provides developers with powerful geospatial capabilities. It provides various endpoints to fetch data related to Maps, Search, Routing, Traffic, Weather, Time Zones, Geolocation. The official documentation can be found here.
Implementation
To create a bot that consumes Azure Maps services, we will go ahead to https://web.powerva.microsoft.com/ and create a bot by selecting the top right option
Which will provide us with the window to name the Bot and the language to be used. Once the bot is created, we will create a blank topic inside the bot that will define the way the conversation will be triggered and driven with the end user.
We have named the bot as Weather Bot and to define the phrases that will initiate the conversation, click on trigger phrases.
The trigger phrases will help in defining the words and phrases that will start the conversation with the bot. We have defined 7 such phrases and the more different and related words we add to it, better the chances of bot understanding the trigger condition.
Let’s add a welcome message to the Authoring Canvas
Followed by this we will request more information about the zip code at which the user needs to know the weather conditions using Ask a Question conversational node. We will save the data in the varZipcode variable. Let’s create a Power Automate to which we will pass the zip code as parameter to involve azure maps weather end point. Click on Create a flow which will open Power Automate in the adjacent window where we can define the flow logic.
Create the Power Automate
We will add the input parameter to accept the zipcode from Power virtual agent calling node
To use the weather api, we will need the latitude and longitude of the Zip Code provided by the user. To get the latitude and longitude details, we will initially use the Address API of azure maps. The API URL is https://atlas.microsoft.com/search/address/json
It takes the input query parameters :
query |
Zip code variable |
api-version |
1 |
subscription-key |
<Key obtained from the Azure Maps instance registration> |
Before proceeding, lets create an instance of azure maps account by heading over to azure. Search for Azure Maps in the top search bar which will list Azure maps in the drop down and select it.
Click on create to provision a new azure maps account.
Provide the details and click on create to provision the azure maps account.
We have selected the Gen1 S0 pricing tier as we are doing a dev Proof of Concept.
After the successful creation of the Azure Maps account. head over to the overview section and fetch the subscription id which we will be using in the Power Automate.
Heading back to Power Automate, lets add an HTTP call to the Address Azure Maps API to fetch the equivalent latitude and longitude of the zip code.
So as to display the weather details in the Power Virtual Agent as a table, we will be using mark down language and we will initialize a variable that will hold the header which is defined using the syntax:
|Forecast|Condition|
|---------- |--------------|
To add a table, we use three or more hyphens (---) to create each column’s header and use pipes (|) to separate each column. A detailed overview of mark down language can be seen here
We will now declare 2 variables to host the Latitude and Longitude values with the below expressions
Latitude Expression |
body('HTTP_-_Convert_Zip_Code_to_Latitude_and_Longitude_with_Azure_Map_API')['results'][0]['position']['lat'] |
Longitude Expression |
body('HTTP_-_Convert_Zip_Code_to_Latitude_and_Longitude_with_Azure_Map_API')['results'][0]['position']['lon'] |
Now lets add the Azure Maps Weather API that gets the daily forecast details by issuing a Get Request to the URL : https://atlas.microsoft.com/weather/forecast/daily/json
We will be using the below query parameters :
query |
<latitude variable>,<longitude variable> |
Api-version |
1.1 |
Subscription-key |
<Azure Maps Account Subscription Key> |
duration |
1(In S0 Pricing tier, We can request daily forecast for the next 1, 5, 10, and 15 days) |
The details of the API is listed here
The output the of the HTTP call will be a JSON which we will append to the weather table variable in the mark down format by separating each column values with a Pipe Symbol. The expressions used to spot the required weather data from the returned JSON is described below :
|Summary|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['summary']['phrase']}|
|Min Temperature|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['temperature']['minimum']['value']} Celcius|
|Max Temperature|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['temperature']['maximum']['value']} Celcius|
|Hours of Sun|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['hoursOfSun']}|
|Air Quality|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['airAndPollen'][0]['category']}|
|Day Condition|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['day']['shortPhrase']}|
|Night Condition|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['night']['shortPhrase']}|
|Hours of Day Time Rain|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['day']['hoursOfRain']}|
|Hours of Night Time Rain|@{body('HTTP_-_Get_Weather_Data_using_Azure_Map_Weather_API')['forecasts'][0]['night']['hoursOfRain']}|
The final output is returned to Power Virtual Agent
Calling Power Automate from PVA
We will add the Call an action node which gives us the list of power automate available for calling. We will select the recently created flow and pass the varZipCode variable as the input parameter
The output of the power automate will be stored in varWeatherCondition variable and show as a message to the end user. We will also add the end conversation node with a survey to close the conversation loop. In case of further continuing the conversation, we can ask further question node to expand the chat conversation possibilities.
Test the bot
Now lets test the bot using a trigger word Weather Forecast which will initiate the conversation with the bot that will convert the zip code into corresponding latitude and longitude using Azure Maps Address API and feed it into the Azure Maps Weather Daily Forecast API to get the forecast conditions for the next day.
Summary
Thus, we saw how we can use Power Virtual Agent to call Power Automate which in turn leverages Azure Maps API to fetch the weather conditions and show it as a table in Power Virtual Chat Bot using Mark Down language. We can extend the bot by publishing and deploying it into any of the channels like Teams, Slack, Facebook, custom website etc;