diff --git a/WeatherBot/Controllers/MessagesController.cs b/WeatherBot/Controllers/MessagesController.cs index dfffbfe..e1899e1 100644 --- a/WeatherBot/Controllers/MessagesController.cs +++ b/WeatherBot/Controllers/MessagesController.cs @@ -37,14 +37,15 @@ public MessagesController(IConfiguration configuration) string city = Weather.GetCity(text); if (!string.IsNullOrWhiteSpace(city)) { - if (text.Contains("forecast")) + if (text.Contains("current")) { - //context.Reply(await Weather.GetWeatherForecastByCityName(city)); - context.ReplyWith(WeatherView.FORECAST, city); + context.Reply(await Weather.GetCurrentWeatherByCityName(city)); + context.ReplyWith(WeatherView.CURRENT, city); } else { - context.ReplyWith(WeatherView.CURRENT, city); + context.Reply(await Weather.GetWeatherForecastByCityName(city)); + context.ReplyWith(WeatherView.FORECAST, city); } } else diff --git a/WeatherBot/Weather.cs b/WeatherBot/Weather.cs index 1901831..535b87d 100644 --- a/WeatherBot/Weather.cs +++ b/WeatherBot/Weather.cs @@ -6,20 +6,22 @@ namespace WeatherBot { public static class Weather { - const string WeatherApiBaseUrl = "http://api.openweathermap.org/data/2.5/{0}?{1}={2}&units=imperial&APPID=a0f75f6b2f7ce29295822d2862df66a6"; + 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"; + const string Forecast = "forecast/daily"; const string CityNameParam = "q"; - + const string NumForecastDaysParam = "&cnt=3"; + static HttpClient client = new HttpClient(); public static async Task GetCurrentWeatherByCityName(string cityName) { HttpResponseMessage response = await client.GetAsync( - String.Format(WeatherApiBaseUrl, CurrentWeather, CityNameParam, cityName)); + String.Format(WeatherApiBaseUrl, CurrentWeather, cityName, String.Empty)); if (response.IsSuccessStatusCode) { - return await response.Content.ReadAsStringAsync(); + string jsonResponse = await response.Content.ReadAsStringAsync(); + return jsonResponse; } else { @@ -30,10 +32,11 @@ public static async Task GetCurrentWeatherByCityName(string cityName) public static async Task GetWeatherForecastByCityName(string cityName) { HttpResponseMessage response = await client.GetAsync( - String.Format(WeatherApiBaseUrl, Forecast, CityNameParam, cityName)); + String.Format(WeatherApiBaseUrl, Forecast, cityName, NumForecastDaysParam)); if (response.IsSuccessStatusCode) { - return await response.Content.ReadAsStringAsync(); + string jsonResponse = await response.Content.ReadAsStringAsync(); + return jsonResponse; } else { @@ -61,7 +64,8 @@ public static string GetCity(string text) if (idx > 0) { - return text.Substring(idx + len); + string city = text.Substring(idx + len); + return Char.ToUpperInvariant(city[0]) + city.Substring(1); } return String.Empty; diff --git a/WeatherBot/WeatherView.cs b/WeatherBot/WeatherView.cs index 7b435f8..e7510a5 100644 --- a/WeatherBot/WeatherView.cs +++ b/WeatherBot/WeatherView.cs @@ -3,6 +3,8 @@ using Microsoft.Bot.Builder.Templates; using Microsoft.Bot.Connector; using Newtonsoft.Json; +using System; +using System.Collections.Generic; namespace WeatherBot { @@ -311,6 +313,25 @@ public class WeatherView : TemplateRendererMiddleware } ] }"; + + static readonly Dictionary LargeWeatherImages = new Dictionary() + { + { "Cloudy", "https://raw.githubusercontent.com/tsuwandy/weather/master/Weather-Cloudy.png" }, + { "CloudyWithRain", "https://raw.githubusercontent.com/tsuwandy/weather/master/Weather-Cloudy_w_Rain.png" }, + { "Rain", "https://raw.githubusercontent.com/tsuwandy/weather/master/Weather-Rain.png" }, + { "Sunny", "https://raw.githubusercontent.com/tsuwandy/weather/master/Weather-Sunny.png" }, + { "Snow", "https://raw.githubusercontent.com/tsuwandy/weather/master/Weather-Snow.png" } + }; + + static readonly Dictionary SmallWeatherImages = new Dictionary() + { + { "Cloudy", "https://raw.githubusercontent.com/tsuwandy/weather/master/cloudy.png" }, + { "CloudyWithRain", "https://raw.githubusercontent.com/tsuwandy/weather/master/cloudy_w_rain.png" }, + { "Rain", "https://raw.githubusercontent.com/tsuwandy/weather/master/rain_color.png" }, + { "Sunny", "https://raw.githubusercontent.com/tsuwandy/weather/master/sunny.png" }, + { "Snow", "https://raw.githubusercontent.com/tsuwandy/weather/master/snow_color.png" } + }; + public WeatherView() : base(new DictionaryRenderer(Templates)) { } @@ -319,6 +340,18 @@ public static IMessageActivity GetWeatherCard(BotContext context, string city, b { IMessageActivity activity = context.Request.CreateReply(); AdaptiveCard card = JsonConvert.DeserializeObject(isForecast ? TemplateForecast : TemplateCurrent); + DateTime now = DateTime.Now; + + // update header + TextBlock dateHeader = (TextBlock)(((ColumnSet)card.Body[0]).Columns[0].Items[0]); + dateHeader.Text = now.ToString("ddd MMM dd"); + + TextBlock cityHeader = (TextBlock)(((ColumnSet)card.Body[0]).Columns[1].Items[0]); + cityHeader.Text = city; + + // update updated time + TextBlock updated = (TextBlock)card.Body[card.Body.Count - 1]; + updated.Text = now.ToShortTimeString(); activity.Attachments.Add(new Attachment(AdaptiveCard.ContentType, content: card)); return activity; }