2025-03-11 22:20:44 +00:00
|
|
|
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
2025-02-26 17:41:52 +00:00
|
|
|
using Twilio.Rest.Api.V2010.Account;
|
2025-03-11 22:20:44 +00:00
|
|
|
using Task = System.Threading.Tasks.Task;
|
2025-02-26 17:41:52 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;
|
|
|
|
|
|
|
|
|
|
public class HangupPhoneCallFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger<HangupPhoneCallFn> _logger;
|
|
|
|
|
|
|
|
|
|
public string Name => "util-twilio-hangup_phone_call";
|
|
|
|
|
public string Indication => "Hangup";
|
|
|
|
|
|
|
|
|
|
public HangupPhoneCallFn(
|
|
|
|
|
IServiceProvider services,
|
|
|
|
|
ILogger<HangupPhoneCallFn> logger)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
2025-03-11 22:20:44 +00:00
|
|
|
var args = JsonSerializer.Deserialize<HangupPhoneCallArgs>(message.FunctionArgs);
|
2025-02-26 19:16:57 +00:00
|
|
|
var states = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var callSid = states.GetState("twilio_call_sid");
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(callSid))
|
|
|
|
|
{
|
|
|
|
|
message.Content = "The call has not been initiated.";
|
|
|
|
|
_logger.LogError(message.Content);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 22:20:44 +00:00
|
|
|
message.Content = args.GoodbyeMessage;
|
2025-02-26 17:41:52 +00:00
|
|
|
|
2025-03-11 22:20:44 +00:00
|
|
|
_ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(args.GoodbyeMessage.Split(' ').Length * 400);
|
|
|
|
|
// Have to find the SID by the phone number
|
|
|
|
|
var call = CallResource.Update(
|
|
|
|
|
status: CallResource.UpdateStatusEnum.Completed,
|
|
|
|
|
pathSid: callSid
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-12 19:03:38 +00:00
|
|
|
message.Content = "The call has been ended.";
|
2025-03-11 22:20:44 +00:00
|
|
|
message.StopCompletion = true;
|
|
|
|
|
});
|
2025-02-26 17:41:52 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|