BotSharp/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/HangupPhoneCallFn.cs

60 lines
1.8 KiB
C#
Raw Normal View History

2025-03-15 02:06:40 +00:00
using BotSharp.Abstraction.Routing;
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;
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;
public class HangupPhoneCallFn : IFunctionCallback
{
private readonly IServiceProvider _services;
private readonly ILogger<HangupPhoneCallFn> _logger;
2025-03-15 02:06:40 +00:00
private readonly TwilioSetting _twilioSetting;
2025-02-26 17:41:52 +00:00
public string Name => "util-twilio-hangup_phone_call";
public string Indication => "Hangup";
public HangupPhoneCallFn(
IServiceProvider services,
2025-03-15 02:06:40 +00:00
ILogger<HangupPhoneCallFn> logger,
TwilioSetting twilioSetting)
2025-02-26 17:41:52 +00:00
{
_services = services;
_logger = logger;
2025-03-15 02:06:40 +00:00
_twilioSetting = twilioSetting;
2025-02-26 17:41:52 +00:00
}
public async Task<bool> Execute(RoleDialogModel message)
{
2025-03-11 22:20:44 +00:00
var args = JsonSerializer.Deserialize<HangupPhoneCallArgs>(message.FunctionArgs);
2025-03-15 02:06:40 +00:00
var routing = _services.GetRequiredService<IRoutingService>();
var conversationId = routing.Context.ConversationId;
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-15 02:06:40 +00:00
if (args.AnythingElseToHelp)
{
message.Content = "Tell me how I can help.";
}
else
2025-03-11 22:20:44 +00:00
{
var call = CallResource.Update(
2025-03-15 02:06:40 +00:00
url: new Uri($"{_twilioSetting.CallbackHost}/twilio/voice/hang-up?conversation-id={conversationId}"),
2025-03-11 22:20:44 +00:00
pathSid: callSid
);
2025-03-15 02:06:40 +00:00
message.Content = "The call is ending.";
2025-03-11 22:20:44 +00:00
message.StopCompletion = true;
2025-03-15 02:06:40 +00:00
}
2025-02-26 17:41:52 +00:00
return true;
}
}