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>()
|
|
|
|
|
{
|
|
|
|
|
Gather.InputEnum.Speech
|
|
|
|
|
},
|
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-08 19:54:32 +00:00
|
|
|
public VoiceResponse ReturnInstructions(string speechPath, string callbackPath, bool actionOnEmptyResult)
|
|
|
|
|
{
|
|
|
|
|
var response = new VoiceResponse();
|
|
|
|
|
var gather = new Gather()
|
|
|
|
|
{
|
|
|
|
|
Input = new List<Gather.InputEnum>()
|
|
|
|
|
{
|
|
|
|
|
Gather.InputEnum.Speech
|
|
|
|
|
},
|
|
|
|
|
Action = new Uri($"{_settings.CallbackHost}/{callbackPath}"),
|
2024-08-09 03:28:39 +00:00
|
|
|
SpeechTimeout = "3",
|
2024-08-08 19:54:32 +00:00
|
|
|
ActionOnEmptyResult = actionOnEmptyResult
|
|
|
|
|
};
|
|
|
|
|
if (!string.IsNullOrEmpty(speechPath))
|
|
|
|
|
{
|
|
|
|
|
gather.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
|
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
Input = new List<Gather.InputEnum>() { Gather.InputEnum.Speech },
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|