2018-10-03 06:00:07 +00:00
|
|
|
|
using BotSharp.Core.Engines;
|
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-10-03 01:44:11 +00:00
|
|
|
|
using Platform.Dialogflow.Models;
|
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
|
|
|
|
|
2018-10-03 01:44:11 +00:00
|
|
|
|
namespace Platform.Dialogflow.Controllers
|
2018-07-13 12:21:40 +00:00
|
|
|
|
{
|
2018-10-03 01:44:11 +00:00
|
|
|
|
#if DIALOGFLOW
|
2018-07-15 12:31:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Agent
|
|
|
|
|
|
/// </summary>
|
2018-10-03 01:44:11 +00:00
|
|
|
|
[Route("v1/[controller]")]
|
2018-07-13 12:21:40 +00:00
|
|
|
|
public class AgentController : ControllerBase
|
|
|
|
|
|
{
|
2018-10-03 01:44:11 +00:00
|
|
|
|
private DialogflowAi<AgentModel> builder;
|
2018-08-06 15:51:49 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize dialog controller and get a platform instance
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="platform"></param>
|
2018-10-03 01:44:11 +00:00
|
|
|
|
public AgentController(IConfiguration configuration)
|
2018-08-06 15:51:49 +00:00
|
|
|
|
{
|
2018-10-03 01:44:11 +00:00
|
|
|
|
builder = new DialogflowAi<AgentModel>();
|
|
|
|
|
|
builder.PlatformConfig = configuration.GetSection("DialogflowAi");
|
2018-08-12 04:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-13 12:21:40 +00:00
|
|
|
|
/// <summary>
|
2018-10-03 01:44:11 +00:00
|
|
|
|
/// Import a agent from a uploaded zip file
|
2018-07-13 12:21:40 +00:00
|
|
|
|
/// </summary>
|
2018-08-27 12:59:47 +00:00
|
|
|
|
/// <param name="file"></param>
|
2018-07-13 12:21:40 +00:00
|
|
|
|
/// <returns></returns>
|
2018-10-03 01:44:11 +00:00
|
|
|
|
[HttpPost("import")]
|
|
|
|
|
|
public async Task<ActionResult> Import(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();
|
2018-09-21 20:11:57 +00:00
|
|
|
|
Console.WriteLine($"Temp file saved to {filePath}");
|
2018-08-27 12:59:47 +00:00
|
|
|
|
|
|
|
|
|
|
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-10-03 01:44:11 +00:00
|
|
|
|
string dest = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", uploadedFile.FileName.Split('.').First(), "tmp");
|
2018-10-03 06:00:07 +00:00
|
|
|
|
if (Directory.Exists(dest))
|
|
|
|
|
|
{
|
|
|
|
|
|
System.IO.Directory.Delete(dest, true);
|
|
|
|
|
|
}
|
2018-09-21 20:11:57 +00:00
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Extract zip file to {dest}");
|
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-10-03 06:00:07 +00:00
|
|
|
|
Console.WriteLine($"Loading agent from folder {dest}");
|
2018-10-03 01:44:11 +00:00
|
|
|
|
var agent = builder.LoadAgentFromFile<AgentImporterInDialogflow<AgentModel>>(dest);
|
2018-10-03 06:00:07 +00:00
|
|
|
|
builder.SaveAgent(agent);
|
2018-09-21 20:11:57 +00:00
|
|
|
|
|
2018-08-27 21:04:00 +00:00
|
|
|
|
return Ok(agent.Id);
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
2018-07-15 12:31:52 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Dump agent
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="agentId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet("{agentId}")]
|
2018-10-03 06:00:07 +00:00
|
|
|
|
public ActionResult Dump([FromRoute] String agentId)
|
2018-07-15 12:31:52 +00:00
|
|
|
|
{
|
2018-10-03 01:44:11 +00:00
|
|
|
|
return Ok();
|
2018-07-15 12:31:52 +00:00
|
|
|
|
}
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|
2018-10-03 01:44:11 +00:00
|
|
|
|
#endif
|
2018-07-13 12:21:40 +00:00
|
|
|
|
}
|