@@ -36,13 +36,13 @@ public TwiMLResult InitiateConversation(VoiceRequest request, [FromQuery] string
3636 string conversationId = $ "TwilioVoice_{ request . CallSid } ";
3737 var twilio = _services . GetRequiredService < TwilioService > ( ) ;
3838 var url = $ "twilio/voice/{ conversationId } /receive/0?states={ states } ";
39- var response = twilio . ReturnInstructions ( new List < string > { "twilio/welcome.mp3" } , url , true ) ;
39+ var response = twilio . ReturnNoninterruptedInstructions ( new List < string > { "twilio/welcome.mp3" } , url , true ) ;
4040 return TwiML ( response ) ;
4141 }
4242
4343 [ ValidateRequest ]
4444 [ HttpPost ( "twilio/voice/{conversationId}/receive/{seqNum}" ) ]
45- public async Task < TwiMLResult > ReceiveCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum , [ FromQuery ] string states , VoiceRequest request )
45+ public async Task < TwiMLResult > ReceiveCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum , [ FromQuery ] string states , [ FromQuery ] int attempts , VoiceRequest request )
4646 {
4747 var twilio = _services . GetRequiredService < TwilioService > ( ) ;
4848 var messageQueue = _services . GetRequiredService < TwilioMessageQueue > ( ) ;
@@ -81,7 +81,21 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
8181 }
8282 else
8383 {
84- response = twilio . ReturnInstructions ( null , $ "twilio/voice/{ conversationId } /receive/{ seqNum } ?states={ states } ", true ) ;
84+ if ( attempts >= 3 )
85+ {
86+ var speechPaths = new List < string > ( ) ;
87+ if ( seqNum == 0 )
88+ {
89+ speechPaths . Add ( "twilio/welcome.mp3" ) ;
90+ }
91+ else
92+ {
93+ var lastRepy = await sessionManager . GetAssistantReplyAsync ( conversationId , seqNum - 1 ) ;
94+ speechPaths . Add ( $ "twilio/voice/speeches/{ conversationId } /{ lastRepy . SpeechFileName } ") ;
95+ }
96+ response = twilio . ReturnInstructions ( speechPaths , $ "twilio/voice/{ conversationId } /receive/{ seqNum } ?states={ states } ", true ) ;
97+ }
98+ response = twilio . ReturnInstructions ( null , $ "twilio/voice/{ conversationId } /receive/{ seqNum } ?states={ states } &attempts={ ++ attempts } ", true ) ;
8599 }
86100
87101 return TwiML ( response ) ;
@@ -90,7 +104,7 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
90104 [ ValidateRequest ]
91105 [ HttpPost ( "twilio/voice/{conversationId}/reply/{seqNum}" ) ]
92106 public async Task < TwiMLResult > ReplyCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum ,
93- [ FromQuery ] string states , [ FromQuery ] string play , VoiceRequest request )
107+ [ FromQuery ] string states , VoiceRequest request )
94108 {
95109 var nextSeqNum = seqNum + 1 ;
96110 var sessionManager = _services . GetRequiredService < ITwilioSessionManager > ( ) ;
@@ -107,6 +121,7 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
107121 if ( indication != null )
108122 {
109123 var speechPaths = new List < string > ( ) ;
124+ int segIndex = 0 ;
110125 foreach ( var text in indication . Split ( '|' ) )
111126 {
112127 var seg = text . Trim ( ) ;
@@ -119,12 +134,14 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
119134 var textToSpeechService = CompletionProvider . GetTextToSpeech ( _services , "openai" , "tts-1" ) ;
120135 var fileService = _services . GetRequiredService < IFileStorageService > ( ) ;
121136 var data = await textToSpeechService . GenerateSpeechFromTextAsync ( seg ) ;
122- var fileName = $ "indication_{ seqNum } .mp3";
137+ var fileName = $ "indication_{ seqNum } _ { segIndex } .mp3";
123138 await fileService . SaveSpeechFileAsync ( conversationId , fileName , data ) ;
124139 speechPaths . Add ( $ "twilio/voice/speeches/{ conversationId } /{ fileName } ") ;
140+ segIndex ++ ;
125141 }
126142 }
127143 response = twilio . ReturnInstructions ( speechPaths , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true ) ;
144+ await sessionManager . RemoveReplyIndicationAsync ( conversationId , seqNum ) ;
128145 }
129146 else
130147 {
0 commit comments