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;
|
2024-03-27 18:50:27 +00:00
|
|
|
using BotSharp.Abstraction.Routing;
|
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}";
|
|
|
|
|
|
2024-03-27 18:50:27 +00:00
|
|
|
var inputMsg = new RoleDialogModel(AgentRole.User, input.SpeechResult);
|
2023-11-09 20:02:57 +00:00
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
2024-03-27 18:50:27 +00:00
|
|
|
var routing = _services.GetRequiredService<IRoutingService>();
|
|
|
|
|
routing.Context.SetMessageId(sessionId, inputMsg.MessageId);
|
|
|
|
|
|
|
|
|
|
conv.SetConversationId(sessionId, new List<MessageState>
|
2023-11-09 20:02:57 +00:00
|
|
|
{
|
2024-03-27 18:50:27 +00:00
|
|
|
new MessageState("channel", ConversationChannel.Phone),
|
|
|
|
|
new MessageState("calling_phone", input.DialCallSid)
|
2023-11-09 20:02:57 +00:00
|
|
|
});
|
|
|
|
|
|
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,
|
2024-03-27 18:50:27 +00:00
|
|
|
inputMsg,
|
2024-03-16 14:43:00 +00:00
|
|
|
replyMessage: null,
|
2023-11-27 03:04:48 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|