From 7fd1a96e280956fc5dbc6d66732eb5e428942040 Mon Sep 17 00:00:00 2001 From: hchen2020 <101423@smsassist.com> Date: Tue, 29 Aug 2023 15:31:53 -0500 Subject: [PATCH] Switch routing-profile per channel. --- .../Routing/Models/RoutingProfileRecord.cs | 12 ++++++++++++ .../BotSharp.Core/Routing/Router.cs | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs new file mode 100644 index 00000000..753881ec --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingProfileRecord.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace BotSharp.Abstraction.Routing.Models; + +public class RoutingProfileRecord +{ + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("agent_ids")] + public string[] AgentIds { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Routing/Router.cs b/src/Infrastructure/BotSharp.Core/Routing/Router.cs index 594ee734..69ff0c99 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Router.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Router.cs @@ -1,7 +1,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Routing.Models; using System.IO; -using static Tensorflow.ApiDef.Types; namespace BotSharp.Core.Routing; @@ -33,7 +32,23 @@ public class Router : IAgentRouting var agentSettings = _services.GetRequiredService(); var dbSettings = _services.GetRequiredService(); var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json"); - return JsonSerializer.Deserialize(File.ReadAllText(filePath)); + var records = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + + // check if routing profile is specified + filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json"); + if (File.Exists(filePath)) + { + var state = _services.GetRequiredService(); + var name = state.GetState("channel"); + var profiles = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + var spcificedProfile = profiles.FirstOrDefault(x => x.Name == name); + if (spcificedProfile != null) + { + records = records.Where(x => spcificedProfile.AgentIds.Contains(x.AgentId)).ToArray(); + } + } + + return records; } public RoutingRecord GetRecordByName(string name)