BotSharp/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs

57 lines
1.8 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.ApiAdapters;
2023-06-23 04:43:00 +00:00
using BotSharp.Abstraction.Conversations.Models;
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-23 04:43:00 +00:00
public ConversationController(IServiceProvider services,
IUserIdentity user)
{
_services = services;
2023-06-23 04:43:00 +00:00
_user = user;
}
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-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-27 18:31:13 +00:00
[HttpDelete("/conversation/{agentId}/{conversationId}")]
public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
{
2023-06-27 18:31:13 +00:00
var service = _services.GetRequiredService<IConversationService>();
}
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
var result = await conv.SendMessage(agentId, conversationId, new RoleDialogModel("user", input.Text));
2023-06-13 03:27:31 +00:00
return new MessageResponseModel
{
2023-07-21 15:15:30 +00:00
Text = result
2023-06-13 03:27:31 +00:00
};
}
}