Skip to content

Commit de44da5

Browse files
Bo YinBo Yin
authored andcommitted
draft
1 parent 23f547a commit de44da5

File tree

7 files changed

+27
-58
lines changed

7 files changed

+27
-58
lines changed

src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Speech.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BotSharp.Core.Files.Services
44
{
5-
public partial class BotSharpFileService
5+
public partial class LocalFileStorageService
66
{
77
public async Task SaveSpeechFileAsync(string conversationId, string fileName, BinaryData data)
88
{

src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,24 @@ public async Task<TwiMLResult> ReceivedVoiceMessage([FromRoute] string agentId,
8686
}
8787

8888

89-
[HttpPost("anonymous/start")]
89+
[HttpPost("start")]
9090
public TwiMLResult InitiateConversation(VoiceRequest request)
9191
{
9292
if (request?.CallSid == null) throw new ArgumentNullException(nameof(VoiceRequest.CallSid));
9393
string sessionId = $"TwilioVoice_{request.CallSid}";
9494
var twilio = _services.GetRequiredService<TwilioService>();
95-
var url = $"twiliovoice/anonymous/{sessionId}/send/0";
96-
var response = twilio.DummyInstructions("Hello, how may I help you?", url, false);
95+
var url = $"twiliovoice/{sessionId}/send/0";
96+
var response = twilio.ReturnInstructions("twilio/welcome.mp3", url, false);
9797
return TwiML(response);
9898
}
9999

100-
[HttpPost("anonymous/{sessionId}/send/{seqNum}")]
100+
[HttpPost("{sessionId}/send/{seqNum}")]
101101
public async Task<TwiMLResult> SendCallerMessage([FromRoute] string sessionId, [FromRoute] int seqNum, VoiceRequest request)
102102
{
103103
var twilio = _services.GetRequiredService<TwilioService>();
104104
var messageQueue = _services.GetRequiredService<TwilioMessageQueue>();
105105
var sessionManager = _services.GetRequiredService<ITwilioSessionManager>();
106-
var url = $"twiliovoice/anonymous/{sessionId}/reply/{seqNum}";
106+
var url = $"twiliovoice/{sessionId}/reply/{seqNum}";
107107
var messages = await sessionManager.RetrieveStagedCallerMessagesAsync(sessionId, seqNum);
108108
if (!string.IsNullOrWhiteSpace(request.SpeechResult))
109109
{
@@ -121,16 +121,16 @@ public async Task<TwiMLResult> SendCallerMessage([FromRoute] string sessionId, [
121121
From = request.From
122122
};
123123
await messageQueue.EnqueueAsync(callerMessage);
124-
response = twilio.DummyInstructions("Please hold on and wait a moment.", url, true);
124+
response = twilio.ReturnInstructions("twilio/holdon.mp3", url, true);
125125
}
126126
else
127127
{
128-
response = twilio.HangUp("Thanks for calling. Good bye.");
128+
response = twilio.HangUp("twilio/holdon.mp3");
129129
}
130130
return TwiML(response);
131131
}
132132

133-
[HttpPost("anonymous/{sessionId}/reply/{seqNum}")]
133+
[HttpPost("{sessionId}/reply/{seqNum}")]
134134
public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string sessionId, [FromRoute] int seqNum, VoiceRequest request)
135135
{
136136
var nextSeqNum = seqNum + 1;
@@ -144,38 +144,28 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string sessionId,
144144
VoiceResponse response;
145145
if (string.IsNullOrEmpty(reply))
146146
{
147-
response = twilio.ReturnInstructions(null, $"twiliovoice/anonymous/{sessionId}/reply/{seqNum}", true);
147+
response = twilio.ReturnInstructions(null, $"twiliovoice/{sessionId}/reply/{seqNum}", true);
148148
}
149149
else
150150
{
151151

152152
var textToSpeechService = CompletionProvider.GetTextToSpeech(_services, "openai", "tts-1");
153-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
153+
var fileService = _services.GetRequiredService<IFileStorageService>();
154154
var data = await textToSpeechService.GenerateSpeechFromTextAsync(reply);
155155
var fileName = $"{seqNum}.mp3";
156156
await fileService.SaveSpeechFileAsync(sessionId, fileName, data);
157-
response = twilio.ReturnInstructions($"twiliovoice/anonymous/speeches/{sessionId}/{fileName}", $"twiliovoice/anonymous/{sessionId}/send/{nextSeqNum}", true);
157+
response = twilio.ReturnInstructions($"twiliovoice/speeches/{sessionId}/{fileName}", $"twiliovoice/{sessionId}/send/{nextSeqNum}", true);
158158
}
159159
return TwiML(response);
160160
}
161161

162-
[HttpGet("anonymous/speeches/{conversationId}/{fileName}")]
162+
[HttpGet("speeches/{conversationId}/{fileName}")]
163163
public async Task<FileContentResult> RetrieveSpeechFile([FromRoute] string conversationId, [FromRoute] string fileName)
164164
{
165-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
165+
var fileService = _services.GetRequiredService<IFileStorageService>();
166166
var data = await fileService.RetrieveSpeechFileAsync(conversationId, fileName);
167-
var result = new FileContentResult(data.ToArray(), "application/octet-stream");
167+
var result = new FileContentResult(data.ToArray(), "audio/mpeg");
168168
result.FileDownloadName = fileName;
169169
return result;
170170
}
171-
172-
[HttpGet("anonymous/text-to-speech")]
173-
public async Task<IActionResult> TextToSpeech([FromQuery] string text)
174-
{
175-
var textToSpeechService = CompletionProvider.GetTextToSpeech(_services, "openai", "tts-1");
176-
var data = await textToSpeechService.GenerateSpeechFromTextAsync(text);
177-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
178-
await fileService.SaveSpeechFileAsync("123", "sample.mp3", data);
179-
return Ok();
180-
}
181171
}

src/Plugins/BotSharp.Plugin.Twilio/Models/CallerMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class CallerMessage
99

1010
public override string ToString()
1111
{
12-
return $"({SessionId}-{SeqNumber}) {Content}";
12+
return $"{SessionId}-{SeqNumber}";
1313
}
1414
}
1515
}

src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueue.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public async ValueTask EnqueueAsync(CallerMessage request)
2525

2626
internal void Stop()
2727
{
28+
Console.WriteLine($"[{DateTime.UtcNow}] Complete queue");
2829
_queue.Writer.TryComplete();
2930
}
3031
}

src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BotSharp.Abstraction.Routing;
22
using BotSharp.Plugin.Twilio.Models;
33
using Microsoft.Extensions.Hosting;
4-
using System.Security.Cryptography;
54
using System.Threading;
65
using Task = System.Threading.Tasks.Task;
76

@@ -31,12 +30,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3130
{
3231
try
3332
{
34-
Console.WriteLine("Processing {message}.", message);
33+
Console.WriteLine($"Start processing {message}.");
3534
await ProcessUserMessageAsync(message);
3635
}
3736
catch (Exception ex)
3837
{
39-
Console.WriteLine("Processing {message} failed due to {ex}.", message, ex.Message);
38+
Console.WriteLine($"Processing {message} failed due to {ex.Message}.");
4039
}
4140
finally
4241
{
@@ -57,18 +56,17 @@ private async Task ProcessUserMessageAsync(CallerMessage message)
5756
using var scope = _serviceProvider.CreateScope();
5857
var sp = scope.ServiceProvider;
5958
string reply = null;
60-
//await Task.Delay(2000);
61-
//reply = $"response for sequence number {message.SeqNumber}";
6259
var inputMsg = new RoleDialogModel(AgentRole.User, message.Content);
6360
var conv = sp.GetRequiredService<IConversationService>();
6461
var routing = sp.GetRequiredService<IRoutingService>();
62+
var config = sp.GetRequiredService<TwilioSetting>();
6563
routing.Context.SetMessageId(message.SessionId, inputMsg.MessageId);
6664
conv.SetConversationId(message.SessionId, new List<MessageState>
6765
{
6866
new MessageState("channel", ConversationChannel.Phone),
6967
new MessageState("calling_phone", message.From)
7068
});
71-
var result = await conv.SendMessage("2cd4b805-7078-4405-87e9-2ec9aadf8a11",
69+
var result = await conv.SendMessage(config.AgentId,
7270
inputMsg,
7371
replyMessage: null,
7472
async msg =>
@@ -82,7 +80,7 @@ private async Task ProcessUserMessageAsync(CallerMessage message)
8280
);
8381
if (string.IsNullOrWhiteSpace(reply))
8482
{
85-
reply = "Bye.";
83+
reply = "Sorry, something was wrong.";
8684
}
8785
var sessionManager = sp.GetRequiredService<ITwilioSessionManager>();
8886
await sessionManager.SetAssistantReplyAsync(message.SessionId, message.SeqNumber, reply);

src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Plugin.Twilio.Settings;
21
using Twilio.Jwt.AccessToken;
32
using Token = Twilio.Jwt.AccessToken.Token;
43

@@ -74,6 +73,7 @@ public VoiceResponse ReturnInstructions(string speechPath, string callbackPath,
7473
Gather.InputEnum.Speech
7574
},
7675
Action = new Uri($"{_settings.CallbackHost}/{callbackPath}"),
76+
SpeechTimeout = "3",
7777
ActionOnEmptyResult = actionOnEmptyResult
7878
};
7979
if (!string.IsNullOrEmpty(speechPath))
@@ -84,32 +84,12 @@ public VoiceResponse ReturnInstructions(string speechPath, string callbackPath,
8484
return response;
8585
}
8686

87-
public VoiceResponse DummyInstructions(string message, string callbackPath, bool actionOnEmptyResult)
87+
public VoiceResponse HangUp(string speechPath)
8888
{
8989
var response = new VoiceResponse();
90-
var gather = new Gather()
91-
{
92-
Input = new List<Gather.InputEnum>()
93-
{
94-
Gather.InputEnum.Speech
95-
},
96-
Action = new Uri($"{_settings.CallbackHost}/{callbackPath}"),
97-
ActionOnEmptyResult = actionOnEmptyResult
98-
};
99-
if (!string.IsNullOrEmpty(message))
100-
{
101-
gather.Say(message);
102-
}
103-
response.Append(gather);
104-
return response;
105-
}
106-
107-
public VoiceResponse HangUp(string message)
108-
{
109-
var response = new VoiceResponse();
110-
if (!string.IsNullOrEmpty(message))
90+
if (!string.IsNullOrEmpty(speechPath))
11191
{
112-
response.Say(message);
92+
response.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
11393
}
11494
response.Hangup();
11595
return response;

src/Plugins/BotSharp.Plugin.Twilio/TwilioPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
1919
});
2020

2121
services.AddScoped<TwilioService>();
22-
var conn = ConnectionMultiplexer.Connect("10.2.3.227");
22+
var conn = ConnectionMultiplexer.Connect(config["Twilio:RedisConnectionString"]);
2323
var sessionManager = new TwilioSessionManager(conn);
2424
services.AddSingleton<ITwilioSessionManager>(sessionManager);
2525
services.AddSingleton<TwilioMessageQueue>();

0 commit comments

Comments
 (0)