22using BotSharp . Core . Infrastructures ;
33using BotSharp . Plugin . Twilio . Models ;
44using BotSharp . Plugin . Twilio . Services ;
5- using Microsoft . AspNetCore . Authorization ;
5+ using Microsoft . AspNetCore . Http ;
66using Microsoft . AspNetCore . Mvc ;
7- using System . IdentityModel . Tokens . Jwt ;
87
98namespace BotSharp . Plugin . Twilio . Controllers ;
109
11- [ AllowAnonymous ]
12- [ Route ( "twilio/voice" ) ]
1310public class TwilioVoiceController : TwilioController
1411{
1512 private readonly TwilioSetting _settings ;
1613 private readonly IServiceProvider _services ;
14+ private readonly IHttpContextAccessor _context ;
1715
18- public TwilioVoiceController ( TwilioSetting settings , IServiceProvider services )
16+ public TwilioVoiceController ( TwilioSetting settings , IServiceProvider services , IHttpContextAccessor context )
1917 {
2018 _settings = settings ;
2119 _services = services ;
20+ _context = context ;
2221 }
2322
24- [ Authorize ]
25- [ HttpGet ( "/twilio/token" ) ]
26- public Token GetAccessToken ( )
27- {
28- var twilio = _services . GetRequiredService < TwilioService > ( ) ;
29- var accessToken = twilio . GetAccessToken ( ) ;
30- var jwt = new JwtSecurityTokenHandler ( ) . ReadJwtToken ( accessToken ) ;
31- return new Token
32- {
33- AccessToken = accessToken ,
34- ExpireTime = jwt . Payload . Exp . Value ,
35- TokenType = "Bearer" ,
36- Scope = "api"
37- } ;
38- }
39-
40- [ HttpPost ( "welcome" ) ]
23+ /// <summary>
24+ /// https://github.com/twilio-labs/twilio-aspnet?tab=readme-ov-file#validate-twilio-http-requests
25+ /// </summary>
26+ /// <param name="request"></param>
27+ /// <param name="states"></param>
28+ /// <returns></returns>
29+ /// <exception cref="ArgumentNullException"></exception>
30+ [ ValidateRequest ]
31+ [ HttpPost ( "twilio/voice/welcome" ) ]
4132 public TwiMLResult InitiateConversation ( VoiceRequest request , [ FromQuery ] string states )
4233 {
4334 if ( request ? . CallSid == null ) throw new ArgumentNullException ( nameof ( VoiceRequest . CallSid ) ) ;
@@ -48,7 +39,8 @@ public TwiMLResult InitiateConversation(VoiceRequest request, [FromQuery] string
4839 return TwiML ( response ) ;
4940 }
5041
51- [ HttpPost ( "{conversationId}/receive/{seqNum}" ) ]
42+ [ ValidateRequest ]
43+ [ HttpPost ( "twilio/voice/{conversationId}/receive/{seqNum}" ) ]
5244 public async Task < TwiMLResult > ReceiveCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum , [ FromQuery ] string states , VoiceRequest request )
5345 {
5446 var twilio = _services . GetRequiredService < TwilioService > ( ) ;
@@ -88,12 +80,15 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
8880 }
8981 }
9082 await messageQueue . EnqueueAsync ( callerMessage ) ;
91- response = twilio . ReturnInstructions ( null , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true , 1 ) ;
83+
84+ int audioIndex = Random . Shared . Next ( 1 , 5 ) ;
85+ response = twilio . ReturnInstructions ( $ "twilio/hold-on-{ audioIndex } .mp3", $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true , 1 ) ;
9286 }
9387 return TwiML ( response ) ;
9488 }
9589
96- [ HttpPost ( "{conversationId}/reply/{seqNum}" ) ]
90+ [ ValidateRequest ]
91+ [ HttpPost ( "twilio/voice/{conversationId}/reply/{seqNum}" ) ]
9792 public async Task < TwiMLResult > ReplyCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum , [ FromQuery ] string states , VoiceRequest request )
9893 {
9994 var nextSeqNum = seqNum + 1 ;
@@ -151,7 +146,8 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
151146 return TwiML ( response ) ;
152147 }
153148
154- [ HttpGet ( "speeches/{conversationId}/{fileName}" ) ]
149+ [ ValidateRequest ]
150+ [ HttpGet ( "twilio/voice/speeches/{conversationId}/{fileName}" ) ]
155151 public async Task < FileContentResult > RetrieveSpeechFile ( [ FromRoute ] string conversationId , [ FromRoute ] string fileName )
156152 {
157153 var fileService = _services . GetRequiredService < IFileStorageService > ( ) ;
0 commit comments