diff --git a/src/Tests/WhatsAppModelTests.cs b/src/Tests/WhatsAppModelTests.cs index 6449efa..4147ce4 100644 --- a/src/Tests/WhatsAppModelTests.cs +++ b/src/Tests/WhatsAppModelTests.cs @@ -1,4 +1,6 @@ -namespace Devlooped.WhatsApp; +using System.Text.Json; + +namespace Devlooped.WhatsApp; public class WhatsAppModelTests(ITestOutputHelper output) { @@ -176,4 +178,17 @@ public async Task DeserializeReaction() Assert.NotNull(message.User); Assert.Equal("😊", reaction.Emoji); } + + [Fact] + public void SerializeAnonymous() + { + var response = Response.Create("123456789012345", "987654321098765", (client, cancellation) => Task.FromResult(null)); + + var json = JsonSerializer.Serialize(response, JsonContext.DefaultOptions); + + var message = JsonSerializer.Deserialize(json, JsonContext.Default.AnonymousResponse); + + Assert.NotNull(message); + Assert.Null(message.Sender); + } } diff --git a/src/WhatsApp/AnonymousResponse.cs b/src/WhatsApp/AnonymousResponse.cs index 89309fa..48eefd9 100644 --- a/src/WhatsApp/AnonymousResponse.cs +++ b/src/WhatsApp/AnonymousResponse.cs @@ -1,4 +1,6 @@ -namespace Devlooped.WhatsApp; +using System.Text.Json.Serialization; + +namespace Devlooped.WhatsApp; /// /// A response that uses a function to send the message. @@ -15,5 +17,10 @@ public record AnonymousResponse(string ServiceId, string UserNumber, Func> sender) : this(message.ServiceId, message.UserNumber, sender) { } + [JsonConstructor] + internal AnonymousResponse(string ServiceId, string UserNumber) : this(ServiceId, UserNumber, (client, cancellation) => Task.FromResult(null)) + { + } + protected override Task SendCoreAsync(IWhatsAppClient client, CancellationToken cancellation = default) => Sender(client, cancellation); } diff --git a/src/WhatsApp/IMessage.cs b/src/WhatsApp/IMessage.cs index 983db3b..d7c8160 100644 --- a/src/WhatsApp/IMessage.cs +++ b/src/WhatsApp/IMessage.cs @@ -22,6 +22,7 @@ namespace Devlooped.WhatsApp; [JsonDerivedType(typeof(ReactionResponse), "response/reaction")] [JsonDerivedType(typeof(TypingResponse), "response/typing")] [JsonDerivedType(typeof(CallToActionResponse), "response/cta")] +[JsonDerivedType(typeof(AnonymousResponse), "response/dynamic")] public interface IMessage { /// Gets or sets any additional properties associated with the message.