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/deserialization of callback events #92

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public ActionResult Handle([FromBody] JsonObject json)
_logger.LogError("Failed to authorize received callback.");
return Unauthorized();
}

// you can use System.Text.Json Deserialize<T> method
// ReSharper disable once RedundantAssignment
var callbackEvent = json.Deserialize<ICallbackEvent>();
// or use Deserialize provided by SDK
callbackEvent = _sinch.Conversation.Webhooks.DeserializeCallbackEvent(json);
// do something with specific event
switch (callbackEvent)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Sinch/Conversation/Common/ChannelIdentity.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace Sinch.Conversation.Common
{
public class ChannelIdentity
Expand All @@ -8,18 +10,21 @@ public class ChannelIdentity
/// different channel identities on different Conversation API apps. These can be thought of as virtual identities that
/// are app-specific and, therefore, the app_id must be included in the API call.
/// </summary>
[JsonPropertyName("app_id")]
public string? AppId { get; set; }

/// <summary>
/// The channel identity. This will differ from channel to channel. For example, a phone number for SMS, WhatsApp, and
/// Viber Business.
/// </summary>
[JsonPropertyName("identity")]
public string? Identity { get; set; }

/// <summary>
/// The identifier of the channel you want to include. Must be one of the enum values. See
/// <see cref="ConversationChannel" /> fields.
/// </summary>
[JsonPropertyName("channel")]
public ConversationChannel? Channel { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/Sinch/Conversation/Contacts/Contact.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
using Sinch.Conversation.Common;
using Sinch.Core;

Expand All @@ -19,6 +20,7 @@ public sealed class Contact : PropertyMaskQuery
/// <summary>
/// List of channel identities.
/// </summary>
[JsonPropertyName("channel_identities")]
public List<ChannelIdentity>? ChannelIdentities
{
get => _channelIdentities;
Expand All @@ -33,6 +35,7 @@ public List<ChannelIdentity>? ChannelIdentities
/// <summary>
/// List of channels defining the channel priority.
/// </summary>
[JsonPropertyName("channel_priority")]
public List<ConversationChannel>? ChannelPriority
{
get => _channelPriority;
Expand All @@ -47,6 +50,7 @@ public List<ConversationChannel>? ChannelPriority
/// <summary>
/// The display name. A default &#39;Unknown&#39; will be assigned if left empty.
/// </summary>
[JsonPropertyName("display_name")]
public string? DisplayName
{
get => _displayName;
Expand All @@ -61,6 +65,7 @@ public string? DisplayName
/// <summary>
/// Email of the contact.
/// </summary>
[JsonPropertyName("email")]
public string? Email
{
get => _email;
Expand All @@ -75,6 +80,7 @@ public string? Email
/// <summary>
/// Contact identifier in an external system.
/// </summary>
[JsonPropertyName("external_id")]
public string? ExternalId
{
get => _externalId;
Expand All @@ -89,6 +95,7 @@ public string? ExternalId
/// <summary>
/// The ID of the contact.
/// </summary>
[JsonPropertyName("id")]
public string? Id
{
get => _id;
Expand All @@ -103,6 +110,7 @@ public string? Id
/// <summary>
/// Gets or Sets Language
/// </summary>
[JsonPropertyName("language")]
public ConversationLanguage? Language
{
get => _language;
Expand All @@ -117,6 +125,7 @@ public ConversationLanguage? Language
/// <summary>
/// Metadata associated with the contact. Up to 1024 characters long.
/// </summary>
[JsonPropertyName("metadata")]
public string? Metadata
{
get => _metadata;
Expand Down
9 changes: 9 additions & 0 deletions src/Sinch/Conversation/Conversations/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public sealed class Conversation : PropertyMaskQuery
/// <summary>
/// Gets or Sets ActiveChannel
/// </summary>
[JsonPropertyName("active_channel")]
public ConversationChannel? ActiveChannel
{
get => _activeChannel;
Expand All @@ -38,6 +39,7 @@ public ConversationChannel? ActiveChannel
/// <summary>
/// Flag for whether this conversation is active.
/// </summary>
[JsonPropertyName("active")]
public bool? Active
{
get => _active;
Expand All @@ -51,6 +53,7 @@ public bool? Active
/// <summary>
/// The ID of the participating app.
/// </summary>
[JsonPropertyName("app_id")]
public string? AppId
{
get => _appId;
Expand All @@ -64,6 +67,7 @@ public string? AppId
/// <summary>
/// The ID of the participating contact.
/// </summary>
[JsonPropertyName("contact_id")]
public string? ContactId
{
get => _contactId;
Expand All @@ -77,17 +81,20 @@ public string? ContactId
/// <summary>
/// The ID of the conversation.
/// </summary>
[JsonPropertyName("id")]
public string? Id { get; set; }

/// <summary>
/// The timestamp of the latest message in the conversation.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[JsonPropertyName("last_received")]
public DateTime LastReceived { get; set; }

/// <summary>
/// Arbitrary data set by the Conversation API clients. Up to 1024 characters long.
/// </summary>
[JsonPropertyName("metadata")]
public string? Metadata
{
get => _metadata;
Expand All @@ -102,6 +109,7 @@ public string? Metadata
/// Arbitrary data set by the Conversation API clients and/or provided in the conversation_metadata field of a
/// SendMessageRequest. A valid JSON object.
/// </summary>
[JsonPropertyName("metadata_json")]
public JsonObject? MetadataJson
{
get => _metadataJson;
Expand All @@ -112,6 +120,7 @@ public JsonObject? MetadataJson
}
}

[JsonPropertyName("correlation_id")]
public string? CorrelationId
{
get => _correlationId;
Expand Down
3 changes: 2 additions & 1 deletion src/Sinch/Conversation/Hooks/CallbackEventBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Sinch.Conversation.Hooks
Expand Down Expand Up @@ -35,7 +36,7 @@ public abstract class CallbackEventBase : ICallbackEvent
/// Context-dependent metadata. Refer to specific callback&#39;s documentation for exact information provided.
/// </summary>
[JsonPropertyName("message_metadata")]
public string? MessageMetadata { get; set; }
public JsonNode? MessageMetadata { get; set; }


/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Sinch/Conversation/Messages/Message/ContactMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("choice_response_message")]
public ChoiceResponseMessage? ChoiceResponseMessage { get; private set; }


Expand All @@ -61,6 +62,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("fallback_message")]
public FallbackMessage? FallbackMessage { get; private set; }


Expand All @@ -69,6 +71,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("location_message")]
public LocationMessage? LocationMessage { get; private set; }


Expand All @@ -77,6 +80,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("media_card_message")]
public MediaCarouselMessage? MediaCardMessage { get; private set; }


Expand All @@ -85,6 +89,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("media_message")]
public MediaMessage? MediaMessage { get; private set; }


Expand All @@ -93,6 +98,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("reply_to")]
public ReplyTo? ReplyTo { get; private set; }


Expand All @@ -101,6 +107,7 @@ public ContactMessage(TextMessage textMessage)
/// </summary>
[JsonInclude]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("text_message")]
public TextMessage? TextMessage { get; private set; }


Expand Down
4 changes: 4 additions & 0 deletions src/Sinch/Conversation/Messages/Message/FallbackMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.Json.Serialization;

namespace Sinch.Conversation.Messages.Message
{
Expand Down Expand Up @@ -42,18 +43,21 @@ public sealed class Reason
/// <summary>
/// Gets or Sets Code
/// </summary>
[JsonPropertyName("code")]
public string? Code { get; set; }


/// <summary>
/// A textual description of the reason.
/// </summary>
[JsonPropertyName("description")]
public string? Description { get; set; }


/// <summary>
/// Gets or Sets SubCode
/// </summary>
[JsonPropertyName("sub_code")]
public string? SubCode { get; set; }


Expand Down
10 changes: 7 additions & 3 deletions src/Sinch/Conversation/Messages/Message/MediaMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Text.Json.Serialization;

namespace Sinch.Conversation.Messages.Message
{
Expand All @@ -11,21 +12,24 @@ public sealed class MediaMessage : IOmniMessageOverride
/// <summary>
/// An optional parameter. Will be used where it is natively supported.
/// </summary>
public Uri? ThumbnailUrl { get; set; }
[JsonPropertyName("thumbnail_url")]
public string? ThumbnailUrl { get; set; }


/// <summary>
/// Url to the media file.
/// </summary>
[JsonPropertyName("url")]
#if NET7_0_OR_GREATER
public required Uri Url { get; init; }
public required string Url { get; init; }
#else
public Uri Url { get; set; } = null!;
public string Url { get; set; } = null!;
#endif

/// <summary>
/// Overrides the media file name.
/// </summary>
[JsonPropertyName("filename_override")]
public string? FilenameOverride { get; set; }

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Sinch/Conversation/Messages/Message/TextMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.Json.Serialization;

namespace Sinch.Conversation.Messages.Message
{
Expand Down Expand Up @@ -30,6 +31,8 @@ public override string ToString()
}

/// <summary>Text</summary>
[JsonPropertyName("text")]
[JsonInclude]
public string Text { get; init; }
}
}
Loading
Loading