BotSharp/Platform.Articulate/Controllers/IntentController.cs

132 lines
4.4 KiB
C#
Raw Normal View History

2018-09-29 18:18:34 +00:00
using BotSharp.Core;
using BotSharp.Platform.Models;
2018-09-30 21:52:33 +00:00
using DotNetToolkit;
2018-09-29 18:18:34 +00:00
using Microsoft.AspNetCore.Mvc;
2018-10-01 17:15:17 +00:00
using Microsoft.Extensions.Configuration;
2018-09-28 00:49:43 +00:00
using Newtonsoft.Json;
using Platform.Articulate.Models;
using Platform.Articulate.ViewModels;
2018-09-28 00:49:43 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Platform.Articulate.Controllers
2018-09-28 00:49:43 +00:00
{
#if ARTICULATE
[Route("[controller]")]
public class IntentController : ControllerBase
{
2018-10-01 17:15:17 +00:00
private readonly IConfiguration configuration;
private ArticulateAi<AgentModel> builder;
2018-09-29 18:18:34 +00:00
2018-10-01 17:15:17 +00:00
public IntentController(IConfiguration configuration)
2018-09-29 18:18:34 +00:00
{
2018-10-01 17:15:17 +00:00
builder = new ArticulateAi<AgentModel>();
builder.PlatformConfig = configuration.GetSection("ArticulateAi");
2018-09-29 18:18:34 +00:00
}
2018-09-28 00:49:43 +00:00
[HttpGet("{intentId}")]
2018-09-30 21:52:33 +00:00
public IntentViewModel GetIntent([FromRoute] string intentId)
2018-09-28 00:49:43 +00:00
{
2018-09-30 15:11:36 +00:00
var agent = builder.GetAgentByIntentId(intentId);
2018-09-28 00:49:43 +00:00
2018-09-30 21:52:33 +00:00
return agent.Item3.ToObject<IntentViewModel>();
2018-09-28 00:49:43 +00:00
}
2018-09-29 18:18:34 +00:00
[HttpPost]
2018-09-30 21:52:33 +00:00
public IntentViewModel PostIntent()
2018-09-29 18:18:34 +00:00
{
2018-09-30 21:52:33 +00:00
IntentViewModel intent = null;
2018-09-29 18:18:34 +00:00
using (var reader = new StreamReader(Request.Body))
{
string body = reader.ReadToEnd();
2018-09-30 21:52:33 +00:00
intent = JsonConvert.DeserializeObject<IntentViewModel>(body);
2018-09-29 18:18:34 +00:00
}
var agent = builder.GetAgentByName(intent.Agent);
intent.Id = Guid.NewGuid().ToString();
2018-10-01 01:27:57 +00:00
var domain = agent.Domains.First(x => x.DomainName == intent.Domain);
domain.Intents.Add(intent.ToObject<IntentModel>());
2018-09-29 18:18:34 +00:00
builder.SaveAgent(agent);
return intent;
}
2018-10-01 17:15:17 +00:00
[HttpPut("{intentId}")]
public IntentViewModel PutIntent([FromRoute] string intentId)
{
IntentViewModel intent = null;
using (var reader = new StreamReader(Request.Body))
{
string body = reader.ReadToEnd();
intent = JsonConvert.DeserializeObject<IntentViewModel>(body);
}
var agent = builder.GetAgentByIntentId(intentId);
var updateAgent = agent.Item1;
var updateIntents = updateAgent.Domains.First(x => x.Id == agent.Item2.Id).Intents;
var updateIntent = updateIntents.First(x => x.Id == agent.Item3.Id);
updateIntent.IntentName = intent.IntentName;
updateIntent.Examples = intent.Examples;
builder.SaveAgent(updateAgent);
return intent;
}
2018-09-28 00:49:43 +00:00
[HttpGet("{intentId}/webhook")]
2018-09-29 18:18:34 +00:00
public void GetIntentWebhook([FromRoute] string intentId)
2018-09-28 00:49:43 +00:00
{
}
[HttpGet("{intentId}/postFormat")]
2018-09-29 18:18:34 +00:00
public void GetIntentPostFormat([FromRoute] string intentId)
2018-09-28 00:49:43 +00:00
{
}
[HttpGet("/agent/{agentId}/intent")]
2018-09-29 18:18:34 +00:00
public IntentPageViewModel GetAgentIntents([FromRoute] string agentId, [FromQuery] int start, [FromQuery] int limit)
2018-09-28 00:49:43 +00:00
{
2018-09-30 21:52:33 +00:00
var intents = new List<IntentViewModel>();
2018-09-28 00:49:43 +00:00
string dataDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Articulate");
var agentPaths = Directory.GetFiles(dataDir).Where(x => x.Contains($"agent-{agentId}-intent-")).ToList();
for (int i = 0; i < agentPaths.Count; i++)
{
string json = System.IO.File.ReadAllText(agentPaths[i]);
2018-09-30 21:52:33 +00:00
var intent = JsonConvert.DeserializeObject<IntentViewModel>(json);
2018-09-28 00:49:43 +00:00
intents.Add(intent);
}
return new IntentPageViewModel { Intents = intents, Total = intents.Count };
}
[HttpGet("/entity/{entityId}/intent")]
2018-10-01 01:27:57 +00:00
public List<IntentViewModel> GetReferencedIntentsByEntity([FromRoute] string entityId, [FromQuery] int start, [FromQuery] int limit)
2018-09-28 00:49:43 +00:00
{
2018-10-01 01:27:57 +00:00
return builder.GetReferencedIntentsByEntity(entityId).Select(x => x.ToObject<IntentViewModel>()).ToList();
2018-09-28 00:49:43 +00:00
}
[HttpGet("/domain/{domainId}/intent")]
2018-09-29 18:18:34 +00:00
public IntentPageViewModel GetReferencedIntentsByDomain([FromRoute] string domainId, [FromQuery] int start, [FromQuery] int limit)
2018-09-28 00:49:43 +00:00
{
2018-09-30 15:11:36 +00:00
var agent = builder.GetAgentByDomainId(domainId);
2018-09-30 21:52:33 +00:00
var intents = agent.Item2.Intents.Select(x => x.ToObject<IntentViewModel>()).ToList();
2018-09-28 00:49:43 +00:00
return new IntentPageViewModel { Intents = intents, Total = intents.Count };
}
}
#endif
}