2018-08-01 04:29:55 +00:00
|
|
|
|
using BotSharp.Core.Engines;
|
|
|
|
|
|
using BotSharp.Core.Models;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
2018-08-13 16:12:10 +00:00
|
|
|
|
namespace BotSharp.RestApi.Dialogflow
|
2018-08-01 04:29:55 +00:00
|
|
|
|
{
|
2018-08-13 16:12:10 +00:00
|
|
|
|
#if MODE_DIALOGFLOW
|
2018-08-01 04:29:55 +00:00
|
|
|
|
/// <summary>
|
2018-08-13 16:12:10 +00:00
|
|
|
|
/// Dialogflow mode query controller
|
2018-08-01 04:29:55 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Route("v1/[controller]")]
|
2018-08-13 16:12:10 +00:00
|
|
|
|
public class QueryController : ControllerBase
|
2018-08-01 04:29:55 +00:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IBotPlatform _platform;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize dialog controller and get a platform instance
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="platform"></param>
|
2018-08-13 16:12:10 +00:00
|
|
|
|
public QueryController(IBotPlatform platform)
|
2018-08-01 04:29:55 +00:00
|
|
|
|
{
|
|
|
|
|
|
_platform = platform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
2018-08-13 16:12:10 +00:00
|
|
|
|
/// Both GET and POST methods return the same JSON response.
|
2018-08-01 04:29:55 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2018-08-13 16:12:10 +00:00
|
|
|
|
[HttpGet, HttpPost]
|
|
|
|
|
|
public ActionResult<AIResponse> Query(QueryModel request)
|
2018-08-01 04:29:55 +00:00
|
|
|
|
{
|
2018-08-09 21:06:40 +00:00
|
|
|
|
String clientAccessToken = Request.Headers["ClientAccessToken"];
|
2018-08-01 04:29:55 +00:00
|
|
|
|
var config = new AIConfiguration(clientAccessToken, SupportedLanguage.English);
|
|
|
|
|
|
config.SessionId = request.SessionId;
|
|
|
|
|
|
|
|
|
|
|
|
_platform.LoadAgent(clientAccessToken);
|
|
|
|
|
|
|
|
|
|
|
|
var aIResponse = _platform.TextRequest(new AIRequest
|
|
|
|
|
|
{
|
|
|
|
|
|
Timezone = request.Timezone,
|
2018-08-10 14:52:31 +00:00
|
|
|
|
Contexts = request?.Contexts?.Select(x => new AIContext { Name = x })?.ToList(),
|
2018-08-01 04:29:55 +00:00
|
|
|
|
Language = request.Lang,
|
|
|
|
|
|
Query = new String[] { request.Query }
|
|
|
|
|
|
});
|
2018-08-13 16:12:10 +00:00
|
|
|
|
|
2018-08-01 04:29:55 +00:00
|
|
|
|
return aIResponse;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-13 16:12:10 +00:00
|
|
|
|
#endif
|
2018-08-01 04:29:55 +00:00
|
|
|
|
}
|