Skip to content

Commit 20a7f61

Browse files
authored
Merge pull request #607 from seplz/update/twilioPlugin
complete text2speech in queue service
2 parents 8d5f28d + 0b32ae4 commit 20a7f61

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,13 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
128128
}
129129
else
130130
{
131-
var textToSpeechService = CompletionProvider.GetTextToSpeech(_services, "openai", "tts-1");
132-
var fileService = _services.GetRequiredService<IFileStorageService>();
133-
var data = await textToSpeechService.GenerateSpeechFromTextAsync(reply.Content);
134-
var fileName = $"reply_{reply.MessageId ?? seqNum.ToString()}.mp3";
135-
await fileService.SaveSpeechFileAsync(conversationId, fileName, data);
136131
if (reply.ConversationEnd)
137132
{
138-
response = twilio.HangUp($"twilio/voice/speeches/{conversationId}/{fileName}");
133+
response = twilio.HangUp($"twilio/voice/speeches/{conversationId}/{reply.SpeechFileName}");
139134
}
140135
else
141136
{
142-
response = twilio.ReturnInstructions($"twilio/voice/speeches/{conversationId}/{fileName}", $"twilio/voice/{conversationId}/receive/{nextSeqNum}?states={states}", true);
137+
response = twilio.ReturnInstructions($"twilio/voice/speeches/{conversationId}/{reply.SpeechFileName}", $"twilio/voice/{conversationId}/receive/{nextSeqNum}?states={states}", true);
143138
}
144139

145140
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public class AssistantMessage
55
public bool ConversationEnd { get; set; }
66
public string Content { get; set; }
77
public string MessageId { get; set; }
8+
public string SpeechFileName { get; set; }
89
}
910
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
using BotSharp.Abstraction.Files;
12
using BotSharp.Abstraction.Routing;
3+
using BotSharp.Core.Infrastructures;
24
using BotSharp.Plugin.Twilio.Models;
35
using Microsoft.Extensions.Hosting;
6+
using System;
47
using System.Threading;
58
using Task = System.Threading.Tasks.Task;
69

@@ -94,14 +97,13 @@ private async Task ProcessUserMessageAsync(CallerMessage message)
9497
async functionExecuted =>
9598
{ }
9699
);
97-
if (reply == null || string.IsNullOrWhiteSpace(reply.Content))
98-
{
99-
reply = new AssistantMessage()
100-
{
101-
ConversationEnd = true,
102-
Content = "Sorry, something was wrong."
103-
};
104-
}
100+
var textToSpeechService = CompletionProvider.GetTextToSpeech(sp, "openai", "tts-1");
101+
var fileService = sp.GetRequiredService<IFileStorageService>();
102+
var data = await textToSpeechService.GenerateSpeechFromTextAsync(reply.Content);
103+
var fileName = $"reply_{reply.MessageId}.mp3";
104+
await fileService.SaveSpeechFileAsync(message.ConversationId, fileName, data);
105+
reply.SpeechFileName = fileName;
106+
reply.Content = null;
105107
await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply);
106108
}
107109
}

0 commit comments

Comments
 (0)