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

fix: add specific object into list message #50

Merged
merged 14 commits into from
Apr 25, 2024
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 @@ -5,7 +5,7 @@ namespace Sinch.Conversation.Messages.Message
/// <summary>
/// A message component for interactive messages, containing a choice.
/// </summary>
public class ListItemChoice : IListItem
public class ChoiceItem : IListItem
{
/// <summary>
/// Required parameter. Title for the choice item.
Expand Down
60 changes: 59 additions & 1 deletion src/Sinch/Conversation/Messages/Message/ListMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Sinch.Core;

namespace Sinch.Conversation.Messages.Message
Expand Down Expand Up @@ -90,11 +93,66 @@ public override string ToString()
}
}

[JsonInterfaceConverter(typeof(InterfaceConverter<IListItem>))]
[JsonInterfaceConverter(typeof(ListItemJsonConverter))]
public interface IListItem
{
}

public class ListItemJsonConverter : JsonConverter<IListItem>
{
public override IListItem? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var elem = JsonElement.ParseValue(ref reader);
if (elem.TryGetProperty("choice", out var choice))
{
return choice.Deserialize<ChoiceItem>(options);
}

if (elem.TryGetProperty("product", out var product))
{
return product.Deserialize<ProductItem>(options);
}

throw new JsonException(
$"Failed to match {nameof(IListItem)}, got json element: {elem.ToString()}");
}

public override void Write(Utf8JsonWriter writer, IListItem value, JsonSerializerOptions options)
{
var type = value.GetType();
if (type == typeof(ChoiceItem))
{
JsonSerializer.Serialize(writer, new ListItemChoiceWrapper()
{
Choice = value as ChoiceItem
}, options);
return;
}

if (type == typeof(ProductItem))
{
JsonSerializer.Serialize(writer, new ListItemProductWrapper()
{
Product = value as ProductItem
}, options);
return;
}

throw new InvalidOperationException(
$"Value is not in range of expected types - actual type is {type.FullName}");
}
}

internal class ListItemChoiceWrapper
{
public ChoiceItem? Choice { get; set; }
}

internal class ListItemProductWrapper
{
public ProductItem? Product { get; set; }
}

/// <summary>
/// Additional properties for the message. Required if sending a product list message.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Sinch.Conversation.Messages.Message
/// <summary>
/// A message component for interactive messages, containing a product.
/// </summary>
public sealed class ListItemProduct : IListItem
public sealed class ProductItem : IListItem
{
/// <summary>
/// Required parameter. The ID for the product.
Expand Down
28 changes: 17 additions & 11 deletions tests/Sinch.Tests/Conversation/MessagesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task GetMessage()
Title = "sec1",
Items = new List<IListItem>()
{
new ListItemChoice()
new ChoiceItem()
{
Title = "title",
Description = "desc",
Expand All @@ -59,9 +59,9 @@ public async Task GetMessage()
new ListSection()
{
Title = "sec2",
Items = new List<IListItem>()
Items = new List<IListItem>
{
new ListItemProduct()
new ProductItem()
{
Id = "id",
Marketplace = "amazon"
Expand Down Expand Up @@ -207,13 +207,16 @@ private static object Message()
{
new
{
title = "title",
description = "desc",
media = new
choice = new
{
url = "http://localhost",
},
postback_data = "postback"
title = "title",
description = "desc",
media = new
{
url = "http://localhost",
},
postback_data = "postback"
}
}
}
},
Expand All @@ -224,8 +227,11 @@ private static object Message()
{
new
{
id = "id",
marketplace = "amazon"
product = new
{
id = "id",
marketplace = "amazon"
}
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions tests/Sinch.Tests/Conversation/SendMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,22 +344,28 @@ public async Task SendList()
{
new
{
title = "listitemchoice",
postback_data = "postno",
description = "desc",
media = new
choice = new
{
url = "https://nolocalhost",
thumbnail_url = "https://knowyourmeme.com/photos/377946"
title = "listitemchoice",
postback_data = "postno",
description = "desc",
media = new
{
url = "https://nolocalhost",
thumbnail_url = "https://knowyourmeme.com/photos/377946"
}
}
},
new
{
id = "prod_id",
marketplace = "amazon",
currency = "eur",
quantity = 20,
item_price = 12.1000004f,
product = new
{
id = "prod_id",
marketplace = "amazon",
currency = "eur",
quantity = 20,
item_price = 12.1000004f,
}
}
}
}
Expand All @@ -376,7 +382,7 @@ public async Task SendList()
Title = "item1",
Items = new List<IListItem>()
{
new ListItemChoice()
new ChoiceItem()
{
Title = "listitemchoice",
PostbackData = "postno",
Expand All @@ -387,7 +393,7 @@ public async Task SendList()
ThumbnailUrl = new Uri("https://knowyourmeme.com/photos/377946")
}
},
new ListItemProduct
new ProductItem
{
Id = "prod_id",
Marketplace = "amazon",
Expand Down
Loading