Power Virtual Agents: Build a Weather Bot using Azure Maps

Introduction

Power Virtual Agents helps us create intelligent conversational chatbots 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 inquiry 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

Graphical user interface, text, application, chat or text message

Description automatically generated

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.

Graphical user interface, text, application, Word

Description automatically generated

We have named the bot as Weather Bot and to define the phrases that will initiate the conversation, click on trigger phrases.

Graphical user interface, application

Description automatically generated

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, the better the chances of bot understanding the trigger condition.

Graphical user interface, text, application, email

Description automatically generated

Let’s add a welcome message to the Authoring Canvas

Graphical user interface, application, Teams

Description automatically generated

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 a parameter to involve azure maps weather endpoint. Click on Create a flow which will open Power Automate in the adjacent window where we can define the flow logic.

Graphical user interface, application

Description automatically generated

Create the Power Automate

We will add the input parameter to accept the zipcode from Power virtual agent calling node

Graphical user interface, application

Description automatically generated

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, let’s create an instance of the 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.

Graphical user interface, application

Description automatically generated

Click on create to provision a new azure maps account.

Graphical user interface, text, application

Description automatically generated

Provide the details and click on create to provision the azure maps account.

Graphical user interface, text, application, email

Description automatically generated

We have selected the Gen1 S0 pricing tier as we are doing a dev Proof of Concept.

Graphical user interface, text, application, email

Description automatically generated

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.

Graphical user interface, text, application, email

Description automatically generated

Heading back to Power Automate, let’s add an HTTP call to the Address Azure Maps API to fetch the equivalent latitude and longitude of the zip code.

Graphical user interface, application

Description automatically generated

So as to display the weather details in the Power Virtual Agent as a table, we will be using markdown 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

Graphical user interface, text, application

Description automatically generated

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’]

Graphical user interface, text, application, email

Description automatically generated

Now let’s 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 are listed here

Graphical user interface, application, email

Description automatically generated

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’]}|
Graphical user interface, text

Description automatically generated

The final output is returned to Power Virtual Agent

Graphical user interface, text, application, email, Teams

Description automatically generated

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

Graphical user interface, application

Description automatically generated

The output of the power automate will be stored in varWeatherCondition variable and shown 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.

Graphical user interface, application

Description automatically generated

Test the bot

Now let’s 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.

Graphical user interface, text, application

Description automatically generated

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 MarkDown language. We can extend the bot by publishing and deploying it into any of the channels like Teams, Slack, Facebook, custom website, etc;

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles