2023-11-09 20:02:57 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-12-06 22:55:55 +00:00
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using BotSharp.Plugin.Twilio.Services;
|
2023-11-09 20:02:57 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.Twilio.Controllers;
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public class TwilioVoiceController : TwilioController
|
|
|
|
|
{
|
|
|
|
|
private readonly TwilioSetting _settings;
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
|
|
|
|
public TwilioVoiceController(TwilioSetting settings, IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_services = services;
|
|
|
|
|
}
|
2023-12-06 22:55:55 +00:00
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpGet("/twilio/token")]
|
|
|
|
|
public Token GetAccessToken()
|
|
|
|
|
{
|
|
|
|
|
var twilio = _services.GetRequiredService<TwilioService>();
|
|
|
|
|
var accessToken = twilio.GetAccessToken();
|
|
|
|
|
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken);
|
|
|
|
|
return new Token
|
|
|
|
|
{
|
|
|
|
|
AccessToken = accessToken,
|
|
|
|
|
ExpireTime = jwt.Payload.Exp.Value,
|
|
|
|
|
TokenType = "Bearer",
|
|
|
|
|
Scope = "api"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-09 20:02:57 +00:00
|
|
|
[HttpPost("/twilio/voice/welcome")]
|
|
|
|
|
public async Task<TwiMLResult> StartConversation(VoiceRequest request)
|
|
|
|
|
{
|
|
|
|
|
string sessionId = $"TwilioVoice_{request.CallSid}";
|
2023-12-06 22:55:55 +00:00
|
|
|
var twilio = _services.GetRequiredService<TwilioService>();
|
|
|
|
|
var response = twilio.ReturnInstructions("Hello, how may I help you?");
|
2023-11-09 20:02:57 +00:00
|
|
|
return TwiML(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/twilio/voice/{agentId}")]
|
|
|
|
|
public async Task<TwiMLResult> ReceivedVoiceMessage([FromRoute] string agentId, VoiceRequest input)
|
|
|
|
|
{
|
|
|
|
|
string sessionId = $"TwilioVoice_{input.CallSid}";
|
|
|
|
|
|
|
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
conv.SetConversationId(sessionId, new List<string>
|
|
|
|
|
{
|
2023-11-27 03:44:17 +00:00
|
|
|
$"channel={ConversationChannel.Phone}",
|
2023-11-09 20:02:57 +00:00
|
|
|
$"calling_phone={input.DialCallSid}"
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-06 22:55:55 +00:00
|
|
|
var twilio = _services.GetRequiredService<TwilioService>();
|
2023-11-09 20:02:57 +00:00
|
|
|
VoiceResponse response = default;
|
|
|
|
|
|
2023-11-27 03:04:48 +00:00
|
|
|
var result = await conv.SendMessage(agentId,
|
|
|
|
|
new RoleDialogModel(AgentRole.User, input.SpeechResult),
|
|
|
|
|
async msg =>
|
|
|
|
|
{
|
2023-12-06 22:55:55 +00:00
|
|
|
response = twilio.ReturnInstructions(msg.Content);
|
|
|
|
|
if (msg.FunctionName == "conversation_end")
|
|
|
|
|
{
|
|
|
|
|
response = twilio.HangUp(msg.Content);
|
|
|
|
|
}
|
2023-11-27 03:04:48 +00:00
|
|
|
}, async functionExecuting =>
|
|
|
|
|
{
|
|
|
|
|
}, async functionExecuted =>
|
|
|
|
|
{
|
|
|
|
|
});
|
2023-11-09 20:02:57 +00:00
|
|
|
|
|
|
|
|
return TwiML(response);
|
|
|
|
|
}
|
|
|
|
|
}
|