add twilio outbound phone call utility

This commit is contained in:
Bo Yin 2024-11-19 13:05:12 -06:00
parent b070df43c8
commit 7f7bd80aa6
16 changed files with 143 additions and 89 deletions

View file

@ -8,6 +8,20 @@
<OutputPath>$(SolutionDir)packages</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\twilio_outbound_phone_call.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\twilio_outbound_phone_call.fn.liquid" />
</ItemGroup>
<ItemGroup>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\twilio_outbound_phone_call.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\twilio_outbound_phone_call.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.7.27" />
<PackageReference Include="StrongGrid" Version="0.108.0" />

View file

@ -6,6 +6,7 @@ using BotSharp.Plugin.Twilio.Models;
using BotSharp.Plugin.Twilio.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Twilio.Http;
namespace BotSharp.Plugin.Twilio.Controllers;
@ -373,6 +374,24 @@ public class TwilioVoiceController : TwilioController
return TwiML(response);
}
[ValidateRequest]
[HttpPost("twilio/voice/init-call")]
public TwiMLResult InitiateOutboundCall(VoiceRequest request, [Required][FromQuery] string conversationId)
{
var instruction = new ConversationalVoiceResponse
{
ActionOnEmptyResult = true,
CallbackPath = $"twilio/voice/{conversationId}/receive/1",
SpeechPaths = new List<string>
{
$"twilio/voice/speeches/{conversationId}/intial.mp3"
}
};
var twilio = _services.GetRequiredService<TwilioService>();
var response = twilio.ReturnNoninterruptedInstructions(instruction);
return TwiML(response);
}
[ValidateRequest]
[HttpGet("twilio/voice/speeches/{conversationId}/{fileName}")]
public async Task<FileContentResult> GetSpeechFile([FromRoute] string conversationId, [FromRoute] string fileName)

View file

@ -1,7 +0,0 @@
namespace BotSharp.Plugin.Twilio.OutboundCallHandler.Enums
{
public class UtilityName
{
public const string TwilioOutboundCaller = "twilio-outbound-caller";
}
}

View file

@ -1,34 +0,0 @@
using BotSharp.Abstraction.Options;
using BotSharp.Plugin.Twilio.OutboundCallHandler.LlmContexts;
namespace BotSharp.Plugin.Twilio.OutboundCallHandler.Functions
{
public class HandleOutboundCallFn : IFunctionCallback
{
private readonly IServiceProvider _services;
private readonly ILogger<HandleOutboundCallFn> _logger;
private readonly BotSharpOptions _options;
private readonly TwilioSetting _twilioSetting;
public string Name => "handle_outbound_call";
public string Indication => "Dialing the number";
public HandleOutboundCallFn(
IServiceProvider services,
ILogger<HandleOutboundCallFn> logger,
BotSharpOptions options,
TwilioSetting twilioSetting)
{
_services = services;
_logger = logger;
_options = options;
_twilioSetting = twilioSetting;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
return true;
}
}
}

View file

@ -1,12 +0,0 @@
using BotSharp.Plugin.Twilio.OutboundCallHandler.Enums;
namespace BotSharp.Plugin.Twilio.OutboundCallHandler.Hooks
{
public class OutboundCallHandlerUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(UtilityName.TwilioOutboundCaller);
}
}
}

View file

@ -1,18 +0,0 @@
using BotSharp.Plugin.Twilio.OutboundCallHandler.Hooks;
namespace BotSharp.Plugin.Twilio.OutboundCallHandler
{
public class OutboundCallHandlerPlugin : IBotSharpPlugin
{
public string Id => "901ec364-01da-4602-944f-2bc49e03b8b2";
public string Name => "Outbound Call Handler";
public string Description => "Empower agent to make outbound call via Twilio";
public string IconUrl => "https://cdn-icons-png.freepik.com/512/6711/6711567.png";
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IAgentHook, OutboundCallHandlerHook>();
services.AddScoped<IAgentUtilityHook, OutboundCallHandlerUtilityHook>();
}
}
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Enums
{
public class UtilityName
{
public const string OutboundPhoneCall = "twilio-outbound-phone-call";
}
}

View file

@ -0,0 +1,68 @@
using BotSharp.Abstraction.Files;
using BotSharp.Abstraction.Options;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.Twilio.Interfaces;
using BotSharp.Plugin.Twilio.Models;
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions
{
public class HandleOutboundPhoneCallFn : IFunctionCallback
{
private readonly IServiceProvider _services;
private readonly ILogger<HandleOutboundPhoneCallFn> _logger;
private readonly BotSharpOptions _options;
private readonly TwilioSetting _twilioSetting;
public string Name => "twilio_outbound_phone_call";
public string Indication => "Dialing the number";
public HandleOutboundPhoneCallFn(
IServiceProvider services,
ILogger<HandleOutboundPhoneCallFn> logger,
BotSharpOptions options,
TwilioSetting twilioSetting)
{
_services = services;
_logger = logger;
_options = options;
_twilioSetting = twilioSetting;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
if (args.PhoneNumber.Length != 12 || !args.PhoneNumber.StartsWith("+1", StringComparison.OrdinalIgnoreCase))
{
_logger.LogError("Invalid phone number format: {phone}", args.PhoneNumber);
return false;
}
if (string.IsNullOrWhiteSpace(args.InitialMessage))
{
_logger.LogError("Initial message is empty.");
return false;
}
var completion = CompletionProvider.GetAudioCompletion(_services, "openai", "tts-1");
var fileStorage = _services.GetRequiredService<IFileStorageService>();
var data = await completion.GenerateAudioFromTextAsync(args.InitialMessage);
var conversationId = Guid.NewGuid().ToString();
var fileName = $"intial.mp3";
fileStorage.SaveSpeechFile(conversationId, fileName, data);
// TODO: Add initial message in the new conversation
var sessionManager = _services.GetRequiredService<ITwilioSessionManager>();
await sessionManager.SetAssistantReplyAsync(conversationId, 0, new AssistantMessage
{
Content = args.InitialMessage,
SpeechFileName = fileName
});
var call = await CallResource.CreateAsync(
url: new Uri($"{_twilioSetting.CallbackHost}/twilio/voice/init-call?conversationId={conversationId}"),
to: new PhoneNumber(args.PhoneNumber),
from: new PhoneNumber(_twilioSetting.PhoneNumber));
message.StopCompletion = true;
return true;
}
}
}

View file

@ -2,17 +2,17 @@ using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Utilities;
using BotSharp.Plugin.Twilio.OutboundCallHandler.Enums;
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Enums;
namespace BotSharp.Plugin.Twilio.OutboundCallHandler.Hooks
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks
{
internal class OutboundCallHandlerHook : AgentHookBase
internal class OutboundPhoneCallHandlerHook : AgentHookBase
{
private static string FUNCTION_NAME = "handle_outbound_call";
private static string FUNCTION_NAME = "twilio_outbound_phone_call";
public override string SelfId => string.Empty;
public OutboundCallHandlerHook(IServiceProvider services, AgentSettings settings) : base(services, settings)
public OutboundPhoneCallHandlerHook(IServiceProvider services, AgentSettings settings) : base(services, settings)
{
}
@ -20,7 +20,7 @@ namespace BotSharp.Plugin.Twilio.OutboundCallHandler.Hooks
{
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.TwilioOutboundCaller);
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.OutboundPhoneCall);
if (isConvMode && isEnabled)
{

View file

@ -0,0 +1,12 @@
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Enums;
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks
{
public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(UtilityName.OutboundPhoneCall);
}
}
}

View file

@ -1,13 +1,13 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.Twilio.OutboundCallHandler.LlmContexts
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
{
public class LlmContextIn
{
[JsonPropertyName("phone_number")]
public string PhoneNumber { get; set; }
[JsonPropertyName("first_message")]
public string FirstMessage { get; set; }
[JsonPropertyName("initial_message")]
public string InitialMessage { get; set; }
}
}

View file

@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.Twilio.OutboundCallHandler.LlmContexts
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
{
public class LlmContextOut
{

View file

@ -99,6 +99,7 @@ public class TwilioService
public VoiceResponse ReturnNoninterruptedInstructions(ConversationalVoiceResponse conversationalVoiceResponse)
{
var response = new VoiceResponse();
response.Pause(2);
if (conversationalVoiceResponse.SpeechPaths != null && conversationalVoiceResponse.SpeechPaths.Any())
{
foreach (var speechPath in conversationalVoiceResponse.SpeechPaths)
@ -152,8 +153,8 @@ public class TwilioService
var response = new VoiceResponse();
var gather = new Gather()
{
Input = new List<Gather.InputEnum>()
{
Input = new List<Gather.InputEnum>()
{
Gather.InputEnum.Speech,
Gather.InputEnum.Dtmf
},

View file

@ -1,7 +1,9 @@
using BotSharp.Abstraction.Settings;
using BotSharp.Plugin.Twilio.Interfaces;
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks;
using BotSharp.Plugin.Twilio.Services;
using StackExchange.Redis;
using Twilio;
namespace BotSharp.Plugin.Twilio;
@ -18,7 +20,7 @@ public class TwilioPlugin : IBotSharpPlugin
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<TwilioSetting>("Twilio");
});
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:AuthToken"]);
services.AddScoped<TwilioService>();
var conn = ConnectionMultiplexer.Connect(config["Twilio:RedisConnectionString"]);
@ -28,5 +30,7 @@ public class TwilioPlugin : IBotSharpPlugin
services.AddSingleton<TwilioMessageQueue>();
services.AddHostedService<TwilioMessageQueueService>();
services.AddTwilioRequestValidation();
services.AddScoped<IAgentHook, OutboundPhoneCallHandlerHook>();
services.AddScoped<IAgentUtilityHook, OutboundPhoneCallHandlerUtilityHook>();
}
}

View file

@ -1,6 +1,6 @@
{
"name": "handle_outbound_call",
"description": "If the user wants to initiate a phone call, you need to capture the phone number and the message the users wants to send. Then call this function to make an outbound call via Twilio.",
"name": "twilio_outbound_phone_call",
"description": "If the user wants to initiate a phone call, you need to capture the phone number and compose the message the users wants to send. Then call this function to make an outbound call via Twilio.",
"parameters": {
"type": "object",
"properties": {
@ -8,11 +8,11 @@
"to_read": "string",
"description": "The phone number which will be dialed. It needs to be a valid phone number starting with +1."
},
"first_message": {
"initial_message": {
"to_read": "string",
"description": "The first message which will be sent."
"description": "The initial message which will be sent."
}
},
"required": [ "phone_number", "first_message" ]
"required": [ "phone_number", "initial_message" ]
}
}