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 00000000..b150f5f4 Binary files /dev/null and b/BotSharp.WebHost/wwwroot/images/favicon.ico differ