BotSharp/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs

210 lines
7 KiB
C#
Raw Normal View History

2024-08-30 16:29:21 +00:00
using BotSharp.Abstraction.Utilities;
2024-11-12 22:51:33 +00:00
using BotSharp.Plugin.Twilio.Models;
2023-12-06 22:55:55 +00:00
using Twilio.Jwt.AccessToken;
2025-02-07 17:18:47 +00:00
using Twilio.TwiML.Messaging;
2023-12-06 22:55:55 +00:00
using Token = Twilio.Jwt.AccessToken.Token;
namespace BotSharp.Plugin.Twilio.Services;
/// <summary>
/// https://github.com/TwilioDevEd/voice-javascript-sdk-quickstart-csharp
/// </summary>
public class TwilioService
{
private readonly TwilioSetting _settings;
private readonly IServiceProvider _services;
public TwilioService(TwilioSetting settings, IServiceProvider services)
{
_settings = settings;
_services = services;
}
public string GetAccessToken()
{
// These are specific to Voice
var user = _services.GetRequiredService<IUserIdentity>();
// Create a Voice grant for this token
var grant = new VoiceGrant();
grant.OutgoingApplicationSid = _settings.AppSID;
// Optional: add to allow incoming calls
grant.IncomingAllow = true;
var grants = new HashSet<IGrant>
{
{ grant }
};
// Create an Access Token generator
var token = new Token(
_settings.AccountSID,
_settings.ApiKeySID,
_settings.ApiSecret,
user.Id,
grants: grants);
return token.ToJwt();
}
public VoiceResponse ReturnInstructions(string message)
{
2024-01-13 21:17:40 +00:00
var twilioSetting = _services.GetRequiredService<TwilioSetting>();
2023-12-06 22:55:55 +00:00
var response = new VoiceResponse();
var gather = new Gather()
{
Input = new List<Gather.InputEnum>()
{
2024-08-26 22:33:47 +00:00
Gather.InputEnum.Speech,
Gather.InputEnum.Dtmf
2023-12-06 22:55:55 +00:00
},
2025-01-14 17:11:42 +00:00
Action = new Uri($"{_settings.CallbackHost}/twilio/voice/{twilioSetting.AgentId}"),
Enhanced = true,
SpeechModel = Gather.SpeechModelEnum.PhoneCall,
SpeechTimeout = "auto"
2023-12-06 22:55:55 +00:00
};
2024-08-28 20:34:41 +00:00
2023-12-06 22:55:55 +00:00
gather.Say(message);
response.Append(gather);
return response;
}
2024-11-12 22:51:33 +00:00
public VoiceResponse ReturnInstructions(ConversationalVoiceResponse conversationalVoiceResponse)
2024-08-08 19:54:32 +00:00
{
var response = new VoiceResponse();
var gather = new Gather()
{
Input = new List<Gather.InputEnum>()
{
2024-08-26 22:33:47 +00:00
Gather.InputEnum.Speech,
Gather.InputEnum.Dtmf
2024-08-08 19:54:32 +00:00
},
2024-11-12 22:51:33 +00:00
Action = new Uri($"{_settings.CallbackHost}/{conversationalVoiceResponse.CallbackPath}"),
2025-01-14 17:11:42 +00:00
Enhanced = true,
2024-08-14 21:44:47 +00:00
SpeechModel = Gather.SpeechModelEnum.PhoneCall,
2024-08-30 21:19:05 +00:00
SpeechTimeout = "auto", // timeout > 0 ? timeout.ToString() : "3",
2024-11-12 22:51:33 +00:00
Timeout = conversationalVoiceResponse.Timeout > 0 ? conversationalVoiceResponse.Timeout : 3,
ActionOnEmptyResult = conversationalVoiceResponse.ActionOnEmptyResult,
Hints = conversationalVoiceResponse.Hints
2024-08-08 19:54:32 +00:00
};
2024-08-30 16:29:21 +00:00
2024-11-12 22:51:33 +00:00
if (!conversationalVoiceResponse.SpeechPaths.IsNullOrEmpty())
2024-08-08 19:54:32 +00:00
{
2024-11-12 22:51:33 +00:00
foreach (var speechPath in conversationalVoiceResponse.SpeechPaths)
2024-08-22 21:13:38 +00:00
{
2024-08-28 20:03:23 +00:00
gather.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
2024-08-22 21:13:38 +00:00
}
2024-08-08 19:54:32 +00:00
}
response.Append(gather);
return response;
}
2024-11-12 22:51:33 +00:00
public VoiceResponse ReturnNoninterruptedInstructions(ConversationalVoiceResponse conversationalVoiceResponse)
2024-08-29 19:36:07 +00:00
{
var response = new VoiceResponse();
2024-11-19 19:05:12 +00:00
response.Pause(2);
2024-11-12 22:51:33 +00:00
if (conversationalVoiceResponse.SpeechPaths != null && conversationalVoiceResponse.SpeechPaths.Any())
2024-08-29 19:36:07 +00:00
{
2024-11-12 22:51:33 +00:00
foreach (var speechPath in conversationalVoiceResponse.SpeechPaths)
2024-08-29 19:36:07 +00:00
{
response.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
}
}
var gather = new Gather()
{
Input = new List<Gather.InputEnum>()
{
Gather.InputEnum.Speech,
Gather.InputEnum.Dtmf
},
2024-11-12 22:51:33 +00:00
Action = new Uri($"{_settings.CallbackHost}/{conversationalVoiceResponse.CallbackPath}"),
2025-01-14 17:11:42 +00:00
Enhanced = true,
2024-08-29 19:36:07 +00:00
SpeechModel = Gather.SpeechModelEnum.PhoneCall,
2024-11-13 21:09:39 +00:00
SpeechTimeout = "auto", // conversationalVoiceResponse.Timeout > 0 ? conversationalVoiceResponse.Timeout.ToString() : "3",
2024-11-12 22:51:33 +00:00
Timeout = conversationalVoiceResponse.Timeout > 0 ? conversationalVoiceResponse.Timeout : 3,
ActionOnEmptyResult = conversationalVoiceResponse.ActionOnEmptyResult
2024-08-29 19:36:07 +00:00
};
response.Append(gather);
return response;
}
2024-08-09 03:28:39 +00:00
public VoiceResponse HangUp(string speechPath)
2024-08-08 19:54:32 +00:00
{
var response = new VoiceResponse();
2024-08-09 03:28:39 +00:00
if (!string.IsNullOrEmpty(speechPath))
2023-12-06 22:55:55 +00:00
{
2024-08-09 03:28:39 +00:00
response.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
2023-12-06 22:55:55 +00:00
}
response.Hangup();
return response;
}
2024-09-06 15:14:10 +00:00
public VoiceResponse DialCsrAgent(string speechPath)
{
var response = new VoiceResponse();
if (!string.IsNullOrEmpty(speechPath))
{
response.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
}
response.Dial(_settings.CsrAgentNumber);
return response;
}
2023-12-06 22:55:55 +00:00
public VoiceResponse HoldOn(int interval, string message = null)
{
2024-01-13 21:17:40 +00:00
var twilioSetting = _services.GetRequiredService<TwilioSetting>();
2023-12-06 22:55:55 +00:00
var response = new VoiceResponse();
var gather = new Gather()
{
2024-11-19 19:05:12 +00:00
Input = new List<Gather.InputEnum>()
{
2024-08-26 22:33:47 +00:00
Gather.InputEnum.Speech,
Gather.InputEnum.Dtmf
},
2024-01-13 21:17:40 +00:00
Action = new Uri($"{_settings.CallbackHost}/twilio/voice/{twilioSetting.AgentId}"),
2023-12-06 22:55:55 +00:00
ActionOnEmptyResult = true
};
2024-08-28 20:34:41 +00:00
2023-12-06 22:55:55 +00:00
if (!string.IsNullOrEmpty(message))
{
gather.Say(message);
}
gather.Pause(interval);
response.Append(gather);
return response;
}
2025-02-07 17:18:47 +00:00
/// <summary>
/// Bidirectional Media Streams
/// </summary>
/// <param name="conversationalVoiceResponse"></param>
/// <returns></returns>
2025-02-10 23:28:03 +00:00
public VoiceResponse ReturnBidirectionalMediaStreamsInstructions(string conversationId, ConversationalVoiceResponse conversationalVoiceResponse)
2025-02-07 17:18:47 +00:00
{
var response = new VoiceResponse();
if (conversationalVoiceResponse.SpeechPaths != null && conversationalVoiceResponse.SpeechPaths.Any())
{
foreach (var speechPath in conversationalVoiceResponse.SpeechPaths)
{
2025-03-03 21:30:06 +00:00
if (speechPath.StartsWith("twilio/"))
{
response.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
}
else
{
response.Play(new Uri($"{_settings.CallbackHost}/twilio/voice/speeches/{conversationId}/{speechPath}"));
}
2025-02-07 17:18:47 +00:00
}
}
var connect = new Connect();
var host = _settings.CallbackHost.Split("://").Last();
2025-02-10 23:28:03 +00:00
connect.Stream(url: $"wss://{host}/twilio/stream/{conversationId}");
2025-02-07 17:18:47 +00:00
response.Append(connect);
return response;
}
2023-12-06 22:55:55 +00:00
}