Skip to content

Commit

Permalink
Merge pull request #78 from Martijnvos/fix/suggestions-missing-derive…
Browse files Browse the repository at this point in the history
…d-types

Add derived types to Suggestions
  • Loading branch information
Alkiimista authored Jan 31, 2025
2 parents 7001c91 + 60d8e6e commit d6b2d46
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.11.1] - 2025-01-31
### Added
- Derived type declarations on `SuggestionBase`

## [2.11.0] - 2025-01-10
### Added
- Context.MessageId on RichMessages supporting WhatsApp for referencing a previous message
Expand Down
123 changes: 123 additions & 0 deletions CM.Text.Tests/SuggestionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Text.Json;
using CM.Text.BusinessMessaging.Model.MultiChannel;
using FluentAssertions;

namespace CM.Text.Tests
{
[TestClass]
public class SuggestionsTests
{
[TestMethod]
public void SerializationCalendarSuggestionTest()
{
var calenderSuggestion = new CalendarSuggestion()
{
Calendar = new CalendarOptions()
{
Title = "Appointment selection",
Description = "Schedule an appointment with us",
EndTime = DateTime.UtcNow,
StartTime = DateTime.UtcNow,
}
};

var serialized= JsonSerializer.Serialize<SuggestionBase>(calenderSuggestion);

serialized.Should().NotBeNullOrWhiteSpace();

var deserialized = JsonSerializer.Deserialize<SuggestionBase>(serialized);

deserialized.Should().BeOfType<CalendarSuggestion>();
var deserializedCalendarSuggestion = deserialized as CalendarSuggestion;
deserializedCalendarSuggestion.Should().BeEquivalentTo(calenderSuggestion);
}

[TestMethod]
public void SerializationDialSuggestionTest()
{
var dialSuggestion = new DialSuggestion()
{
Dial = new Dial()
{
PhoneNumber = "0031612345678"
}
};

var serialized= JsonSerializer.Serialize<SuggestionBase>(dialSuggestion);

serialized.Should().NotBeNullOrWhiteSpace();

var deserialized = JsonSerializer.Deserialize<SuggestionBase>(serialized);

deserialized.Should().BeOfType<DialSuggestion>();
var deserializedDialSuggestion = deserialized as DialSuggestion;
deserializedDialSuggestion.Should().BeEquivalentTo(dialSuggestion);
}

[TestMethod]
public void SerializationOpenUrlSuggestionTest()
{
var openUrlSuggestion = new OpenUrlSuggestion()
{
Url = "https://www.cm.com"
};

var serialized= JsonSerializer.Serialize<SuggestionBase>(openUrlSuggestion);

serialized.Should().NotBeNullOrWhiteSpace();

var deserialized = JsonSerializer.Deserialize<SuggestionBase>(serialized);

deserialized.Should().BeOfType<OpenUrlSuggestion>();
var deserializedOpenUrlSuggestion = deserialized as OpenUrlSuggestion;
deserializedOpenUrlSuggestion.Should().BeEquivalentTo(openUrlSuggestion);
}

[TestMethod]
public void SerializationViewLocationSuggestionTest()
{
var viewLocationSuggestion = new ViewLocationSuggestion()
{
Location = new ViewLocationOptions()
{
Label = "CM.com headquarters",
Latitude = "51.602885",
Longitude = "4.7683932",
SearchQuery = "CM.com headquarters",
Radius = 5
}
};

var serialized= JsonSerializer.Serialize<SuggestionBase>(viewLocationSuggestion);

serialized.Should().NotBeNullOrWhiteSpace();

var deserialized = JsonSerializer.Deserialize<SuggestionBase>(serialized);

deserialized.Should().BeOfType<ViewLocationSuggestion>();
var deserializedViewLocationSuggestion = deserialized as ViewLocationSuggestion;
deserializedViewLocationSuggestion.Should().BeEquivalentTo(viewLocationSuggestion);
}

[TestMethod]
public void SerializationReplySuggestionTest()
{
var replySuggestion = new ReplySuggestion()
{
Label = "Some label",
PostbackData = "LABEL",
Description = "Description of the label",
Media = new MediaContent("Test image", "https://example.com", "image/jpg")
};
var serialized= JsonSerializer.Serialize<SuggestionBase>(replySuggestion);

serialized.Should().NotBeNullOrWhiteSpace();

var deserialized = JsonSerializer.Deserialize<SuggestionBase>(serialized);

deserialized.Should().BeOfType<ReplySuggestion>();
var deserializedReplySuggestion = deserialized as ReplySuggestion;
deserializedReplySuggestion.Should().BeEquivalentTo(replySuggestion);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class CalendarOptions
/// <summary>
/// The end of the appointment.
/// </summary>
[JsonPropertyName("endTime")] public DateTime EndTime;
[JsonPropertyName("endTime")]
public DateTime EndTime { get; set; }

/// <summary>
/// The start of the appointment.
/// </summary>
[JsonPropertyName("startTime")] public DateTime StartTime;
[JsonPropertyName("startTime")]
public DateTime StartTime { get; set; }

/// <summary>
/// The description which will appear in the calendar app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ namespace CM.Text.BusinessMessaging.Model.MultiChannel
[PublicAPI]
public class CalendarSuggestion : SuggestionBase
{
/// <summary>
/// The action of this suggestion
/// </summary>
[JsonPropertyName("action")]
public override string Action => "CreateCalendarEvent";

/// <summary>
/// The options of the agenda item
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ namespace CM.Text.BusinessMessaging.Model.MultiChannel
[PublicAPI]
public class DialSuggestion : SuggestionBase
{
/// <summary>
/// The action of this suggestion
/// </summary>
[JsonPropertyName("action")]
public override string Action => "Dial";

/// <summary>
/// The dial options
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ namespace CM.Text.BusinessMessaging.Model.MultiChannel
[PublicAPI]
public class OpenUrlSuggestion : SuggestionBase
{
/// <summary>
/// The action of this suggestion
/// </summary>
[JsonPropertyName("action")]
public override string Action => "openUrl";

/// <summary>
/// The url the end user can open
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ namespace CM.Text.BusinessMessaging.Model.MultiChannel
[PublicAPI]
public class ReplySuggestion : SuggestionBase
{
/// <summary>
/// The action of this suggestion
/// </summary>
[JsonPropertyName("action")]
public override string Action => "reply";

/// <summary>
/// Description of the Reply suggestion
/// </summary>
Expand Down
14 changes: 8 additions & 6 deletions CM.Text/BusinessMessaging/Model/MultiChannel/SuggestionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ namespace CM.Text.BusinessMessaging.Model.MultiChannel
/// <summary>
/// Suggestions can be used in several channels, not all channels
/// support all suggestions.
///
/// Requires a json derived type for serialization to work
/// </summary>
[PublicAPI]
[JsonPolymorphic(TypeDiscriminatorPropertyName = "action")]
[JsonDerivedType(typeof(CalendarSuggestion), "CreateCalendarEvent")]
[JsonDerivedType(typeof(DialSuggestion), "Dial")]
[JsonDerivedType(typeof(OpenUrlSuggestion), "openUrl")]
[JsonDerivedType(typeof(ReplySuggestion), "reply")]
[JsonDerivedType(typeof(ViewLocationSuggestion), "viewLocation")]
public abstract class SuggestionBase
{
/// <summary>
/// The action of this suggestion
/// </summary>
[JsonPropertyName("action")]
public virtual string Action { get; }

/// <summary>
/// The text the end user will see
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ namespace CM.Text.BusinessMessaging.Model.MultiChannel
[PublicAPI]
public class ViewLocationSuggestion : SuggestionBase
{
/// <summary>
/// The action of this suggestion
/// </summary>
[JsonPropertyName("action")]
public override string Action => "viewLocation";

/// <summary>
/// The location options
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions CM.Text/CM.Text.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../CHANGELOG.md"))</PackageReleaseNotes>
<Version>2.11.0</Version>
<Version>2.11.1</Version>
<PackageProjectUrl>https://github.com/cmdotcom/text-sdk-dotnet</PackageProjectUrl>
<NeutralLanguage>en</NeutralLanguage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyVersion>2.11.0</AssemblyVersion>
<FileVersion>2.11.0</FileVersion>
<AssemblyVersion>2.11.1</AssemblyVersion>
<FileVersion>2.11.1</FileVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down

0 comments on commit d6b2d46

Please sign in to comment.