BotSharp/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs

96 lines
3.6 KiB
C#
Raw Normal View History

2025-05-07 21:34:39 +00:00
using BotSharp.Abstraction.Conversations.Dtos;
2025-07-28 22:41:28 +00:00
using BotSharp.Abstraction.Conversations.Enums;
2024-03-07 18:27:29 +00:00
using Microsoft.AspNetCore.SignalR;
2025-07-28 22:41:28 +00:00
using System.Runtime.CompilerServices;
2024-03-07 18:27:29 +00:00
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;
2025-07-28 22:41:28 +00:00
public WelcomeHook(
IServiceProvider services,
2024-03-07 18:27:29 +00:00
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
};
2025-07-28 22:41:28 +00:00
var data = new ChatResponseDto()
2024-03-07 18:27:29 +00:00
{
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,
2025-05-07 21:34:39 +00:00
Sender = new()
2024-03-07 18:27:29 +00:00
{
FirstName = agent.Name,
LastName = "",
Role = AgentRole.Assistant
}
2025-07-28 22:41:28 +00:00
};
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);
2025-07-28 22:41:28 +00:00
await SendEvent(ChatEvent.OnMessageReceivedFromAssistant, conversation.Id, data);
2024-03-07 18:27:29 +00:00
}
}
await base.OnUserAgentConnectedInitially(conversation);
}
2025-02-27 22:19:43 +00:00
2025-07-28 22:41:28 +00:00
private async Task SendEvent<T>(string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
2025-02-27 22:19:43 +00:00
{
2025-07-28 22:41:28 +00:00
var user = _services.GetRequiredService<IUserIdentity>();
2025-08-06 16:27:26 +00:00
var json = JsonSerializer.Serialize(data, _options.JsonSerializerOptions);
await EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, json, nameof(WelcomeHook), callerName);
2025-02-27 22:19:43 +00:00
}
2024-03-07 18:27:29 +00:00
}