2024-03-07 18:27:29 +00:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.ChatHub.Hooks;
|
|
|
|
|
|
|
|
|
|
public class WelcomeHook : ConversationHookBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IHubContext<SignalRHub> _chatHub;
|
2025-02-27 22:19:43 +00:00
|
|
|
private readonly ILogger<WelcomeHook> _logger;
|
2024-03-07 18:27:29 +00:00
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
private readonly IConversationStorage _storage;
|
2024-03-18 19:36:41 +00:00
|
|
|
private readonly BotSharpOptions _options;
|
2025-02-13 20:52:46 +00:00
|
|
|
private readonly ChatHubSettings _settings;
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
private const string RECEIVE_ASSISTANT_MESSAGE = "OnMessageReceivedFromAssistant";
|
|
|
|
|
#endregion
|
2024-03-18 19:36:41 +00:00
|
|
|
|
2024-03-07 18:27:29 +00:00
|
|
|
public WelcomeHook(IServiceProvider services,
|
|
|
|
|
IHubContext<SignalRHub> chatHub,
|
2025-02-27 22:19:43 +00:00
|
|
|
ILogger<WelcomeHook> logger,
|
2024-03-07 18:27:29 +00:00
|
|
|
IUserIdentity user,
|
2024-03-18 19:36:41 +00:00
|
|
|
IConversationStorage storage,
|
2025-02-13 20:52:46 +00:00
|
|
|
BotSharpOptions options,
|
|
|
|
|
ChatHubSettings settings)
|
2024-03-07 18:27:29 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_chatHub = chatHub;
|
2025-02-27 22:19:43 +00:00
|
|
|
_logger = logger;
|
2024-03-07 18:27:29 +00:00
|
|
|
_user = user;
|
|
|
|
|
_storage = storage;
|
2024-03-18 19:36:41 +00:00
|
|
|
_options = options;
|
2025-02-13 20:52:46 +00:00
|
|
|
_settings = settings;
|
2024-03-07 18:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
|
|
|
|
|
{
|
2025-01-08 22:05:29 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var agent = db.GetAgent(conversation.AgentId);
|
2024-03-07 18:27:29 +00:00
|
|
|
|
|
|
|
|
// Check if the Welcome template exists.
|
2025-01-08 22:05:29 +00:00
|
|
|
var welcomeTemplate = agent?.Templates?.FirstOrDefault(x => x.Name == ".welcome");
|
2024-03-07 18:27:29 +00:00
|
|
|
if (welcomeTemplate != null)
|
|
|
|
|
{
|
|
|
|
|
// Render template
|
|
|
|
|
var templating = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
var user = _services.GetRequiredService<IUserIdentity>();
|
2024-09-16 22:04:08 +00:00
|
|
|
var content = templating.Render(welcomeTemplate.Content, new Dictionary<string, object>
|
2024-03-07 18:27:29 +00:00
|
|
|
{
|
|
|
|
|
{ "user", user }
|
|
|
|
|
});
|
|
|
|
|
var richContentService = _services.GetRequiredService<IRichContentService>();
|
2024-09-16 22:04:08 +00:00
|
|
|
var messages = richContentService.ConvertToMessages(content);
|
2024-10-11 20:07:41 +00:00
|
|
|
var guid = Guid.NewGuid().ToString();
|
2024-03-07 18:27:29 +00:00
|
|
|
|
|
|
|
|
foreach (var message in messages)
|
|
|
|
|
{
|
2024-09-17 17:50:39 +00:00
|
|
|
var richContent = new RichContent<IRichMessage>(message);
|
2024-10-11 20:07:41 +00:00
|
|
|
var dialog = new RoleDialogModel(AgentRole.Assistant, message.Text)
|
|
|
|
|
{
|
|
|
|
|
MessageId = guid,
|
|
|
|
|
CurrentAgentId = agent.Id,
|
|
|
|
|
RichContent = richContent
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-07 18:27:29 +00:00
|
|
|
var json = JsonSerializer.Serialize(new ChatResponseModel()
|
|
|
|
|
{
|
|
|
|
|
ConversationId = conversation.Id,
|
2024-10-11 20:07:41 +00:00
|
|
|
MessageId = dialog.MessageId,
|
2024-03-07 18:27:29 +00:00
|
|
|
Text = message.Text,
|
2024-09-16 22:04:08 +00:00
|
|
|
RichContent = richContent,
|
2024-03-07 18:27:29 +00:00
|
|
|
Sender = new UserViewModel()
|
|
|
|
|
{
|
|
|
|
|
FirstName = agent.Name,
|
|
|
|
|
LastName = "",
|
|
|
|
|
Role = AgentRole.Assistant
|
|
|
|
|
}
|
2024-03-18 19:36:41 +00:00
|
|
|
}, _options.JsonSerializerOptions);
|
2024-03-07 18:27:29 +00:00
|
|
|
|
|
|
|
|
await Task.Delay(300);
|
|
|
|
|
|
2024-10-11 20:07:41 +00:00
|
|
|
_storage.Append(conversation.Id, dialog);
|
2024-03-07 18:27:29 +00:00
|
|
|
|
2025-02-27 22:19:43 +00:00
|
|
|
await SendEvent(conversation.Id, json);
|
2024-03-07 18:27:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await base.OnUserAgentConnectedInitially(conversation);
|
|
|
|
|
}
|
2025-02-27 22:19:43 +00:00
|
|
|
|
|
|
|
|
private async Task SendEvent(string conversationId, string json)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_settings.EventDispatchBy == EventDispatchType.Group)
|
|
|
|
|
{
|
|
|
|
|
await _chatHub.Clients.Group(conversationId).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _chatHub.Clients.User(_user.Id).SendAsync(RECEIVE_ASSISTANT_MESSAGE, json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-05-02 16:31:28 +00:00
|
|
|
_logger.LogWarning(ex, $"Failed to send event in {nameof(WelcomeHook)} (conversation id: {conversationId}).");
|
2025-02-27 22:19:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-07 18:27:29 +00:00
|
|
|
}
|