Skip to content

Commit

Permalink
take 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuwandy committed Dec 12, 2017
1 parent ddef353 commit 6eb2913
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 59 deletions.
59 changes: 59 additions & 0 deletions WeatherBot/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Connector;
using Microsoft.Extensions.Configuration;

namespace WeatherBot.Controllers
{
[Route("api/[controller]")]
public class MessagesController : Controller
{
public static BotFrameworkAdapter activityAdapter = null;
public static Bot bot = null;

public MessagesController(IConfiguration configuration)
{
if (activityAdapter == null)
{
string appId = configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value;
string appPassword = configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value;

// create the activity adapter to send/receive Activity objects
activityAdapter = new BotFrameworkAdapter(appId, appPassword);
bot = new Bot(activityAdapter)
.OnReceive(async (context) =>
{
// THIS IS YOUR BOT LOGIC
if (context.Request.Type == ActivityTypes.Message)
{
string city = context.Request.Text;
if (!string.IsNullOrWhiteSpace(city))
{
string weatherJson = await Weather.GetWeatherByCityName(city);
context.Reply(weatherJson);
//return new ReceiveResponse(true);
}
else
{

}
}
else
{
context.Reply("Type city name to get weather");
}
});
}
}

[Authorize(Roles = "Bot")]
[HttpPost]
public async void Post([FromBody]Activity activity)
{
await activityAdapter.Receive(HttpContext.Request.Headers, activity);
}
}
}
44 changes: 0 additions & 44 deletions WeatherBot/Controllers/ValuesController.cs

This file was deleted.

49 changes: 35 additions & 14 deletions WeatherBot/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace WeatherBot
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup(IHostingEnvironment env)
{
Configuration = configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public IConfiguration Configuration { get; }
public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton(_ => Configuration);
var credentialProvider = new StaticCredentialProvider(
Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value);

services.AddAuthentication(
// This can be removed after https://github.com/aspnet/IISIntegration/issues/371
options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}
)
.AddBotAuthentication(credentialProvider);

services.AddSingleton(typeof(ICredentialProvider), credentialProvider);
services.AddMvc(options =>
{
options.Filters.Add(typeof(TrustServiceUrlAttribute));
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc();
}
}
Expand Down
31 changes: 31 additions & 0 deletions WeatherBot/Weather.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace WeatherBot
{
public static class Weather
{
const string WeatherApiBaseUrl = "http://api.openweathermap.org/data/2.5/weather?{0}&units=imperial&APPID=a0f75f6b2f7ce29295822d2862df66a6";
static HttpClient client = new HttpClient();

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

public static string GetCurrentTemperature(string cityName)
{
return string.Empty;
}
}
}
7 changes: 7 additions & 0 deletions WeatherBot/WeatherBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

<ItemGroup>
<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.AspNetCore" Version="2.0.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion WeatherBot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"Default": "Warning"
}
}
}
},

"MicrosoftAppId": "",
"MicrosoftAppPassword": "",
"DataConnectionString": ""
}

0 comments on commit 6eb2913

Please sign in to comment.