@@ -36,13 +36,13 @@ public TwiMLResult InitiateConversation(VoiceRequest request, [FromQuery] string
36
36
string conversationId = $ "TwilioVoice_{ request . CallSid } ";
37
37
var twilio = _services . GetRequiredService < TwilioService > ( ) ;
38
38
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 ) ;
40
40
return TwiML ( response ) ;
41
41
}
42
42
43
43
[ ValidateRequest ]
44
44
[ 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 )
46
46
{
47
47
var twilio = _services . GetRequiredService < TwilioService > ( ) ;
48
48
var messageQueue = _services . GetRequiredService < TwilioMessageQueue > ( ) ;
@@ -81,7 +81,21 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
81
81
}
82
82
else
83
83
{
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 ) ;
85
99
}
86
100
87
101
return TwiML ( response ) ;
@@ -90,7 +104,7 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
90
104
[ ValidateRequest ]
91
105
[ HttpPost ( "twilio/voice/{conversationId}/reply/{seqNum}" ) ]
92
106
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 )
94
108
{
95
109
var nextSeqNum = seqNum + 1 ;
96
110
var sessionManager = _services . GetRequiredService < ITwilioSessionManager > ( ) ;
@@ -107,6 +121,7 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
107
121
if ( indication != null )
108
122
{
109
123
var speechPaths = new List < string > ( ) ;
124
+ int segIndex = 0 ;
110
125
foreach ( var text in indication . Split ( '|' ) )
111
126
{
112
127
var seg = text . Trim ( ) ;
@@ -119,12 +134,14 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
119
134
var textToSpeechService = CompletionProvider . GetTextToSpeech ( _services , "openai" , "tts-1" ) ;
120
135
var fileService = _services . GetRequiredService < IFileStorageService > ( ) ;
121
136
var data = await textToSpeechService . GenerateSpeechFromTextAsync ( seg ) ;
122
- var fileName = $ "indication_{ seqNum } .mp3";
137
+ var fileName = $ "indication_{ seqNum } _ { segIndex } .mp3";
123
138
await fileService . SaveSpeechFileAsync ( conversationId , fileName , data ) ;
124
139
speechPaths . Add ( $ "twilio/voice/speeches/{ conversationId } /{ fileName } ") ;
140
+ segIndex ++ ;
125
141
}
126
142
}
127
143
response = twilio . ReturnInstructions ( speechPaths , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true ) ;
144
+ await sessionManager . RemoveReplyIndicationAsync ( conversationId , seqNum ) ;
128
145
}
129
146
else
130
147
{
0 commit comments