util-twilio-text_message

This commit is contained in:
Haiping Chen 2025-03-14 16:44:19 -05:00
parent addb1b1bba
commit ace2e906f0
6 changed files with 75 additions and 2 deletions

View file

@ -13,6 +13,9 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-hangup_phone_call.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-text_message.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-outbound_phone_call.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -1,5 +1,4 @@
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
using Microsoft.VisualBasic;
using Twilio.Rest.Api.V2010.Account;
using Task = System.Threading.Tasks.Task;

View file

@ -0,0 +1,49 @@
using BotSharp.Abstraction.Options;
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
using Twilio.Rest.Api.V2010.Account;
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;
public class TextMessageFn : IFunctionCallback
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private readonly TwilioSetting _twilioSetting;
public string Name => "util-twilio-text_message";
public string Indication => "Sending text message";
public TextMessageFn(
IServiceProvider services,
ILogger<TextMessageFn> logger,
TwilioSetting twilioSetting)
{
_services = services;
_logger = logger;
_twilioSetting = twilioSetting;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, BotSharpOptions.defaultJsonOptions);
// Send the message
var twilioMessage = MessageResource.Create(
to: args.PhoneNumber,
from: _twilioSetting.MessagingShortCode,
body: args.InitialMessage
);
if (twilioMessage.Status == MessageResource.StatusEnum.Queued)
{
message.Content = $"Queued message to {args.PhoneNumber}: {args.InitialMessage} [MESSAGING SID: {twilioMessage.Sid}]";
message.StopCompletion = true;
}
else
{
message.Content = twilioMessage.ErrorMessage;
}
return true;
}
}

View file

@ -8,6 +8,7 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
private static string PREFIX = "util-twilio-";
private static string OUTBOUND_PHONE_CALL_FN = $"{PREFIX}outbound_phone_call";
private static string HANGUP_PHONE_CALL_FN = $"{PREFIX}hangup_phone_call";
public static string TEXT_MESSAGE_FN = $"{PREFIX}text_message";
public void AddUtilities(List<AgentUtility> utilities)
{
@ -17,7 +18,8 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
Functions =
[
new($"{OUTBOUND_PHONE_CALL_FN}"),
new($"{HANGUP_PHONE_CALL_FN}")
new($"{HANGUP_PHONE_CALL_FN}"),
new($"{TEXT_MESSAGE_FN}")
],
Templates =
[

View file

@ -18,6 +18,8 @@ public class TwilioSetting
public string ApiSecret { get; set; }
public string CallbackHost { get; set; }
public string? MessagingShortCode { get; set; }
/// <summary>
/// Default Agent Id to handle inbound phone call
/// </summary>

View file

@ -0,0 +1,18 @@
{
"name": "util-twilio-text_message",
"description": "If the user wants to send SMS message to a phone.",
"parameters": {
"type": "object",
"properties": {
"phone_number": {
"type": "string",
"description": "The phone number which will receive message. It needs to be a valid phone number starting with +1."
},
"initial_message": {
"type": "string",
"description": "The initial message which will be sent."
}
},
"required": [ "phone_number", "initial_message" ]
}
}