BotSharp/Platform.Dialogflow/Controllers/AgentController.cs

90 lines
2.7 KiB
C#
Raw Normal View History

2018-10-03 06:00:07 +00:00
using BotSharp.Core.Engines;
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
2018-10-03 01:44:11 +00:00
using Platform.Dialogflow.Models;
using System;
using System.Collections.Generic;
using System.IO;
2018-08-27 14:14:20 +00:00
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2018-10-03 01:44:11 +00:00
namespace Platform.Dialogflow.Controllers
{
2018-10-03 01:44:11 +00:00
#if DIALOGFLOW
/// <summary>
/// Agent
/// </summary>
2018-10-03 01:44:11 +00:00
[Route("v1/[controller]")]
public class AgentController : ControllerBase
{
2018-10-03 01:44:11 +00:00
private DialogflowAi<AgentModel> builder;
/// <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-10-03 01:44:11 +00:00
builder = new DialogflowAi<AgentModel>();
builder.PlatformConfig = configuration.GetSection("DialogflowAi");
2018-08-12 04:52:38 +00:00
}
/// <summary>
2018-10-03 01:44:11 +00:00
/// Import a agent from a uploaded zip file
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
2018-10-03 01:44:11 +00:00
[HttpPost("import")]
public async Task<ActionResult> Import(IFormFile uploadedFile)
{
if (uploadedFile == null || uploadedFile.Length == 0)
{
return BadRequest();
}
var filePath = Path.GetTempFileName();
2018-09-21 20:11:57 +00:00
Console.WriteLine($"Temp file saved to {filePath}");
using (var stream = new FileStream(filePath, FileMode.Create))
{
await uploadedFile.CopyToAsync(stream);
}
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);
System.IO.File.Delete(filePath);
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
return Ok(agent.Id);
}
/// <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-10-03 01:44:11 +00:00
return Ok();
}
}
2018-10-03 01:44:11 +00:00
#endif
}