From 51113f98b24769ee2c382c55e77797cef20eb629 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Tue, 31 Jul 2018 23:29:55 -0500 Subject: [PATCH] Abstract Bot Platform query interface, add platform dependency injection. --- BotSharp.Core/Abstractions/IBotPlatform.cs | 10 ++- BotSharp.Core/Engines/BotEngineBase.cs | 5 -- BotSharp.RestApi/ConversationController.cs | 13 ---- BotSharp.RestApi/Dialogs/DialogController.cs | 62 +++++++++++++++++++ BotSharp.RestApi/Dialogs/QueryModel.cs | 27 ++++++++ BotSharp.WebHost/Settings/app.json | 3 +- BotSharp.WebHost/Startup.cs | 25 ++++++++ BotSharp.WebHost/wwwroot/images/favicon.ico | Bin 0 -> 4286 bytes 8 files changed, 125 insertions(+), 20 deletions(-) delete mode 100644 BotSharp.RestApi/ConversationController.cs create mode 100644 BotSharp.RestApi/Dialogs/DialogController.cs create mode 100644 BotSharp.RestApi/Dialogs/QueryModel.cs create mode 100644 BotSharp.WebHost/wwwroot/images/favicon.ico diff --git a/BotSharp.Core/Abstractions/IBotPlatform.cs b/BotSharp.Core/Abstractions/IBotPlatform.cs index df6ee8bd..a07c4198 100644 --- a/BotSharp.Core/Abstractions/IBotPlatform.cs +++ b/BotSharp.Core/Abstractions/IBotPlatform.cs @@ -1,4 +1,5 @@ -using BotSharp.Core.Models; +using BotSharp.Core.Agents; +using BotSharp.Core.Models; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -8,6 +9,13 @@ namespace BotSharp.Core.Engines { public interface IBotPlatform { + /// + /// Load agent profile + /// + /// agentId, clientAccessToken, developerAccessToken + /// + Agent LoadAgent(string id); + AIResponse TextRequest(AIRequest request); void Train(); diff --git a/BotSharp.Core/Engines/BotEngineBase.cs b/BotSharp.Core/Engines/BotEngineBase.cs index 47c6c544..345da2c1 100644 --- a/BotSharp.Core/Engines/BotEngineBase.cs +++ b/BotSharp.Core/Engines/BotEngineBase.cs @@ -31,11 +31,6 @@ namespace BotSharp.Core.Engines DbInitializerPath = $"{Database.ContentRootPath}App_Data{Path.DirectorySeparatorChar}DbInitializer{Path.DirectorySeparatorChar}"; } - /// - /// Load Agent - /// - /// agentId, clientAccessToken, developerAccessToken - /// public Agent LoadAgent(string id) { if (agent == null) diff --git a/BotSharp.RestApi/ConversationController.cs b/BotSharp.RestApi/ConversationController.cs deleted file mode 100644 index 98786113..00000000 --- a/BotSharp.RestApi/ConversationController.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.RestApi -{ - [Route("v1/[controller]/[action]")] - public class ConversationController : ControllerBase - { - - } -} diff --git a/BotSharp.RestApi/Dialogs/DialogController.cs b/BotSharp.RestApi/Dialogs/DialogController.cs new file mode 100644 index 00000000..85487baf --- /dev/null +++ b/BotSharp.RestApi/Dialogs/DialogController.cs @@ -0,0 +1,62 @@ +using BotSharp.Core.Engines; +using BotSharp.Core.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BotSharp.RestApi.Dialogs +{ + /// + /// Conversation controller + /// + [Route("v1/[controller]")] + public class DialogController : ControllerBase + { + private readonly IBotPlatform _platform; + + /// + /// Initialize dialog controller and get a platform instance + /// + /// + public DialogController(IBotPlatform platform) + { + _platform = platform; + } + + /// + /// The query endpoint is used to process natural language in the form of text. + /// The query requests return structured data in JSON format with an action and parameters for that action. + /// + /// + /// + [AllowAnonymous] + [HttpPost("/v1/query")] + public ActionResult Query([FromBody] QueryModel request) + { + String clientAccessToken = Request.Headers["Authorization"]; + if (String.IsNullOrEmpty(clientAccessToken)) + { + return Unauthorized(); + } + + var config = new AIConfiguration(clientAccessToken, SupportedLanguage.English); + config.SessionId = request.SessionId; + + _platform.LoadAgent(clientAccessToken); + + var aIResponse = _platform.TextRequest(new AIRequest + { + Timezone = request.Timezone, + Contexts = request.Contexts.Select(x => new AIContext { Name = x }).ToList(), + Language = request.Lang, + Query = new String[] { request.Query } + }); + + return aIResponse; + } + } +} diff --git a/BotSharp.RestApi/Dialogs/QueryModel.cs b/BotSharp.RestApi/Dialogs/QueryModel.cs new file mode 100644 index 00000000..d08257df --- /dev/null +++ b/BotSharp.RestApi/Dialogs/QueryModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace BotSharp.RestApi.Dialogs +{ + public class QueryModel + { + public List Contexts { get; set; } + + public String Event { get;set; } + + public String Lang { get; set; } + + /// + /// User says + /// + [Required] + public String Query { get; set; } + + [Required] + public String SessionId { get; set; } + + public String Timezone { get; set; } + } +} diff --git a/BotSharp.WebHost/Settings/app.json b/BotSharp.WebHost/Settings/app.json index 497ba711..b86eafa8 100644 --- a/BotSharp.WebHost/Settings/app.json +++ b/BotSharp.WebHost/Settings/app.json @@ -1,3 +1,4 @@ { - "Assemblies": "BotSharp.Core" + "Assemblies": "BotSharp.Core", + "BotPlatform": "ApiAi" } diff --git a/BotSharp.WebHost/Startup.cs b/BotSharp.WebHost/Startup.cs index 509ac10f..4a160690 100644 --- a/BotSharp.WebHost/Startup.cs +++ b/BotSharp.WebHost/Startup.cs @@ -1,10 +1,13 @@ using System; using System.IO; using System.Linq; +using BotSharp.Core.Engines; +using DotNetToolkit; using EntityFrameworkCore.BootKit; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.PlatformAbstractions; @@ -43,6 +46,17 @@ namespace BotSharp.WebHost var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml"); c.IncludeXmlComments(filePath); }); + + // register platform dependency + services.AddTransient((provider) => + { + var implements = TypeHelper.GetClassesWithInterface(Database.Assemblies); + string platform = Database.Configuration.GetValue("BotPlatform"); + var implement = implements.FirstOrDefault(x => x.Name.Split('.').Last() == platform); + var instance = (IBotPlatform)Activator.CreateInstance(implement); + + return instance; + }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) @@ -73,6 +87,17 @@ namespace BotSharp.WebHost app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()); + app.Use(async (context, next) => + { + string token = context.Request.Headers["Authorization"]; + if (string.IsNullOrWhiteSpace(token)) + { + } + + await next.Invoke(); + }); + app.UseAuthentication(); + app.UseMvc(); Database.Configuration = Configuration; diff --git a/BotSharp.WebHost/wwwroot/images/favicon.ico b/BotSharp.WebHost/wwwroot/images/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..b150f5f4147f6295e7b3880291fe4ce1d3edb5cc GIT binary patch literal 4286 zcmb`KTd2)h9LLug%yDfWcXHW;A-4y&muVa|;XyRbgGizj!wcobl-dVH#1mp_rp5zF zGI9wIbflSSGDYyH>y|Bo>> z{0$ps^lJ{*8Pm@gGX_wZ86bUfG1j|VGtG*Rzt0YJH1GusfGiBHl7ZOug3eF_AA?re zl8@yZL4vK09r zv^T%+|CD&^p({jdkRJNLIq-cySNk<_a}b>e^vXXAo~>O=n1_eXx;|Hcvt|Z;Vn3SUuu5qz)zplgKjMR3% z@IQopItK@V?-Fgx2D-x;2;-=)=d;zlpp)N7xC^15_T|?Q%_*hEEXw^O6d{bG{uW4V z{a{un-}4aq={(&H+FyDWV=P0TgR>BuL&dxgQ^8N2w57A`MTYs+R}Gbj`S`k5_^*NJ z`w_j%uXmU4*WR}Fug=FGpw+tj6T!G@m<}(Xy?N!Y`O@B*1g+NG&#kt#8G`%=K1Y7V z(7Asc^d9a2VcM3y#z5aNX?y1pV|o{bTtjyUp1@m()vmGC&{`VHwVJCLy$`_cCW zy>oo}(bv39g!yn7VtYXQcp;>&g&kXh)Y^qL{En`NY+{%6mafQ6&;((OOXvnbEQIr) z#`v>{?>%&1rTMym8VAkOUl8WkH^TSe%*&+op!&3?p>GjgtM_MF06N$>c1B_-<_2ez z;;U!mXo~N;G+!6XjU#ub#MtF*oR5=EkKCBzdjP%O)y}0%N^j9R#pY1=zunoDIopmM z`xdGF7OsKz-V_)G%HcDX{$yATe?aK#`YRYq>a37oYp@(11aY*#^__MH z<<~Pj0b;3JbQ%YJFMkJ@A+{gg{d|hl*;4TwbVXq2)^Z%}Um&(8tIe(Y^-K>0m!eH6 zTZ_(=xo`w@mR36-vbzhLp+BUqd!=HWA5!Pv3{bpta0BkZZMbFoTCYa<6;{C*sA|qD z)!@d6rF+mGtb?I2!jeVme-XV8x