Skip to content

Commit

Permalink
take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuwandy committed Dec 14, 2017
1 parent 6eb2913 commit 170d371
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 8 deletions.
21 changes: 16 additions & 5 deletions WeatherBot/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Builder.Storage;
using Microsoft.Bot.Connector;
using Microsoft.Extensions.Configuration;

Expand All @@ -24,21 +25,31 @@ public MessagesController(IConfiguration configuration)
// create the activity adapter to send/receive Activity objects
activityAdapter = new BotFrameworkAdapter(appId, appPassword);
bot = new Bot(activityAdapter)
.Use(new MemoryStorage())
.Use(new BotStateManager()) // --- add Bot State Manager to automatically persist and load the context.State.Conversation and context.State.User objects
.Use(new WeatherView())
.OnReceive(async (context) =>
{
// THIS IS YOUR BOT LOGIC
if (context.Request.Type == ActivityTypes.Message)
{
string city = context.Request.Text;
string text = context.Request.Text.ToLower();
string city = Weather.GetCity(text);
if (!string.IsNullOrWhiteSpace(city))
{
string weatherJson = await Weather.GetWeatherByCityName(city);
context.Reply(weatherJson);
//return new ReceiveResponse(true);
if (text.Contains("forecast"))
{
//context.Reply(await Weather.GetWeatherForecastByCityName(city));
context.ReplyWith(WeatherView.FORECAST, city);
}
else
{
context.ReplyWith(WeatherView.CURRENT, city);
}
}
else
{

context.Reply("Please specify city");
}
}
else
Expand Down
45 changes: 42 additions & 3 deletions WeatherBot/Weather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ namespace WeatherBot
{
public static class Weather
{
const string WeatherApiBaseUrl = "http://api.openweathermap.org/data/2.5/weather?{0}&units=imperial&APPID=a0f75f6b2f7ce29295822d2862df66a6";
const string WeatherApiBaseUrl = "http://api.openweathermap.org/data/2.5/{0}?{1}={2}&units=imperial&APPID=a0f75f6b2f7ce29295822d2862df66a6";
const string CurrentWeather = "weather";
const string Forecast = "forecast";
const string CityNameParam = "q";

static HttpClient client = new HttpClient();

public static async Task<string> GetWeatherByCityName(string cityName)
public static async Task<string> GetCurrentWeatherByCityName(string cityName)
{
HttpResponseMessage response = await client.GetAsync(
String.Format(WeatherApiBaseUrl, "q=" + cityName));
String.Format(WeatherApiBaseUrl, CurrentWeather, CityNameParam, cityName));
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
Expand All @@ -23,9 +27,44 @@ public static async Task<string> GetWeatherByCityName(string cityName)
}
}

public static async Task<string> GetWeatherForecastByCityName(string cityName)
{
HttpResponseMessage response = await client.GetAsync(
String.Format(WeatherApiBaseUrl, Forecast, CityNameParam, cityName));
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Uh oh, weather forecast for {cityName} is not found";
}
}

public static string GetCurrentTemperature(string cityName)
{
return string.Empty;
}

public static string GetCity(string text)
{
int idx = text.IndexOf("for");
int len = 4;
if (idx == 0)
{
idx = text.IndexOf("in");
if (idx > 0)
{
len = 3;
}
}

if (idx > 0)
{
return text.Substring(idx + len);
}

return String.Empty;
}
}
}
3 changes: 3 additions & 0 deletions WeatherBot/WeatherBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AdaptiveCards" Version="0.5.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.0.2-alpha" />
<PackageReference Include="Microsoft.Bot.Builder.Ai" Version="4.0.2-alpha" />
<PackageReference Include="Microsoft.Bot.Builder.Azure" Version="4.0.2-alpha" />
<PackageReference Include="Microsoft.Bot.Builder.BotFramework" Version="4.0.2-alpha" />
<PackageReference Include="Microsoft.Bot.Builder.Conversation" Version="4.0.2-alpha" />
<PackageReference Include="Microsoft.Bot.Connector" Version="3.12.2.4" />
<PackageReference Include="Microsoft.Bot.Connector.AspNetCore" Version="2.0.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1-beta1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 170d371

Please sign in to comment.