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

98 lines
2.7 KiB
C#
Raw Normal View History

2024-01-13 21:17:40 +00:00
using BotSharp.Plugin.Twilio.Settings;
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;
}
public VoiceResponse HangUp(string message)
{
var response = new VoiceResponse();
if (!string.IsNullOrEmpty(message))
{
response.Say(message);
}
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;
}
}