Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Plugin.Twilio.Interfaces;
using BotSharp.Plugin.Twilio.Models;
using Twilio.Rest.Api.V2010.Account;
using BotSharp.Plugin.Twilio.Services;
using Task = System.Threading.Tasks.Task;

namespace BotSharp.Plugin.Twilio.Hooks;
Expand Down Expand Up @@ -57,10 +57,7 @@ public override async Task OnFunctionExecuted(RoleDialogModel message, InvokeFun
// Save all states before reconnect
await states.Save();

CallResource.Update(
pathSid: sid,
url: new Uri(processUrl));

await _services.GetRequiredService<TwilioService>().TryRedirectCallAsync(sid, processUrl);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
using BotSharp.Plugin.Twilio.Services;
using Twilio.Rest.Api.V2010.Account;

namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;

Expand Down Expand Up @@ -42,20 +41,18 @@ public async Task<bool> Execute(RoleDialogModel message)
}

var processUrl = $"{_twilioSetting.CallbackHost}/twilio/voice/hang-up?agent-id={message.CurrentAgentId}&conversation-id={conversationId}";
var twilio = _services.GetRequiredService<TwilioService>();

// Generate initial assistant audio
if (!string.IsNullOrEmpty(args.ResponseContent) && _twilioSetting.GenerateEndingAudio)
{
var twilio = _services.GetRequiredService<TwilioService>();
var initAudioFile = await twilio.SaveSpeechAsync(conversationId, "ending", args.ResponseContent);

processUrl += $"&init-audio-file={initAudioFile}";
}

var call = CallResource.Update(
url: new Uri(processUrl),
pathSid: callSid
);
// A call that already ended is what hanging up wants, so it still counts as hung up
await twilio.TryRedirectCallAsync(callSid, processUrl);

message.Content = args.Reason;
message.StopCompletion = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
using BotSharp.Plugin.Twilio.Services;
using Twilio.Rest.Api.V2010.Account;

namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;

Expand Down Expand Up @@ -47,19 +46,22 @@ public async Task<bool> Execute(RoleDialogModel message)
var conversationId = routing.Context.ConversationId;
var processUrl = $"{_twilioSetting.CallbackHost}/twilio/voice/transfer-call?agent-id={routing.Context.EntryAgentId}&conversation-id={conversationId}&transfer-to={args.PhoneNumber}";

var twilio = _services.GetRequiredService<TwilioService>();

// Generate initial assistant audio
if (!string.IsNullOrEmpty(args.TransitionMessage))
{
var twilio = _services.GetRequiredService<TwilioService>();
var initAudioFile = await twilio.SaveSpeechAsync(conversationId, "transfer", args.TransitionMessage);

processUrl += $"&init-audio-file={initAudioFile}";
}

// Transfer call
var call = CallResource.Update(
pathSid: sid,
url: new Uri(processUrl));
if (!await twilio.TryRedirectCallAsync(sid, processUrl))
{
message.Content = "The phone call has already ended, so it was not transferred.";
return false;
}

message.Content = args.TransitionMessage;
return true;
Expand Down
26 changes: 26 additions & 0 deletions src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using BotSharp.Plugin.Twilio.Interfaces;
using BotSharp.Plugin.Twilio.Models;
using BotSharp.Plugin.Twilio.Models.Relay;
using Twilio.Exceptions;
using Twilio.Jwt.AccessToken;
using CallResource = Twilio.Rest.Api.V2010.Account.CallResource;
using Twilio.Rest.Api.V2010.Account.Call;
using Task = System.Threading.Tasks.Task;
using Token = Twilio.Jwt.AccessToken.Token;
Expand Down Expand Up @@ -423,6 +425,30 @@ public async Task<string> SaveSpeechAsync(string conversationId, string name, st
return fileName;
}

/// <summary>
/// Points a live call at new TwiML. Returns false when the call is no longer in progress (Twilio 21220): the caller
/// already hung up, or the message did not come from a live call at all (e.g. a phone conversation continued over
/// the REST API). Any other Twilio error is thrown.
/// </summary>
public async Task<bool> TryRedirectCallAsync(string callSid, string url)
{
try
{
await CallResource.UpdateAsync(pathSid: callSid, url: new Uri(url));
return true;
}
catch (ApiException ex) when (ex.Code == CallNotInProgressErrorCode)
{
_logger.LogInformation($"Call {callSid} is no longer in progress, not redirected: {ex.Message}");
return false;
}
}

/// <summary>
/// Twilio's "Call is not in-progress. Cannot redirect.",see https://www.twilio.com/docs/api/errors/21220
/// </summary>
private const int CallNotInProgressErrorCode = 21220;

/// <summary>
/// Returns the file name of a speech saved by SaveSpeechAsync, whichever mode it was saved in.
/// </summary>
Expand Down
Loading