Merge branch 'master' of https://github.com/SciSharp/BotSharp
This commit is contained in:
commit
b23b3850ef
|
|
@ -72,6 +72,7 @@ public class TwilioVoiceController : TwilioController
|
|||
ConversationId = conversationId,
|
||||
SeqNumber = seqNum,
|
||||
Content = messageContent,
|
||||
Digits = request.Digits,
|
||||
From = request.From
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace BotSharp.Plugin.Twilio.Models
|
|||
public string ConversationId { get; set; }
|
||||
public int SeqNumber { get; set; }
|
||||
public string Content { get; set; }
|
||||
public string Digits { get; set; }
|
||||
public string From { get; set; }
|
||||
public Dictionary<string, string> States { get; set; } = new();
|
||||
|
||||
|
|
|
|||
|
|
@ -66,23 +66,11 @@ namespace BotSharp.Plugin.Twilio.Services
|
|||
var sessionManager = sp.GetRequiredService<ITwilioSessionManager>();
|
||||
var progressService = sp.GetRequiredService<IConversationProgressService>();
|
||||
InitProgressService(message, sessionManager, progressService);
|
||||
|
||||
routing.Context.SetMessageId(message.ConversationId, inputMsg.MessageId);
|
||||
var states = new List<MessageState>
|
||||
{
|
||||
new MessageState("channel", ConversationChannel.Phone),
|
||||
new MessageState("calling_phone", message.From)
|
||||
};
|
||||
|
||||
foreach (var kvp in message.States)
|
||||
{
|
||||
states.Add(new MessageState(kvp.Key, kvp.Value));
|
||||
}
|
||||
conv.SetConversationId(message.ConversationId, states);
|
||||
|
||||
InitConversation(message, inputMsg, conv, routing);
|
||||
|
||||
var result = await conv.SendMessage(config.AgentId,
|
||||
inputMsg,
|
||||
replyMessage: null,
|
||||
replyMessage: BuildPostbackMessageModel(conv, message),
|
||||
async msg =>
|
||||
{
|
||||
reply = new AssistantMessage()
|
||||
|
|
@ -94,13 +82,50 @@ namespace BotSharp.Plugin.Twilio.Services
|
|||
};
|
||||
}
|
||||
);
|
||||
reply.SpeechFileName = await GetReplySpeechFileName(message.ConversationId, reply, sp);
|
||||
reply.Hints = GetHints(reply);
|
||||
reply.Content = null;
|
||||
await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply);
|
||||
}
|
||||
|
||||
private PostbackMessageModel BuildPostbackMessageModel(IConversationService conv, CallerMessage message)
|
||||
{
|
||||
var messages = conv.GetDialogHistory(1);
|
||||
if (!messages.Any()) return null;
|
||||
var lastMessage = messages[0];
|
||||
if (string.IsNullOrEmpty(lastMessage.PostbackFunctionName)) return null;
|
||||
return new PostbackMessageModel
|
||||
{
|
||||
FunctionName = lastMessage.PostbackFunctionName,
|
||||
ParentId = lastMessage.MessageId,
|
||||
Payload = message.Digits
|
||||
};
|
||||
}
|
||||
|
||||
private static void InitConversation(CallerMessage message, RoleDialogModel inputMsg, IConversationService conv, IRoutingService routing)
|
||||
{
|
||||
routing.Context.SetMessageId(message.ConversationId, inputMsg.MessageId);
|
||||
var states = new List<MessageState>
|
||||
{
|
||||
new("channel", ConversationChannel.Phone),
|
||||
new("calling_phone", message.From)
|
||||
};
|
||||
states.AddRange(message.States.Select(kvp => new MessageState(kvp.Key, kvp.Value)));
|
||||
conv.SetConversationId(message.ConversationId, states);
|
||||
}
|
||||
|
||||
private static async Task<string> GetReplySpeechFileName(string conversationId, AssistantMessage reply, IServiceProvider sp)
|
||||
{
|
||||
var completion = CompletionProvider.GetAudioCompletion(sp, "openai", "tts-1");
|
||||
var fileStorage = sp.GetRequiredService<IFileStorageService>();
|
||||
var data = await completion.GenerateAudioFromTextAsync(reply.Content);
|
||||
var fileName = $"reply_{reply.MessageId}.mp3";
|
||||
fileStorage.SaveSpeechFile(message.ConversationId, fileName, data);
|
||||
reply.SpeechFileName = fileName;
|
||||
fileStorage.SaveSpeechFile(conversationId, fileName, data);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
private static string GetHints(AssistantMessage reply)
|
||||
{
|
||||
var phrases = reply.Content.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
int capcity = 100;
|
||||
var hints = new List<string>(capcity);
|
||||
|
|
@ -122,9 +147,7 @@ namespace BotSharp.Plugin.Twilio.Services
|
|||
}
|
||||
// add frequency short words
|
||||
hints.AddRange(["yes", "no", "correct", "right"]);
|
||||
reply.Hints = string.Join(", ", hints.Select(x => x.ToLower()).Distinct().Reverse());
|
||||
reply.Content = null;
|
||||
await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply);
|
||||
return string.Join(", ", hints.Select(x => x.ToLower()).Distinct().Reverse());
|
||||
}
|
||||
|
||||
private static void InitProgressService(CallerMessage message, ITwilioSessionManager sessionManager, IConversationProgressService progressService)
|
||||
|
|
|
|||
Loading…
Reference in a new issue