Skip to content

Commit

Permalink
take 3
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuwandy committed Dec 15, 2017
1 parent fd39a80 commit b528709
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
9 changes: 5 additions & 4 deletions WeatherBot/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions WeatherBot/Weather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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
{
Expand All @@ -30,10 +32,11 @@ public static async Task<string> GetCurrentWeatherByCityName(string cityName)
public static async Task<string> 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
{
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions WeatherBot/WeatherView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Microsoft.Bot.Builder.Templates;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace WeatherBot
{
Expand Down Expand Up @@ -311,6 +313,25 @@ public class WeatherView : TemplateRendererMiddleware
}
]
}";

static readonly Dictionary<string, string> LargeWeatherImages = new Dictionary<string, string>()
{
{ "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<string, string> SmallWeatherImages = new Dictionary<string, string>()
{
{ "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))
{
}
Expand All @@ -319,6 +340,18 @@ public static IMessageActivity GetWeatherCard(BotContext context, string city, b
{
IMessageActivity activity = context.Request.CreateReply();
AdaptiveCard card = JsonConvert.DeserializeObject<AdaptiveCard>(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;
}
Expand Down

0 comments on commit b528709

Please sign in to comment.