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

131 lines
3.9 KiB
C#
Raw Normal View History

2023-12-06 22:55:55 +00:00
using Twilio.Jwt.AccessToken;
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
},
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
};
gather.Say(message);
response.Append(gather);
return response;
}
2024-08-14 21:44:47 +00:00
public VoiceResponse ReturnInstructions(string speechPath, string callbackPath, bool actionOnEmptyResult, int timeout = 3)
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
},
Action = new Uri($"{_settings.CallbackHost}/{callbackPath}"),
2024-08-14 21:44:47 +00:00
SpeechModel = Gather.SpeechModelEnum.PhoneCall,
SpeechTimeout = timeout > 0 ? timeout.ToString() : "3",
Timeout = timeout > 0 ? timeout : 3,
2024-08-08 19:54:32 +00:00
ActionOnEmptyResult = actionOnEmptyResult
};
if (!string.IsNullOrEmpty(speechPath))
{
gather.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
2024-08-22 21:13:38 +00:00
if (speechPath.Contains("hold-on-"))
{
int audioIndex = Random.Shared.Next(1, 4);
gather.Play(new Uri($"{_settings.CallbackHost}/twilio/typing-{audioIndex}.mp3"));
}
2024-08-08 19:54:32 +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;
}
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-08-26 22:33:47 +00:00
Input = new List<Gather.InputEnum>()
{
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
};
if (!string.IsNullOrEmpty(message))
{
gather.Say(message);
}
gather.Pause(interval);
response.Append(gather);
return response;
}
}