Analytics Vidhya
Published in

Analytics Vidhya

Build your own WeatherBot with Botframework + Openweathermap + Adaptive cards UI

First interaction of weatherbot

Prerequisites

  • .NET Core SDK version 2.1
  • Visual Studio 2017/ visual studio code
  • Bot Framework V4
  • OpenWeathermap API
  • Bot framework Emulator (testing)
  • Adaptive cards for UI

Step 1:- Download the .NET Core SDK and install in your system.

# determine dotnet version
dotnet --version

Step 2:- OpenWeatherMap Account setup

Step 3:- Create a project

  • Clone the repository
git clone https://github.com/zayedrais/WeatherBot.git
  • In a terminal, navigate to WeatherBot/Bots/WeatherBot.cs
  • The bot first welcome interaction will be initiated on added new member of user method called OnMembersAddedAsync.
  • Here using a Hero card for the welcome contents, the below highlighted code is for Hero card.
protected override async Task OnMembersAddedAsync
(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity>
turnContext, CancellationToken cancellationToken)
{
IConversationUpdateActivity iConversationUpdated = turnContext.Activity as IConversationUpdateActivity;
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
var card = new HeroCard
{
Title = "Welcome to WeatherBot",
Text = @"Type the city name for get the weather report",
Images = new List<CardImage>() { new CardImage(ImageToBase64(_images[0])) },
};

await turnContext.SendActivityAsync(MessageFactory.Attachment
(card.ToAttachment()), cancellationToken);
}
}
}
  • Just replaced the API key into OpenwetherMapAPI place.
  • The following things are doing inside this method:-
  • calling the weathermap API
  • Select and update the image for template.
  • Select and update the current data of Json template for UI.
  • Send the updated Json template for showing the weather reports.
protected override async Task OnMessageActivityAsync(ITurnContext
<IMessageActivity> turnContext, CancellationToken
cancellationToken)
{
var client = new OpenWeatherMapClient("OpenwetherMapAPI");
var CloudImage = "http://messagecardplayground.azurewebsites.net/assets/Mostly%20Cloudy-Square.png";
var DizzleImage = "http://messagecardplayground.azurewebsites.net/assets/Drizzle-Square.png";var rainImage = "https://raw.githubusercontent.com/zayedrais/WeatherBot/master/Resources/rain.png";var stormImage = "https://raw.githubusercontent.com/zayedrais/WeatherBot/master/Resources/storm.png";var sunImage = "https://raw.githubusercontent.com/zayedrais/WeatherBot/master/Resources/sun.png";var currentWeather = await client.CurrentWeather.GetByName(turnContext.Activity.Text);var search =await client.Search.GetByName("Chennai");var forcast = await client.Forecast.GetByName("Chennai");var curtTemp = currentWeather.Temperature.Value - 273.15;var MaxTemp = currentWeather.Temperature.Max -273.15;var MinTemp = currentWeather.Temperature.Min -273.15;var updateCard = readFileforUpdate_jobj(_cards[0]);JToken cityName = updateCard.SelectToken("body[0].text");JToken tdyDate = updateCard.SelectToken("body[1].text");JToken curTemp = updateCard.SelectToken("body[2].columns[1].items[0].text");JToken maxTem = updateCard.SelectToken("body[2].columns[3].items[0].text");JToken minTem = updateCard.SelectToken("body[2].columns[3].items[1].text");JToken weatherImageUrl = updateCard.SelectToken("body[2].columns[0].items[0].url");cityName.Replace(currentWeather.City.Name);curTemp.Replace(curtTemp.ToString("N0"));tdyDate.Replace(DateTime.Now.ToString("dddd, dd MMMM yyyy"));maxTem.Replace("Max" +" "+MaxTemp.ToString("N0"));minTem.Replace("Min" + " "+MinTemp.ToString("N0"));var n = currentWeather.Clouds.Name;if(n=="overcast clouds")
{
weatherImageUrl.Replace(rainImage);
}
else if (n.Contains("clouds"))
{
weatherImageUrl.Replace(CloudImage);
}
else if (n.Contains("sky"))
{
weatherImageUrl.Replace(sunImage);
}
else if (n.Contains("rain"))
{
weatherImageUrl.Replace(rainImage);
}
else if(n.Contains("storm") || n.Contains("thunder"))
{
weatherImageUrl.Replace(stormImage);
}
var updateWeatherTem = UpdateAdaptivecardAttachment(updateCard);
await turnContext.SendActivityAsync(MessageFactory.Attachment
(updateWeatherTem), cancellationToken):
}

Run the project:-

  • Run the bot from a terminal or from Visual Studio, choose option A or B.
  • A) From a terminal
# run the bot 
dotnet run
  • B) Or from Visual Studio
  • Launch Visual Studio
  • File -> Open -> Project/Solution
  • Navigate to WeatherBot/Bots folder
  • Select WeatherBot.csproj file
  • Press F5 to run the project

Step 4:-Testing the bot using Bot Framework Emulator

  • Install the Bot Framework Emulator version 4.3.0 or greater from here

Connect to the bot using Bot Framework Emulator

The final look like as below image:-

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Zayed Rais

Learn & share the Insight of new techie skills #ML #AI #Cloud #DataScience in real world. connect me on zayedrais@gmail.com #blogger