|
| 1 | +using BotSharp.Abstraction.Conversations; |
| 2 | +using BotSharp.Plugin.Twilio.Settings; |
| 3 | +using Microsoft.AspNetCore.Authorization; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System; |
| 7 | +using Twilio.AspNet.Common; |
| 8 | +using Twilio.AspNet.Core; |
| 9 | +using Twilio.TwiML; |
| 10 | +using Twilio.TwiML.Voice; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using BotSharp.Abstraction.Routing.Settings; |
| 14 | +using Twilio.Http; |
| 15 | +using Twilio.TwiML.Messaging; |
| 16 | +using BotSharp.Abstraction.Agents.Enums; |
| 17 | +using BotSharp.Abstraction.Conversations.Models; |
| 18 | + |
| 19 | +namespace BotSharp.Plugin.Twilio.Controllers; |
| 20 | + |
| 21 | +[AllowAnonymous] |
| 22 | +public class TwilioVoiceController : TwilioController |
| 23 | +{ |
| 24 | + private readonly TwilioSetting _settings; |
| 25 | + private readonly IServiceProvider _services; |
| 26 | + |
| 27 | + public TwilioVoiceController(TwilioSetting settings, IServiceProvider services) |
| 28 | + { |
| 29 | + _settings = settings; |
| 30 | + _services = services; |
| 31 | + } |
| 32 | + |
| 33 | + [HttpPost("/twilio/voice/welcome")] |
| 34 | + public async Task<TwiMLResult> StartConversation(VoiceRequest request) |
| 35 | + { |
| 36 | + string sessionId = $"TwilioVoice_{request.CallSid}"; |
| 37 | + var response = ReturnInstructions("Hello, how may I help you?"); |
| 38 | + return TwiML(response); |
| 39 | + } |
| 40 | + |
| 41 | + [HttpPost("/twilio/voice/{agentId}")] |
| 42 | + public async Task<TwiMLResult> ReceivedVoiceMessage([FromRoute] string agentId, VoiceRequest input) |
| 43 | + { |
| 44 | + string sessionId = $"TwilioVoice_{input.CallSid}"; |
| 45 | + |
| 46 | + var conv = _services.GetRequiredService<IConversationService>(); |
| 47 | + conv.SetConversationId(sessionId, new List<string> |
| 48 | + { |
| 49 | + "channel=phone", |
| 50 | + $"calling_phone={input.DialCallSid}" |
| 51 | + }); |
| 52 | + |
| 53 | + VoiceResponse response = default; |
| 54 | + |
| 55 | + var result = await conv.SendMessage(agentId, new RoleDialogModel(AgentRole.User, input.SpeechResult), async msg => |
| 56 | + { |
| 57 | + response = HangUp(msg.Content); |
| 58 | + }, async functionExecuting => |
| 59 | + { |
| 60 | + }, async functionExecuted => |
| 61 | + { |
| 62 | + }); |
| 63 | + |
| 64 | + return TwiML(response); |
| 65 | + } |
| 66 | + |
| 67 | + private VoiceResponse ReturnInstructions(string message) |
| 68 | + { |
| 69 | + var routingSetting = _services.GetRequiredService<RoutingSettings>(); |
| 70 | + |
| 71 | + var response = new VoiceResponse(); |
| 72 | + var gather = new Gather() |
| 73 | + { |
| 74 | + Input = new List<Gather.InputEnum>() |
| 75 | + { |
| 76 | + Gather.InputEnum.Speech |
| 77 | + }, |
| 78 | + Action = new Uri($"{_settings.CallbackHost}/twilio/voice/{routingSetting.RouterId}") |
| 79 | + }; |
| 80 | + gather.Say(message); |
| 81 | + response.Append(gather); |
| 82 | + return response; |
| 83 | + } |
| 84 | + |
| 85 | + private VoiceResponse HangUp(string message) |
| 86 | + { |
| 87 | + var response = new VoiceResponse(); |
| 88 | + if (!string.IsNullOrEmpty(message)) |
| 89 | + { |
| 90 | + response.Say(message); |
| 91 | + } |
| 92 | + response.Hangup(); |
| 93 | + return response; |
| 94 | + } |
| 95 | + |
| 96 | + private VoiceResponse HoldOn(int interval, string message = null) |
| 97 | + { |
| 98 | + var routingSetting = _services.GetRequiredService<RoutingSettings>(); |
| 99 | + |
| 100 | + var response = new VoiceResponse(); |
| 101 | + var gather = new Gather() |
| 102 | + { |
| 103 | + Input = new List<Gather.InputEnum>() { Gather.InputEnum.Speech }, |
| 104 | + Action = new Uri($"{_settings.CallbackHost}/twilio/voice/{routingSetting.RouterId}"), |
| 105 | + ActionOnEmptyResult = true |
| 106 | + }; |
| 107 | + if (!string.IsNullOrEmpty(message)) |
| 108 | + { |
| 109 | + gather.Say(message); |
| 110 | + } |
| 111 | + gather.Pause(interval); |
| 112 | + response.Append(gather); |
| 113 | + return response; |
| 114 | + } |
| 115 | +} |
0 commit comments