2018-07-15 12:31:52 +00:00
|
|
|
|
using BotSharp.Core.Agents;
|
|
|
|
|
|
using BotSharp.Core.Engines;
|
2018-08-03 22:18:40 +00:00
|
|
|
|
using BotSharp.Core.Engines.BotSharp;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using BotSharp.Core.Models;
|
2018-08-06 15:51:49 +00:00
|
|
|
|
using DotNetToolkit;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using EntityFrameworkCore.BootKit;
|
2018-08-27 12:59:47 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-08-06 15:51:49 +00:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using System.IO;
|
2018-08-27 14:14:20 +00:00
|
|
|
|
using System.IO.Compression;
|
2018-07-15 12:31:52 +00:00
|
|
|
|
using System.Linq;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
using System.Text;
|
2018-08-27 12:59:47 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2018-07-13 12:21:40 +00:00
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.RestApi
|
|
|
|
|
|
{
|
2018-07-15 12:31:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Agent
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Route("v1/[controller]/[action]")]
|
2018-07-13 12:21:40 +00:00
|
|
|
|
public class AgentController : ControllerBase
|
|
|
|
|
|
{
|
2018-08-06 15:51:49 +00:00
|
|
|
|
private readonly IBotPlatform _platform;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize dialog controller and get a platform instance
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="platform"></param>
|
|
|
|
|
|
public AgentController(IBotPlatform platform)
|
|
|
|
|
|
{
|
|
|
|
|
|
_platform = platform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-12 04:52:38 +00:00
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public ActionResult<List<Agent>> AllAgents()
|
|
|
|
|
|
{
|
|
|
|
|
|
var dc = new DefaultDataContextLoader().GetDefaultDc();
|
|
|
|
|
|
|
|
|
|
|
|
return dc.Table<Agent>().ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-13 12:21:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Restore a agent from a uploaded zip file
|
|
|
|
|
|
/// </summary>
|
2018-08-27 12:59:47 +00:00
|
|
|
|
/// <param name="file"></param>
|
2018-07-13 12:21:40 +00:00
|
|
|
|
/// <returns></returns>
|
2018-08-27 12:59:47 +00:00
|
|
|
|
[HttpPost]
|
2018-08-27 21:04:00 +00:00
|
|
|
|
public async Task<ActionResult> Restore(IFormFile uploadedFile)
|
2018-07-13 12:21:40 +00:00
|
|
|
|
{
|
2018-08-27 21:04:00 +00:00
|
|
|
|
if (uploadedFile == null || uploadedFile.Length == 0)
|
2018-08-27 12:59:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var filePath = Path.GetTempFileName();
|
|
|
|
|
|
|
|
|
|
|
|
using (var stream = new FileStream(filePath, FileMode.Create))
|
|
|
|
|
|
{
|
2018-08-27 21:04:00 +00:00
|
|
|
|
await uploadedFile.CopyToAsync(stream);
|
2018-08-27 12:59:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-27 21:04:00 +00:00
|
|
|
|
string dest = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", uploadedFile.FileName.Split('.').First(), "model_" + DateTime.UtcNow.ToString("MMddyyyyHHmm"));
|
2018-08-27 14:14:20 +00:00
|
|
|
|
ZipFile.ExtractToDirectory(filePath, dest);
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
2018-08-27 21:04:00 +00:00
|
|
|
|
System.IO.File.Delete(filePath);
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
2018-08-27 21:04:00 +00:00
|
|
|
|
var agent = _platform.LoadAgentFromFile<AgentImporterInDialogflow>(dest);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(agent.Id);
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
2018-08-06 15:29:14 +00:00
|
|
|
|
/// <summary>
|
2018-08-27 12:59:47 +00:00
|
|
|
|
/// Train agent
|
2018-08-06 15:29:14 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="agentId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet("{agentId}")]
|
|
|
|
|
|
public string Train([FromRoute] String agentId)
|
|
|
|
|
|
{
|
2018-08-06 15:51:49 +00:00
|
|
|
|
_platform.LoadAgent(agentId);
|
|
|
|
|
|
_platform.Train();
|
2018-08-06 15:29:14 +00:00
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-15 12:31:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Dump agent
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="agentId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet("{agentId}")]
|
|
|
|
|
|
public ActionResult<Agent> Dump([FromRoute] String agentId)
|
|
|
|
|
|
{
|
2018-08-06 15:51:49 +00:00
|
|
|
|
var agent = _platform.LoadAgent(agentId);
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
return Ok(agent);
|
|
|
|
|
|
}
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|