2023-06-12 13:28:49 +00:00
|
|
|
using BotSharp.Abstraction.ApiAdapters;
|
2023-06-23 04:43:00 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
2023-06-12 13:28:49 +00:00
|
|
|
using BotSharp.Core.Conversations.ViewModels;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Conversations;
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class ConversationController : ControllerBase, IApiAdapter
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
2023-06-23 04:43:00 +00:00
|
|
|
private readonly IUserIdentity _user;
|
2023-06-12 13:28:49 +00:00
|
|
|
|
2023-06-23 04:43:00 +00:00
|
|
|
public ConversationController(IServiceProvider services,
|
|
|
|
|
IUserIdentity user)
|
2023-06-12 13:28:49 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2023-06-23 04:43:00 +00:00
|
|
|
_user = user;
|
2023-06-12 13:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-23 04:43:00 +00:00
|
|
|
[HttpPost("/conversation/{agentId}")]
|
2023-06-27 18:31:13 +00:00
|
|
|
public async Task<ConversationViewModel> NewConversation([FromRoute] string agentId)
|
2023-06-12 13:28:49 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var service = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var sess = new Conversation
|
2023-06-23 04:43:00 +00:00
|
|
|
{
|
2023-07-21 15:15:30 +00:00
|
|
|
UserId = _user.Id,
|
2023-06-23 04:43:00 +00:00
|
|
|
AgentId = agentId
|
|
|
|
|
};
|
2023-06-27 18:31:13 +00:00
|
|
|
sess = await service.NewConversation(sess);
|
|
|
|
|
return ConversationViewModel.FromSession(sess);
|
2023-06-12 13:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
[HttpDelete("/conversation/{agentId}/{conversationId}")]
|
|
|
|
|
public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
|
2023-06-12 13:28:49 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var service = _services.GetRequiredService<IConversationService>();
|
2023-06-12 13:28:49 +00:00
|
|
|
}
|
2023-06-13 03:27:31 +00:00
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
[HttpPost("/conversation/{agentId}/{conversationId}")]
|
2023-06-23 04:43:00 +00:00
|
|
|
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
|
2023-06-27 18:31:13 +00:00
|
|
|
[FromRoute] string conversationId,
|
2023-06-23 04:43:00 +00:00
|
|
|
[FromBody] NewMessageModel input)
|
2023-06-13 03:27:31 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
2023-06-13 03:27:31 +00:00
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
var response = new MessageResponseModel();
|
2023-06-13 03:27:31 +00:00
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
await conv.SendMessage(agentId, conversationId, new RoleDialogModel("user", input.Text), async msg =>
|
2023-06-13 03:27:31 +00:00
|
|
|
{
|
2023-07-27 15:07:39 +00:00
|
|
|
response.Text += msg.Content;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return response;
|
2023-06-13 03:27:31 +00:00
|
|
|
}
|
2023-06-12 13:28:49 +00:00
|
|
|
}
|