using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts; using Twilio.Rest.Api.V2010.Account; using Task = System.Threading.Tasks.Task; namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions; public class HangupPhoneCallFn : IFunctionCallback { private readonly IServiceProvider _services; private readonly ILogger _logger; public string Name => "util-twilio-hangup_phone_call"; public string Indication => "Hangup"; public HangupPhoneCallFn( IServiceProvider services, ILogger logger) { _services = services; _logger = logger; } public async Task Execute(RoleDialogModel message) { var args = JsonSerializer.Deserialize(message.FunctionArgs); var states = _services.GetRequiredService(); 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; } message.Content = args.GoodbyeMessage; _ = 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 ); message.Content = "The call has been ended."; message.StopCompletion = true; }); return true; } }