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/channel specific message type #51

Merged
merged 8 commits into from
Apr 9, 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
149 changes: 95 additions & 54 deletions src/Sinch/Conversation/Messages/Message/AppMessage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Sinch.Conversation.Common;
using Sinch.Conversation.Events.AppEvents;
using Sinch.Conversation.Messages.Message.ChannelSpecificMessages;
using Sinch.Conversation.Messages.Message.ChannelSpecificMessages.WhatsApp;
using Sinch.Core;

namespace Sinch.Conversation.Messages.Message
{
Expand All @@ -16,6 +20,44 @@ public AppMessage()
{
}

#region Oneof app message props and constructors

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public TextMessage TextMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public CardMessage CardMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public CarouselMessage CarouselMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ChoiceMessage ChoiceMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public LocationMessage LocationMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public MediaMessage MediaMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public TemplateMessage TemplateMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ListMessage ListMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ContactInfoMessage ContactInfoMessage { get; private set; }

public AppMessage(ChoiceMessage choiceMessage)
{
ChoiceMessage = choiceMessage;
Expand Down Expand Up @@ -61,79 +103,78 @@ public AppMessage(ContactInfoMessage contactInfoMessage)
ContactInfoMessage = contactInfoMessage;
}

#endregion

/// <summary>
/// Optional. Channel specific messages, overriding any transcoding.
/// The key in the map must point to a valid conversation channel as defined by the enum ConversationChannel.
/// </summary>
public JsonObject ExplicitChannelMessage { get; set; }
public Dictionary<ConversationChannel, JsonValue> ExplicitChannelMessage { get; set; }

/// <summary>
/// Channel specific messages, overriding any transcoding.
/// The structure of this property is more well-defined than the open structure of
/// the explicit_channel_message property, and may be easier to use.
/// The key in the map must point to a valid conversation channel as defined in the enum ConversationChannel.
/// </summary>
public Dictionary<ConversationChannel, IChannelSpecificMessage> ChannelSpecificMessage { get; set; }

/// <inheritdoc cref="Agent" />
public Agent Agent { get; set; }
}

/// <summary>
/// A message containing a channel specific message (not supported by OMNI types).
/// </summary>
[JsonDerivedType(typeof(FlowMessage))]
[JsonConverter(typeof(ChannelSpecificMessageJsonInterfaceConverter))]
public interface IChannelSpecificMessage
{
/// <summary>
/// Gets or Sets AdditionalProperties
/// Gets or Sets MessageType
/// </summary>
public AppMessageAdditionalProperties AdditionalProperties { get; set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public TextMessage TextMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public CardMessage CardMessage { get; private set; }

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public CarouselMessage CarouselMessage { get; private set; }
public MessageType MessageType { get; }
}

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ChoiceMessage ChoiceMessage { get; private set; }
public class ChannelSpecificMessageJsonInterfaceConverter : JsonConverter<IChannelSpecificMessage>
{
public override IChannelSpecificMessage Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
// not optimal but straightforward
var elem = JsonElement.ParseValue(ref reader);
var descriptor = elem.EnumerateObject().FirstOrDefault(x => x.Name == "message_type");
var method = descriptor.Value.GetString();

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public LocationMessage LocationMessage { get; private set; }
if (MessageType.Flows.Value == method)
return elem.Deserialize<FlowMessage>(options);

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public MediaMessage MediaMessage { get; private set; }
throw new JsonException(
$"Failed to match {nameof(IChannelSpecificMessage)}, got prop `{descriptor.Name}` with value `{method}`");
}

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public TemplateMessage TemplateMessage { get; private set; }
public override void Write(Utf8JsonWriter writer, IChannelSpecificMessage value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, options);
}
}

public class FlowMessage : IChannelSpecificMessage
{
[JsonPropertyName("message_type")]
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ListMessage ListMessage { get; private set; }
public MessageType MessageType { get; private set; } = MessageType.Flows;

[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ContactInfoMessage ContactInfoMessage { get; private set; }
[JsonPropertyName("message")]
public FlowChannelSpecificMessage Message { get; set; }
}

/// <summary>
/// Additional properties of the message.
/// Defines MessageType
/// </summary>
public sealed class AppMessageAdditionalProperties
[JsonConverter(typeof(EnumRecordJsonConverter<MessageType>))]
public record MessageType(string Value) : EnumRecord(Value)
{
/// <summary>
/// The &#x60;display_name&#x60; of the newly created contact in case it doesn&#39;t exist.
/// </summary>
public string ContactName { get; set; }


/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class AppMessageAdditionalProperties {\n");
sb.Append(" ContactName: ").Append(ContactName).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
public static readonly MessageType Flows = new("FLOWS");
}
}
Loading
Loading