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

61 lines
1.8 KiB
C#
Raw Normal View History

2024-12-09 00:29:04 +00:00
using BotSharp.Abstraction.Crontab.Models;
2024-12-05 17:11:16 +00:00
using BotSharp.Core.Crontab.Abstraction;
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.Plugin.ChatHub.Hooks;
public class ChatHubCrontabHook : ICrontabHook
{
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly IUserIdentity _user;
private readonly IConversationStorage _storage;
private readonly BotSharpOptions _options;
2025-02-13 20:52:46 +00:00
private readonly ChatHubSettings _settings;
#region Events
private const string GENERATE_NOTIFICATION = "OnNotificationGenerated";
#endregion
2024-12-05 17:11:16 +00:00
public ChatHubCrontabHook(IServiceProvider services,
IHubContext<SignalRHub> chatHub,
IUserIdentity user,
IConversationStorage storage,
2025-02-13 20:52:46 +00:00
BotSharpOptions options,
ChatHubSettings settings)
2024-12-05 17:11:16 +00:00
{
_services = services;
_chatHub = chatHub;
_user = user;
_storage = storage;
_options = options;
2025-02-13 20:52:46 +00:00
_settings = settings;
2024-12-05 17:11:16 +00:00
}
public async Task OnCronTriggered(CrontabItem item)
{
var json = JsonSerializer.Serialize(new ChatResponseModel()
{
ConversationId = item.ConversationId,
MessageId = Guid.NewGuid().ToString(),
Text = item.ExecutionResult,
Function = "",
Sender = new UserViewModel()
{
FirstName = "Crontab",
LastName = "AI",
Role = AgentRole.Assistant
}
}, _options.JsonSerializerOptions);
2025-02-13 20:52:46 +00:00
if (_settings.EventDispatchBy == EventDispatchType.Group)
{
await _chatHub.Clients.Group(item.ConversationId).SendAsync(GENERATE_NOTIFICATION, json);
}
else
{
await _chatHub.Clients.User(item.UserId).SendAsync(GENERATE_NOTIFICATION, json);
}
2024-12-05 17:11:16 +00:00
}
}