Skip to content
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
17 changes: 16 additions & 1 deletion src/Tests/WhatsAppModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Devlooped.WhatsApp;
using System.Text.Json;

namespace Devlooped.WhatsApp;

public class WhatsAppModelTests(ITestOutputHelper output)
{
Expand Down Expand Up @@ -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<string?>(null));

var json = JsonSerializer.Serialize(response, JsonContext.DefaultOptions);

var message = JsonSerializer.Deserialize(json, JsonContext.Default.AnonymousResponse);

Assert.NotNull(message);
Assert.Null(message.Sender);
}
}
9 changes: 8 additions & 1 deletion src/WhatsApp/AnonymousResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Devlooped.WhatsApp;
using System.Text.Json.Serialization;

namespace Devlooped.WhatsApp;

/// <summary>
/// A response that uses a function to send the message.
Expand All @@ -15,5 +17,10 @@ public record AnonymousResponse(string ServiceId, string UserNumber, Func<IWhats
public AnonymousResponse(IMessage message, Func<IWhatsAppClient, CancellationToken, Task<string?>> sender)
: this(message.ServiceId, message.UserNumber, sender) { }

[JsonConstructor]
internal AnonymousResponse(string ServiceId, string UserNumber) : this(ServiceId, UserNumber, (client, cancellation) => Task.FromResult<string?>(null))
{
}

protected override Task<string?> SendCoreAsync(IWhatsAppClient client, CancellationToken cancellation = default) => Sender(client, cancellation);
}
1 change: 1 addition & 0 deletions src/WhatsApp/IMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>Gets or sets any additional properties associated with the message.</summary>
Expand Down