Skip to content

Commit ace2e90

Browse files
author
Haiping Chen
committed
util-twilio-text_message
1 parent addb1b1 commit ace2e90

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

src/Plugins/BotSharp.Plugin.Twilio/BotSharp.Plugin.Twilio.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-hangup_phone_call.json">
1414
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1515
</Content>
16+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-text_message.json">
17+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
18+
</Content>
1619
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-outbound_phone_call.json">
1720
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1821
</Content>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
2-
using Microsoft.VisualBasic;
32
using Twilio.Rest.Api.V2010.Account;
43
using Task = System.Threading.Tasks.Task;
54

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using BotSharp.Abstraction.Options;
2+
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
3+
using Twilio.Rest.Api.V2010.Account;
4+
5+
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;
6+
7+
public class TextMessageFn : IFunctionCallback
8+
{
9+
private readonly IServiceProvider _services;
10+
private readonly ILogger _logger;
11+
private readonly TwilioSetting _twilioSetting;
12+
13+
public string Name => "util-twilio-text_message";
14+
public string Indication => "Sending text message";
15+
16+
public TextMessageFn(
17+
IServiceProvider services,
18+
ILogger<TextMessageFn> logger,
19+
TwilioSetting twilioSetting)
20+
{
21+
_services = services;
22+
_logger = logger;
23+
_twilioSetting = twilioSetting;
24+
}
25+
26+
public async Task<bool> Execute(RoleDialogModel message)
27+
{
28+
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, BotSharpOptions.defaultJsonOptions);
29+
30+
// Send the message
31+
var twilioMessage = MessageResource.Create(
32+
to: args.PhoneNumber,
33+
from: _twilioSetting.MessagingShortCode,
34+
body: args.InitialMessage
35+
);
36+
37+
if (twilioMessage.Status == MessageResource.StatusEnum.Queued)
38+
{
39+
message.Content = $"Queued message to {args.PhoneNumber}: {args.InitialMessage} [MESSAGING SID: {twilioMessage.Sid}]";
40+
message.StopCompletion = true;
41+
}
42+
else
43+
{
44+
message.Content = twilioMessage.ErrorMessage;
45+
}
46+
47+
return true;
48+
}
49+
}

src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerUtilityHook.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
88
private static string PREFIX = "util-twilio-";
99
private static string OUTBOUND_PHONE_CALL_FN = $"{PREFIX}outbound_phone_call";
1010
private static string HANGUP_PHONE_CALL_FN = $"{PREFIX}hangup_phone_call";
11+
public static string TEXT_MESSAGE_FN = $"{PREFIX}text_message";
1112

1213
public void AddUtilities(List<AgentUtility> utilities)
1314
{
@@ -17,7 +18,8 @@ public void AddUtilities(List<AgentUtility> utilities)
1718
Functions =
1819
[
1920
new($"{OUTBOUND_PHONE_CALL_FN}"),
20-
new($"{HANGUP_PHONE_CALL_FN}")
21+
new($"{HANGUP_PHONE_CALL_FN}"),
22+
new($"{TEXT_MESSAGE_FN}")
2123
],
2224
Templates =
2325
[

src/Plugins/BotSharp.Plugin.Twilio/Settings/TwilioSetting.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class TwilioSetting
1818
public string ApiSecret { get; set; }
1919
public string CallbackHost { get; set; }
2020

21+
public string? MessagingShortCode { get; set; }
22+
2123
/// <summary>
2224
/// Default Agent Id to handle inbound phone call
2325
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "util-twilio-text_message",
3+
"description": "If the user wants to send SMS message to a phone.",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
"phone_number": {
8+
"type": "string",
9+
"description": "The phone number which will receive message. It needs to be a valid phone number starting with +1."
10+
},
11+
"initial_message": {
12+
"type": "string",
13+
"description": "The initial message which will be sent."
14+
}
15+
},
16+
"required": [ "phone_number", "initial_message" ]
17+
}
18+
}

0 commit comments

Comments
 (0)