4
4
using BotSharp . Plugin . Twilio . Services ;
5
5
using Microsoft . AspNetCore . Http ;
6
6
using Microsoft . AspNetCore . Mvc ;
7
+ using Twilio . Http ;
7
8
8
9
namespace BotSharp . Plugin . Twilio . Controllers ;
9
10
@@ -35,7 +36,7 @@ public TwiMLResult InitiateConversation(VoiceRequest request, [FromQuery] string
35
36
string conversationId = $ "TwilioVoice_{ request . CallSid } ";
36
37
var twilio = _services . GetRequiredService < TwilioService > ( ) ;
37
38
var url = $ "twilio/voice/{ conversationId } /receive/0?states={ states } ";
38
- var response = twilio . ReturnInstructions ( "twilio/welcome.mp3" , url , true ) ;
39
+ var response = twilio . ReturnInstructions ( new List < string > { "twilio/welcome.mp3" } , url , true ) ;
39
40
return TwiML ( response ) ;
40
41
}
41
42
@@ -53,17 +54,10 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
53
54
messages . Add ( text ) ;
54
55
await sessionManager . StageCallerMessageAsync ( conversationId , seqNum , text ) ;
55
56
}
57
+
56
58
VoiceResponse response ;
57
- if ( messages . Count == 0 && seqNum == 0 )
58
- {
59
- response = twilio . ReturnInstructions ( "twilio/welcome.mp3" , $ "twilio/voice/{ conversationId } /receive/{ seqNum } ?states={ states } ", true , timeout : 2 ) ;
60
- }
61
- else
59
+ if ( messages . Any ( ) )
62
60
{
63
- if ( messages . Count == 0 )
64
- {
65
- messages = await sessionManager . RetrieveStagedCallerMessagesAsync ( conversationId , seqNum - 1 ) ;
66
- }
67
61
var messageContent = string . Join ( "\r \n " , messages ) ;
68
62
var callerMessage = new CallerMessage ( )
69
63
{
@@ -82,15 +76,21 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
82
76
}
83
77
await messageQueue . EnqueueAsync ( callerMessage ) ;
84
78
85
- int audioIndex = Random . Shared . Next ( 1 , 5 ) ;
86
- response = twilio . ReturnInstructions ( $ "twilio/hold-on-{ audioIndex } .mp3", $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true , 1 ) ;
79
+ response = new VoiceResponse ( )
80
+ . Redirect ( new Uri ( $ "{ _settings . CallbackHost } /twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ") , HttpMethod . Post ) ;
81
+ }
82
+ else
83
+ {
84
+ response = twilio . ReturnInstructions ( null , $ "twilio/voice/{ conversationId } /receive/{ seqNum } ?states={ states } ", true ) ;
87
85
}
86
+
88
87
return TwiML ( response ) ;
89
88
}
90
89
91
90
[ ValidateRequest ]
92
91
[ HttpPost ( "twilio/voice/{conversationId}/reply/{seqNum}" ) ]
93
- public async Task < TwiMLResult > ReplyCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum , [ FromQuery ] string states , VoiceRequest request )
92
+ public async Task < TwiMLResult > ReplyCallerMessage ( [ FromRoute ] string conversationId , [ FromRoute ] int seqNum ,
93
+ [ FromQuery ] string states , [ FromQuery ] string play , VoiceRequest request )
94
94
{
95
95
var nextSeqNum = seqNum + 1 ;
96
96
var sessionManager = _services . GetRequiredService < ITwilioSessionManager > ( ) ;
@@ -106,25 +106,33 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
106
106
var indication = await sessionManager . GetReplyIndicationAsync ( conversationId , seqNum ) ;
107
107
if ( indication != null )
108
108
{
109
- string speechPath ;
110
- if ( indication . StartsWith ( '# ') )
109
+ var speechPaths = new List < string > ( ) ;
110
+ foreach ( var text in indication . Split ( '| ') )
111
111
{
112
- speechPath = $ "twilio/{ indication . Substring ( 1 ) } ";
112
+ var seg = text . Trim ( ) ;
113
+ if ( seg . StartsWith ( '#' ) )
114
+ {
115
+ speechPaths . Add ( $ "twilio/{ seg . Substring ( 1 ) } .mp3") ;
116
+ }
117
+ else
118
+ {
119
+ var textToSpeechService = CompletionProvider . GetTextToSpeech ( _services , "openai" , "tts-1" ) ;
120
+ var fileService = _services . GetRequiredService < IFileStorageService > ( ) ;
121
+ var data = await textToSpeechService . GenerateSpeechFromTextAsync ( seg ) ;
122
+ var fileName = $ "indication_{ seqNum } .mp3";
123
+ await fileService . SaveSpeechFileAsync ( conversationId , fileName , data ) ;
124
+ speechPaths . Add ( $ "twilio/voice/speeches/{ conversationId } /{ fileName } ") ;
125
+ }
113
126
}
114
- else
115
- {
116
- var textToSpeechService = CompletionProvider . GetTextToSpeech ( _services , "openai" , "tts-1" ) ;
117
- var fileService = _services . GetRequiredService < IFileStorageService > ( ) ;
118
- var data = await textToSpeechService . GenerateSpeechFromTextAsync ( indication ) ;
119
- var fileName = $ "indication_{ seqNum } .mp3";
120
- await fileService . SaveSpeechFileAsync ( conversationId , fileName , data ) ;
121
- speechPath = $ "twilio/voice/speeches/{ conversationId } /{ fileName } ";
122
- }
123
- response = twilio . ReturnInstructions ( speechPath , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true , 2 ) ;
127
+ response = twilio . ReturnInstructions ( speechPaths , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true ) ;
124
128
}
125
129
else
126
130
{
127
- response = twilio . ReturnInstructions ( null , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true , 1 ) ;
131
+ response = twilio . ReturnInstructions ( new List < string >
132
+ {
133
+ $ "twilio/hold-on-{ Random . Shared . Next ( 1 , 5 ) } .mp3",
134
+ $ "twilio/typing-{ Random . Shared . Next ( 2 , 4 ) } .mp3"
135
+ } , $ "twilio/voice/{ conversationId } /reply/{ seqNum } ?states={ states } ", true ) ;
128
136
}
129
137
}
130
138
else
@@ -135,7 +143,7 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
135
143
}
136
144
else
137
145
{
138
- response = twilio . ReturnInstructions ( $ "twilio/voice/speeches/{ conversationId } /{ reply . SpeechFileName } ", $ "twilio/voice/{ conversationId } /receive/{ nextSeqNum } ?states={ states } ", true ) ;
146
+ response = twilio . ReturnInstructions ( new List < string > { $ "twilio/voice/speeches/{ conversationId } /{ reply . SpeechFileName } " } , $ "twilio/voice/{ conversationId } /receive/{ nextSeqNum } ?states={ states } ", true ) ;
139
147
}
140
148
141
149
}
0 commit comments