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;
|
2025-05-07 21:34:39 +00:00
|
|
|
using BotSharp.Abstraction.Crontab;
|
2024-12-09 00:29:04 +00:00
|
|
|
using BotSharp.Abstraction.Crontab.Models;
|
2024-12-05 17:11:16 +00:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.ChatHub.Hooks;
|
|
|
|
|
|
|
|
|
|
public class ChatHubCrontabHook : ICrontabHook
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IHubContext<SignalRHub> _chatHub;
|
2025-02-27 22:19:43 +00:00
|
|
|
private readonly ILogger<ChatHubCrontabHook> _logger;
|
2024-12-05 17:11:16 +00:00
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
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 ChatHubCrontabHook(
|
|
|
|
|
IServiceProvider services,
|
2024-12-05 17:11:16 +00:00
|
|
|
IHubContext<SignalRHub> chatHub,
|
2025-02-27 22:19:43 +00:00
|
|
|
ILogger<ChatHubCrontabHook> logger,
|
2024-12-05 17:11:16 +00:00
|
|
|
IUserIdentity user,
|
2025-02-13 20:52:46 +00:00
|
|
|
BotSharpOptions options,
|
|
|
|
|
ChatHubSettings settings)
|
2024-12-05 17:11:16 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_chatHub = chatHub;
|
2025-02-27 22:19:43 +00:00
|
|
|
_logger = logger;
|
2024-12-05 17:11:16 +00:00
|
|
|
_user = user;
|
|
|
|
|
_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)
|
|
|
|
|
{
|
2025-07-28 22:41:28 +00:00
|
|
|
var data = new ChatResponseDto()
|
2024-12-05 17:11:16 +00:00
|
|
|
{
|
|
|
|
|
ConversationId = item.ConversationId,
|
|
|
|
|
MessageId = Guid.NewGuid().ToString(),
|
|
|
|
|
Text = item.ExecutionResult,
|
|
|
|
|
Function = "",
|
2025-05-07 21:34:39 +00:00
|
|
|
Sender = new()
|
2024-12-05 17:11:16 +00:00
|
|
|
{
|
|
|
|
|
FirstName = "Crontab",
|
|
|
|
|
LastName = "AI",
|
|
|
|
|
Role = AgentRole.Assistant
|
|
|
|
|
}
|
2025-07-28 22:41:28 +00:00
|
|
|
};
|
2024-12-05 17:11:16 +00:00
|
|
|
|
2025-07-28 22:41:28 +00:00
|
|
|
await SendEvent(item, data);
|
2025-02-27 22:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 22:41:28 +00:00
|
|
|
private async Task SendEvent(CrontabItem item, ChatResponseDto data)
|
2025-02-27 22:19:43 +00:00
|
|
|
{
|
|
|
|
|
try
|
2025-02-13 20:52:46 +00:00
|
|
|
{
|
2025-07-28 22:41:28 +00:00
|
|
|
await _chatHub.Clients.User(item.UserId).SendAsync(ChatEvent.OnNotificationGenerated, data);
|
2025-02-13 20:52:46 +00:00
|
|
|
}
|
2025-03-12 15:13:57 +00:00
|
|
|
catch { }
|
2024-12-05 17:11:16 +00:00
|
|
|
}
|
|
|
|
|
}
|