Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichMessageBase and Welcome message. #220

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public virtual Task OnConversationInitialized(Conversation conversation)
{
return Task.CompletedTask;
}

public virtual Task OnUserAgentConnectedInitially(Conversation conversation)
{
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public interface IConversationHook
Conversation Conversation { get; }
IConversationHook SetConversation(Conversation conversation);

/// <summary>
/// Triggered when user connects with agent first time.
/// This hook is the good timing to show welcome infomation.
/// </summary>
/// <param name="conversation"></param>
/// <returns></returns>
Task OnUserAgentConnectedInitially(Conversation conversation);

/// <summary>
/// Triggered once for every new conversation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public interface IConversationService
IConversationStateService States { get; }
string ConversationId { get; }
Task<Conversation> NewConversation(Conversation conversation);
Task MarkConnectionReady(string conversationId);
void SetConversationId(string conversationId, List<string> states);
Task<Conversation> GetConversation(string id);
Task<List<Conversation>> GetConversations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace BotSharp.Abstraction.Messaging;

public interface IRichMessage
{
string Type { get; }
string Text { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class QuickReplyMessage : IRichMessage
public class QuickReplyMessage : RichMessageBase, IRichMessage
{
public string Text { get; set; } = string.Empty;
public override string Type => "quick reply";

[JsonPropertyName("quick_replies")]
public List<QuickReplyElement> QuickReplies { get; set; } = new List<QuickReplyElement>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
/// <summary>
/// https://developers.facebook.com/docs/messenger-platform/send-messages/buttons
/// </summary>
public class ButtonTemplateMessage : IRichMessage
public class ButtonTemplateMessage : RichMessageBase, IRichMessage, ITemplateMessage
{
public string Text { get; set; } = string.Empty;
public override string Type => "template";

[JsonPropertyName("template_type")]
public string TemplateType => "button";
public override string TemplateType => "button";
public List<ButtonElement> Buttons { get; set; } = new List<ButtonElement>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
/// Coupon Template
/// https://developers.facebook.com/docs/messenger-platform/send-messages/template/coupon
/// </summary>
public class CouponTemplateMessage : IRichMessage, ITemplateMessage
public class CouponTemplateMessage : RichMessageBase, IRichMessage, ITemplateMessage
{
public override string Type => "template";

[JsonPropertyName("template_type")]
public override string TemplateType => "coupon";

[JsonIgnore]
public string Text { get; set; }
public override string Text { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }

[JsonPropertyName("template_type")]
public string TemplateType => "coupon";

[JsonPropertyName("coupon_code")]
public string CouponCode { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class MultiSelectTemplateMessage : IRichMessage
public class MultiSelectTemplateMessage : RichMessageBase, IRichMessage
{
public string Text { get; set; } = string.Empty;
public override string Type => "template";

[JsonPropertyName("template_type")]
public string TemplateType => "multi-select";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class ProductTemplateMessage : TemplateMessageBase<ProductElement>, IRichMessage
{
[JsonPropertyName("template_type")]
public override string TemplateType => "product";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class TemplateMessageBase<T>
public class TemplateMessageBase<T> : RichMessageBase
{
[JsonIgnore]
public string Text { get; set; }

public override string Type => "template";

[JsonPropertyName("template_type")]
public virtual string TemplateType => string.Empty;

public T[] Elements { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class TextMessage : IRichMessage
public class TextMessage : RichMessageBase, IRichMessage
{
public string Text { get; set; } = string.Empty;
public string Type => "text";

public TextMessage(string text)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace BotSharp.Abstraction.Messaging;

public class RichMessageBase
{
public virtual string Type { get; }

public virtual string Text { get; set; }

[JsonPropertyName("template_type")]
public virtual string TemplateType { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public async Task<Conversation> NewConversation(Conversation sess)
var hooks = _services.GetServices<IConversationHook>().ToList();
foreach (var hook in hooks)
{
// If user connect agent first time
await hook.OnUserAgentConnectedInitially(sess);

await hook.OnConversationInitialized(record);
}

Expand Down Expand Up @@ -115,4 +118,15 @@ public void SetConversationId(string conversationId, List<string> states)
_state.Load(_conversationId);
states.ForEach(x => _state.SetState(x.Split('=')[0], x.Split('=')[1]));
}

public async Task MarkConnectionReady(string conversationId)
{
var hooks = _services.GetServices<IConversationHook>();
var conv = await GetConversation(conversationId);
foreach (var hook in hooks)
{
// Need to check if user connected with agent is the first time.
await hook.OnUserAgentConnectedInitially(conv);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
return ConversationViewModel.FromSession(conv);
}

[HttpPatch("/conversation/{conversationId}/ready")]
public async Task ReadyToInteractive([FromRoute] string conversationId)
{
var service = _services.GetRequiredService<IConversationService>();
await service.MarkConnectionReady(conversationId);
}

[HttpGet("/conversations/{agentId}")]
public async Task<IEnumerable<ConversationViewModel>> GetConversations()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using System.Text.Json.Serialization;

namespace BotSharp.OpenAPI.ViewModels.Agents;

Expand All @@ -15,12 +16,20 @@ public class AgentViewModel
public List<AgentResponse> Responses { get; set; }
public List<string> Samples { get; set; }
public bool IsPublic { get; set; }

[JsonPropertyName("allow_routing")]
public bool AllowRouting { get; set; }
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }

[JsonPropertyName("routing_rules")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<RoutingRule> RoutingRules { get; set; }

[JsonPropertyName("created_datetime")]
public DateTime CreatedDateTime { get; set; }

[JsonPropertyName("updated_datetime")]
public DateTime UpdatedDateTime { get; set; }

public static AgentViewModel FromAgent(Agent agent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Messaging;
using Microsoft.AspNetCore.SignalR;

namespace BotSharp.Plugin.ChatHub.Hooks;
Expand All @@ -14,6 +15,30 @@ public ChatHubConversationHook(IServiceProvider services,
_chatHub = chatHub;
}

public override async Task OnUserAgentConnectedInitially(Conversation conversation)
{
var agentService = _services.GetService<IAgentService>();
var agent = await agentService.LoadAgent(conversation.AgentId);

// Check if the Welcome template exists.
var welcomeTemplate = agent.Templates.FirstOrDefault(x => x.Name == "welcome");
if (welcomeTemplate != null)
{
var messages = JsonSerializer.Deserialize<RichMessageBase[]>(welcomeTemplate.Content, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});

foreach (var message in messages)
{
await Task.Delay(300);
await OnResponseGenerated(new RoleDialogModel(AgentRole.Assistant, message.Text));
}
}

await base.OnUserAgentConnectedInitially(conversation);
}

public override async Task OnConversationInitialized(Conversation conversation)
{
var userService = _services.GetRequiredService<IUserService>();
Expand Down
22 changes: 0 additions & 22 deletions src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ public SignalRHub(IServiceProvider services,
_user = user;
}

public async Task OnMessageReceivedFromClient(string message)
{
// await Clients.User(_user.Id).SendAsync("ReceiveMessage", message);
}

/// <summary>
/// Received message from client
/// </summary>
/// <param name="user"></param>
/// <param name="message"></param>
/// <returns></returns>
public async Task SendMessage(string user, string message)
{
// await Clients.User(_user.Id).SendAsync("ReceiveMessage", message);
}

public async Task SendMessageToCaller(string user, string message)
=> await Clients.Caller.SendAsync("ReceiveMessage", user, message);

public async Task SendMessageToGroup(string user, string message)
=> await Clients.Group("SignalR Users").SendAsync("ReceiveMessage", user, message);

public override Task OnConnectedAsync()
{
Console.WriteLine($"SignalR Hub: {Context.UserIdentifier} connected in {Context.ConnectionId} [{DateTime.Now}]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>GenerateDocumentationFile</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class AttachmentMessage : IRichMessage

[JsonPropertyName("attachment")]
public AttachmentBody Attachment { get; set; }

public string Type => "template";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"text": "Hello, I'm an AI assistant that help you do variety of tasks."
},
{
"text": "How can I help you today?"
}
]
4 changes: 2 additions & 2 deletions src/web-live-chat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading