@@ -32,7 +32,7 @@ public TwilioVoiceController(TwilioSetting settings, IServiceProvider services,
32
32
/// <exception cref="ArgumentNullException"></exception>
33
33
[ ValidateRequest ]
34
34
[ HttpPost ( "twilio/voice/welcome" ) ]
35
- public TwiMLResult InitiateConversation ( VoiceRequest request , [ FromQuery ] string states )
35
+ public async Task < TwiMLResult > InitiateConversation ( VoiceRequest request , [ FromQuery ] string states , [ FromQuery ] string intent )
36
36
{
37
37
if ( request ? . CallSid == null )
38
38
{
@@ -41,8 +41,29 @@ public TwiMLResult InitiateConversation(VoiceRequest request, [FromQuery] string
41
41
42
42
string conversationId = $ "TwilioVoice_{ request . CallSid } ";
43
43
var twilio = _services . GetRequiredService < TwilioService > ( ) ;
44
- var url = $ "twilio/voice/{ conversationId } /receive/0?states={ states } ";
45
- var response = twilio . ReturnNoninterruptedInstructions ( new List < string > { "twilio/welcome.mp3" } , url , true , timeout : 2 ) ;
44
+ VoiceResponse response ;
45
+ if ( string . IsNullOrWhiteSpace ( intent ) )
46
+ {
47
+ var url = $ "twilio/voice/{ conversationId } /receive/0?states={ states } ";
48
+ response = twilio . ReturnNoninterruptedInstructions ( new List < string > { "twilio/welcome.mp3" } , url , true , timeout : 2 ) ;
49
+ }
50
+ else
51
+ {
52
+ int seqNum = 0 ;
53
+ var messageQueue = _services . GetRequiredService < TwilioMessageQueue > ( ) ;
54
+ var sessionManager = _services . GetRequiredService < ITwilioSessionManager > ( ) ;
55
+ await sessionManager . StageCallerMessageAsync ( conversationId , seqNum , intent ) ;
56
+ var callerMessage = new CallerMessage ( )
57
+ {
58
+ ConversationId = conversationId ,
59
+ SeqNumber = seqNum ,
60
+ Content = intent ,
61
+ From = request . From ,
62
+ States = ParseStates ( states )
63
+ } ;
64
+ await messageQueue . EnqueueAsync ( callerMessage ) ;
65
+ response = new VoiceResponse ( ) . Redirect ( new Uri ( $ "{ _settings . CallbackHost } /twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ") , HttpMethod . Post ) ;
66
+ }
46
67
return TwiML ( response ) ;
47
68
}
48
69
@@ -72,19 +93,10 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
72
93
ConversationId = conversationId ,
73
94
SeqNumber = seqNum ,
74
95
Content = messageContent ,
75
- Digits = request . Digits ,
76
- From = request . From
96
+ Digits = request . Digits ,
97
+ From = request . From ,
98
+ States = ParseStates ( states )
77
99
} ;
78
-
79
- if ( ! string . IsNullOrEmpty ( states ) )
80
- {
81
- var kvp = states . Split ( ':' ) ;
82
- if ( kvp . Length == 2 )
83
- {
84
- callerMessage . States . Add ( kvp [ 0 ] , kvp [ 1 ] ) ;
85
- }
86
- }
87
-
88
100
await messageQueue . EnqueueAsync ( callerMessage ) ;
89
101
90
102
response = new VoiceResponse ( ) . Redirect ( new Uri ( $ "{ _settings . CallbackHost } /twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ") , HttpMethod . Post ) ;
@@ -159,7 +171,7 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
159
171
{
160
172
speechPaths . Add ( $ "twilio/hold-on-short-{ holdOnIndex } .mp3") ;
161
173
}
162
-
174
+
163
175
var fileName = $ "indication_{ seqNum } _{ segIndex } .mp3";
164
176
fileStorage . SaveSpeechFile ( conversationId , fileName , data ) ;
165
177
speechPaths . Add ( $ "twilio/voice/speeches/{ conversationId } /{ fileName } ") ;
@@ -214,7 +226,7 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
214
226
response = twilio . ReturnInstructions ( new List < string >
215
227
{
216
228
$ "twilio/voice/speeches/{ conversationId } /{ reply . SpeechFileName } "
217
- } , $ "twilio/voice/{ conversationId } /receive/{ nextSeqNum } ?states={ states } ", true , hints : reply . Hints ) ;
229
+ } , $ "twilio/voice/{ conversationId } /receive/{ nextSeqNum } ?states={ states } ", true , hints : reply . Hints ) ;
218
230
}
219
231
}
220
232
@@ -233,4 +245,23 @@ public async Task<FileContentResult> GetSpeechFile([FromRoute] string conversati
233
245
} ;
234
246
return result ;
235
247
}
248
+
249
+ private Dictionary < string , string > ParseStates ( string ? states )
250
+ {
251
+ var result = new Dictionary < string , string > ( ) ;
252
+ if ( string . IsNullOrWhiteSpace ( states ) )
253
+ {
254
+ return result ;
255
+ }
256
+ var kvps = states . Split ( ',' , StringSplitOptions . TrimEntries | StringSplitOptions . RemoveEmptyEntries ) ;
257
+ foreach ( var kvp in kvps )
258
+ {
259
+ var parts = kvp . Split ( ':' ) ;
260
+ if ( parts . Length == 2 )
261
+ {
262
+ result . Add ( parts [ 0 ] , parts [ 1 ] ) ;
263
+ }
264
+ }
265
+ return result ;
266
+ }
236
267
}
0 commit comments