diff --git a/WeatherBot/Controllers/MessagesController.cs b/WeatherBot/Controllers/MessagesController.cs new file mode 100644 index 0000000..7eaa848 --- /dev/null +++ b/WeatherBot/Controllers/MessagesController.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/WeatherBot/Controllers/ValuesController.cs b/WeatherBot/Controllers/ValuesController.cs deleted file mode 100644 index 02b2057..0000000 --- a/WeatherBot/Controllers/ValuesController.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace WeatherBot.Controllers -{ - [Route("api/[controller]")] - public class ValuesController : Controller - { - // GET api/values - [HttpGet] - public IEnumerable Get() - { - return new string[] { "value1", "value2" }; - } - - // GET api/values/5 - [HttpGet("{id}")] - public string Get(int id) - { - return "value"; - } - - // POST api/values - [HttpPost] - public void Post([FromBody]string value) - { - } - - // PUT api/values/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody]string value) - { - } - - // DELETE api/values/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} diff --git a/WeatherBot/Startup.cs b/WeatherBot/Startup.cs index de734f8..15f18b3 100644 --- a/WeatherBot/Startup.cs +++ b/WeatherBot/Startup.cs @@ -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(); } } diff --git a/WeatherBot/Weather.cs b/WeatherBot/Weather.cs new file mode 100644 index 0000000..9dd836d --- /dev/null +++ b/WeatherBot/Weather.cs @@ -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 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; + } + } +} diff --git a/WeatherBot/WeatherBot.csproj b/WeatherBot/WeatherBot.csproj index 2a6a149..b6300f8 100644 --- a/WeatherBot/WeatherBot.csproj +++ b/WeatherBot/WeatherBot.csproj @@ -10,6 +10,13 @@ + + + + + + + diff --git a/WeatherBot/appsettings.json b/WeatherBot/appsettings.json index 26bb0ac..4587880 100644 --- a/WeatherBot/appsettings.json +++ b/WeatherBot/appsettings.json @@ -11,5 +11,9 @@ "Default": "Warning" } } - } + }, + + "MicrosoftAppId": "", + "MicrosoftAppPassword": "", + "DataConnectionString": "" }