Skip to content

Commit

Permalink
updated card
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuwandy committed Dec 15, 2017
1 parent b528709 commit f3245ed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
9 changes: 6 additions & 3 deletions WeatherBot/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Bot.Builder.Storage;
using Microsoft.Bot.Connector;
using Microsoft.Extensions.Configuration;
using System.Linq;

namespace WeatherBot.Controllers
{
Expand Down Expand Up @@ -39,12 +40,10 @@ public MessagesController(IConfiguration configuration)
{
if (text.Contains("current"))
{
context.Reply(await Weather.GetCurrentWeatherByCityName(city));
context.ReplyWith(WeatherView.CURRENT, city);
}
else
{
context.Reply(await Weather.GetWeatherForecastByCityName(city));
context.ReplyWith(WeatherView.FORECAST, city);
}
}
Expand All @@ -55,7 +54,11 @@ public MessagesController(IConfiguration configuration)
}
else
{
context.Reply("Type city name to get weather");
var activity = context.Request.AsConversationUpdateActivity();
if (activity.MembersAdded.Where(m => m.Id == activity.Recipient.Id).Any())
{
context.Reply("Hello, type something like 'forecast for seattle'");
}
}
});
}
Expand Down
26 changes: 14 additions & 12 deletions WeatherBot/Weather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ namespace WeatherBot
{
public static class Weather
{
const string WeatherApiBaseUrl = "http://api.openweathermap.org/data/2.5/{0}?q={1}&units=imperial&APPID=a0f75f6b2f7ce29295822d2862df66a6{2}";
const string CurrentWeather = "weather";
const string Forecast = "forecast/daily";
const string CityNameParam = "q";
const string NumForecastDaysParam = "&cnt=3";
const string CurrentConditionApiUrl = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22{0}%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
const string ForecastApiUrl = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22{0}%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

static HttpClient client = new HttpClient();
static string[] Cardinals = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" };

public static async Task<string> GetCurrentWeatherByCityName(string cityName)
public static string DegreesToCardinal(double degrees)
{
HttpResponseMessage response = await client.GetAsync(
String.Format(WeatherApiBaseUrl, CurrentWeather, cityName, String.Empty));
return Cardinals[(int)Math.Round(((double)degrees % 360) / 45)];
}

public static async Task<string> GetCurrentWeatherByCityNameAsync(string cityName)
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(String.Format(ForecastApiUrl, cityName));
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
Expand All @@ -29,10 +31,10 @@ public static async Task<string> GetCurrentWeatherByCityName(string cityName)
}
}

public static async Task<string> GetWeatherForecastByCityName(string cityName)
public static async Task<string> GetWeatherForecastByCityNameAsync(string cityName)
{
HttpResponseMessage response = await client.GetAsync(
String.Format(WeatherApiBaseUrl, Forecast, cityName, NumForecastDaysParam));
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(String.Format(ForecastApiUrl, cityName));
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
Expand Down
71 changes: 63 additions & 8 deletions WeatherBot/WeatherView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WeatherBot
{
Expand All @@ -19,7 +20,7 @@ public class WeatherView : TemplateRendererMiddleware
// Default templates
["default"] = new TemplateIdMap
{
{ CURRENT, (context, city) => GetWeatherCard(context, city, false) },
{ CURRENT, (context, city) => GetWeatherCard(context, city, false) },
{ FORECAST, (context, city) => GetWeatherCard(context, city, true) },
}
};
Expand Down Expand Up @@ -102,12 +103,6 @@ public class WeatherView : TemplateRendererMiddleware
""type"": ""TextBlock"",
""weight"": ""bolder""
},
{
""isSubtle"": true,
""width"": ""small"",
""text"": ""60% chance of rain"",
""type"": ""TextBlock""
},
{
""isSubtle"": true,
""width"": ""small"",
Expand Down Expand Up @@ -336,11 +331,48 @@ public WeatherView() : base(new DictionaryRenderer(Templates))
{
}

public static string GetWeatherImage(bool useSmallImage, string weatherText)
{
Dictionary<string, string> lookup = useSmallImage ? SmallWeatherImages : LargeWeatherImages;
weatherText = weatherText.ToLowerInvariant();
if (weatherText.Contains("cloudy"))
{
return lookup["Cloudy"];
}
else if (weatherText.Contains("rain") ||
weatherText.Contains("showers") ||
weatherText.Contains("thunderstorms"))
{
return lookup["Rain"];
}
else if (weatherText.Contains("snow"))
{
return lookup["Snow"];
}
else
{
return lookup["Sunny"];
}
}

public static IMessageActivity GetWeatherCard(BotContext context, string city, bool isForecast)
{
return Task.Run(() => GetWeatherCardAsync(context, city, isForecast)).Result;
}

public static async Task<IMessageActivity> GetWeatherCardAsync(BotContext context, string city, bool isForecast)
{
IMessageActivity activity = context.Request.CreateReply();
AdaptiveCard card = JsonConvert.DeserializeObject<AdaptiveCard>(isForecast ? TemplateForecast : TemplateCurrent);
DateTime now = DateTime.Now;
string weatherJson = await Weather.GetWeatherForecastByCityNameAsync(city);
dynamic weatherInfo = !String.IsNullOrWhiteSpace(weatherJson) ? JsonConvert.DeserializeObject<dynamic>(weatherJson) : null;

if (weatherInfo == null)
{
activity.Text = $"Could not get weather information for {city}";
return activity;
}

// update header
TextBlock dateHeader = (TextBlock)(((ColumnSet)card.Body[0]).Columns[0].Items[0]);
Expand All @@ -351,7 +383,30 @@ public static IMessageActivity GetWeatherCard(BotContext context, string city, b

// update updated time
TextBlock updated = (TextBlock)card.Body[card.Body.Count - 1];
updated.Text = now.ToShortTimeString();
updated.Text = $"Updated {now.ToShortTimeString()}";

weatherInfo = weatherInfo.query.results.channel;
dynamic wind = weatherInfo.wind;
string cardinalDirection = Weather.DegreesToCardinal(double.Parse(wind.direction.ToString()));

// update wind
TextBlock windInfo = (TextBlock)card.Body[3];
windInfo.Text = $"Winds {wind.speed} mph {cardinalDirection}";

dynamic currentCondition = weatherInfo.item.condition;

// update weather condition text and image
TextBlock conditionSummary = (TextBlock)card.Body[2];
conditionSummary.Text = currentCondition.text;

Image currentWeatherImage = (Image)(((ColumnSet)card.Body[1]).Columns[0].Items[0]);
currentWeatherImage.Url = GetWeatherImage(false, currentCondition.text);

if (isForecast)
{
dynamic forecast = weatherInfo.item.forecast;
}

activity.Attachments.Add(new Attachment(AdaptiveCard.ContentType, content: card));
return activity;
}
Expand Down

0 comments on commit f3245ed

Please sign in to comment.