From 46710c14cc5fda5d1a7e4836e7e00762dbe94a0f Mon Sep 17 00:00:00 2001 From: Yanan Wang <103942@smsassist.com> Date: Fri, 27 Oct 2023 14:05:18 -0500 Subject: [PATCH] Add richContent for future UI rendering --- .../BotSharp.Abstraction.csproj | 4 ---- .../Conversations/IConversationStateService.cs | 1 + .../Conversations/Models/RoleDialogModel.cs | 2 ++ .../Models/RichContent/QuickReplyElement.cs | 13 +++++++++++++ .../Models/RichContent/QuickReplyMessage.cs | 9 +++++++++ .../Messaging/Models/RichContent/Recipient.cs | 8 ++++++++ .../Messaging/Models/RichContent/RichContent.cs | 13 +++++++++++++ .../RichContent/Template/AttachmentTemplate.cs | 9 +++++++++ .../RichContent/Template/ButtonTemplate.cs | 16 ++++++++++++++++ .../RichContent/Template/CommonTemplate.cs | 9 +++++++++ .../RichContent/Template/TemplateMessage.cs | 11 +++++++++++ .../Messaging/Models/RichContent/TextMessage.cs | 8 ++++++++ .../Services/ConversationStateService.cs | 2 ++ .../Controllers/ConversationController.cs | 2 ++ .../Conversations/MessageResponseModel.cs | 1 + 15 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyElement.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Recipient.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/RichContent.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/AttachmentTemplate.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplate.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CommonTemplate.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/TemplateMessage.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj index 18ddee55..ba1ea69d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj +++ b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj @@ -31,8 +31,4 @@ - - - - diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index 26655e2b..07b713c9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Conversations; /// public interface IConversationStateService { + string GetConversationId(); ConversationState Load(string conversationId); string GetState(string name, string defaultValue = ""); bool ContainsState(string name); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index ae798160..fcfada99 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -27,6 +27,8 @@ public class RoleDialogModel /// [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public object Data { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public object? RichContent { get; set; } /// /// Stop conversation completion diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyElement.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyElement.cs new file mode 100644 index 00000000..15a07d23 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyElement.cs @@ -0,0 +1,13 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent +{ + public class QuickReplyElement + { + [JsonPropertyName("content_type")] + public string ContentType { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public string? Payload { get; set; } + [JsonPropertyName("image_url")] + public string? ImageUrl { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs new file mode 100644 index 00000000..b14b27e1 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs @@ -0,0 +1,9 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent +{ + public class QuickReplyMessage : TextMessage + { + [JsonPropertyName("quick_replies")] + public List QuickReplies { get; set; } = new List(); + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Recipient.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Recipient.cs new file mode 100644 index 00000000..7bfbba8e --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Recipient.cs @@ -0,0 +1,8 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent +{ + public class Recipient + { + public string? Id { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/RichContent.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/RichContent.cs new file mode 100644 index 00000000..69224e42 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/RichContent.cs @@ -0,0 +1,13 @@ +namespace BotSharp.Abstraction.Messaging.Models.RichContent +{ + public class RichContent + { + public Recipient? Recipient { get; set; } + /// + /// RESPONSE + /// + [JsonPropertyName("messaging_type")] + public string MessageingType { get; set; } = string.Empty; + public T Message { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/AttachmentTemplate.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/AttachmentTemplate.cs new file mode 100644 index 00000000..89f2137e --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/AttachmentTemplate.cs @@ -0,0 +1,9 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template +{ + public class AttachmentTemplate + { + public string Type => "template"; + public T Payload { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplate.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplate.cs new file mode 100644 index 00000000..e9817729 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplate.cs @@ -0,0 +1,16 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template +{ + public class ButtonTemplate : TextMessage + { + public string TemplateType => "button"; + public List Buttons { get; set; } = new List(); + } + + public class ButtonElement + { + public string? Type { get; set; } + public string? Url { get; set; } + public string? Title { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CommonTemplate.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CommonTemplate.cs new file mode 100644 index 00000000..ace1daac --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CommonTemplate.cs @@ -0,0 +1,9 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template +{ + public class CommonTemplate + { + public string TemplateType { get; set; } = string.Empty; + public T Elements { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/TemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/TemplateMessage.cs new file mode 100644 index 00000000..21b8d339 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/TemplateMessage.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template +{ + public class TemplateMessage + { + public AttachmentTemplate Attachment { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs new file mode 100644 index 00000000..bed603a2 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs @@ -0,0 +1,8 @@ + +namespace BotSharp.Abstraction.Messaging.Models.RichContent +{ + public class TextMessage + { + public string? Text { get; set; } + } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 1f0fe199..7004b929 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -28,6 +28,8 @@ public class ConversationStateService : IConversationStateService, IDisposable _states = new ConversationState(); } + public string GetConversationId() => _conversationId; + public IConversationStateService SetState(string name, T value) { if (value == null) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 3ca6ca9c..fb73fdf6 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -66,11 +66,13 @@ public class ConversationController : ControllerBase, IApiAdapter { response.Function = fnExecuted.FunctionName; response.Data = fnExecuted.Data; + response.RichContent = fnExecuted.RichContent; }); response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content)); response.Data = response.Data ?? stackMsg.Last().Data; response.Function = stackMsg.Last().FunctionName; + response.RichContent = response.RichContent ?? stackMsg.Last().RichContent; return response; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs index 5f7241c0..f5045ed1 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs @@ -5,4 +5,5 @@ public class MessageResponseModel public string Text { get; set; } public string Function { get; set; } public object Data { get; set; } + public object? RichContent { get; set; } }