BotSharp/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs

108 lines
4 KiB
C#
Raw Normal View History

2024-08-08 19:54:32 +00:00
using BotSharp.Abstraction.Routing;
using BotSharp.Plugin.Twilio.Models;
using Microsoft.Extensions.Hosting;
using System.Threading;
using Task = System.Threading.Tasks.Task;
namespace BotSharp.Plugin.Twilio.Services
{
public class TwilioMessageQueueService : BackgroundService
{
private readonly TwilioMessageQueue _queue;
private readonly IServiceProvider _serviceProvider;
private readonly SemaphoreSlim _throttler;
public TwilioMessageQueueService(
TwilioMessageQueue queue,
IServiceProvider serviceProvider)
{
_queue = queue;
_serviceProvider = serviceProvider;
_throttler = new SemaphoreSlim(4, 4);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await foreach (var message in _queue.Reader.ReadAllAsync(stoppingToken))
{
await _throttler.WaitAsync(stoppingToken);
_ = Task.Run(async () =>
{
try
{
2024-08-09 03:28:39 +00:00
Console.WriteLine($"Start processing {message}.");
2024-08-08 19:54:32 +00:00
await ProcessUserMessageAsync(message);
}
catch (Exception ex)
{
2024-08-09 03:28:39 +00:00
Console.WriteLine($"Processing {message} failed due to {ex.Message}.");
2024-08-08 19:54:32 +00:00
}
finally
{
_throttler.Release();
}
});
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_queue.Stop();
await base.StopAsync(cancellationToken);
}
private async Task ProcessUserMessageAsync(CallerMessage message)
{
using var scope = _serviceProvider.CreateScope();
var sp = scope.ServiceProvider;
2024-08-14 21:44:47 +00:00
AssistantMessage reply = null;
2024-08-08 19:54:32 +00:00
var inputMsg = new RoleDialogModel(AgentRole.User, message.Content);
var conv = sp.GetRequiredService<IConversationService>();
var routing = sp.GetRequiredService<IRoutingService>();
2024-08-09 03:28:39 +00:00
var config = sp.GetRequiredService<TwilioSetting>();
2024-08-14 21:44:47 +00:00
routing.Context.SetMessageId(message.ConversationId, inputMsg.MessageId);
var states = new List<MessageState>
2024-08-08 19:54:32 +00:00
{
new MessageState("channel", ConversationChannel.Phone),
new MessageState("calling_phone", message.From)
2024-08-14 21:44:47 +00:00
};
foreach (var kvp in message.States)
{
states.Add(new MessageState(kvp.Key, kvp.Value));
}
conv.SetConversationId(message.ConversationId, states);
var sessionManager = sp.GetRequiredService<ITwilioSessionManager>();
2024-08-09 03:28:39 +00:00
var result = await conv.SendMessage(config.AgentId,
2024-08-08 19:54:32 +00:00
inputMsg,
replyMessage: null,
async msg =>
{
2024-08-14 21:44:47 +00:00
reply = new AssistantMessage()
{
ConversationEnd = msg.Instruction.ConversationEnd,
Content = msg.Content
};
},
async msg =>
{
if (!string.IsNullOrEmpty(msg.Indication))
{
await sessionManager.SetReplyIndicationAsync(message.ConversationId, message.SeqNumber, msg.Indication);
}
2024-08-08 19:54:32 +00:00
},
async functionExecuted =>
{ }
);
2024-08-14 21:44:47 +00:00
if (reply == null || string.IsNullOrWhiteSpace(reply.Content))
2024-08-08 19:54:32 +00:00
{
2024-08-14 21:44:47 +00:00
reply = new AssistantMessage()
{
ConversationEnd = true,
Content = "Sorry, something was wrong."
};
}
await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply);
2024-08-08 19:54:32 +00:00
}
}
}