Skip to content

Commit

Permalink
fix!: revamped channel provider delivery reports and registration
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyMelton committed Apr 2, 2024
1 parent e1a6268 commit d7cf0bf
Show file tree
Hide file tree
Showing 18 changed files with 524 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using System.Text.Json.Serialization;
namespace Transmitly.ChannelProvider.Infobip
{
sealed class CallbackStatus
public sealed class CallbackStatus
{
[JsonPropertyName("groupId")]
public int? GroupId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using System.Text.Json.Serialization;
namespace Transmitly.ChannelProvider.Infobip
{
sealed class ErrorStatus
public sealed class ErrorStatus
{
[JsonPropertyName("groupId")]
public int? GroupId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using Transmitly.ChannelProvider;
using Transmitly.ChannelProvider.Infobip.Email;
using Transmitly.ChannelProvider.Infobip.Sms;
using Transmitly.ChannelProvider.Infobip.Voice;
Expand Down Expand Up @@ -63,6 +64,16 @@ public static ExtendedVoiceChannelProperties Infobip(this IVoiceChannel email)
return new ExtendedVoiceChannelProperties(email);
}

/// <summary>
/// Infobip specific settings for sms delivery reports.
/// </summary>
/// <param name="deliveryReport">Delivery Report.</param>
/// <returns>Infobip SMS delivery report properties.</returns>
public static ExtendedSmsDeliveryReportProperties Infobip(this DeliveryReport deliveryReport)
{
return new ExtendedSmsDeliveryReportProperties(deliveryReport);
}

/// <summary>
/// Adds channel provider support for Infobip.
/// </summary>
Expand All @@ -78,9 +89,11 @@ public static CommunicationsClientBuilder AddInfobipSupport(this CommunicationsC
communicationsClientBuilder.AddChannelProvider<SmsChannelProviderClient, ISms>(Id.ChannelProvider.Infobip(providerId), optionObj, Id.Channel.Sms());
communicationsClientBuilder.AddChannelProvider<EmailChannelProviderClient, IEmail>(Id.ChannelProvider.Infobip(providerId), optionObj, Id.Channel.Email());
communicationsClientBuilder.AddChannelProvider<VoiceChannelProviderClient, IVoice>(Id.ChannelProvider.Infobip(providerId), optionObj, Id.Channel.Voice());
communicationsClientBuilder.ChannelProvider.AddResponseHandler<SmsCallbackHandler>();
communicationsClientBuilder.ChannelProvider.AddResponseHandler<VoiceCallbackHandler>();
communicationsClientBuilder.ChannelProvider.AddDeliveryReportRequestAdaptor<SmsDeliveryStatusReportAdaptor>();
communicationsClientBuilder.ChannelProvider.AddDeliveryReportRequestAdaptor<VoiceDeliveryStatusReportAdaptor>();
return communicationsClientBuilder;
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright (c) Code Impressions, LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;

namespace Transmitly.ChannelProvider.Infobip.Sms
{
public sealed class ExtendedSmsDeliveryReportProperties
{
private readonly IExtendedProperties _extendedProperties;
private const string ProviderKey = Constant.SmsPropertiesKey;
internal ExtendedSmsDeliveryReportProperties(DeliveryReport deliveryReport)
{
_extendedProperties = Guard.AgainstNull(deliveryReport).ExtendedProperties;
}

internal ExtendedSmsDeliveryReportProperties(IExtendedProperties properties)
{
_extendedProperties = Guard.AgainstNull(properties);
}

internal void Apply(SmsStatusReport report)
{
BulkId = report.BulkId;
MessageId = report.MessageId;
To = report.To;
From = report.From;
SentAt = report.SentAt;
DoneAt = report.DoneAt;
SmsCount = report.SmsCount;
MccMnc = report.MccMnc;
CallbackData = report.CallbackData;
Price = report.Price;
Status = report.Status;
Error = report.Error;
EntityId = report.EntityId;
ApplicationId = report.ApplicationId;
}

/// <summary>
/// Unique ID assigned to the request if messaging multiple recipients
/// or sending multiple messages via a single API request.
/// </summary>
public string? BulkId
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(BulkId));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(BulkId), value);
}

/// <summary>
/// Unique message ID.
/// </summary>
public string? MessageId
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(MessageId));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(MessageId), value);
}

/// <summary>
/// Message destination address.
/// </summary>
public string? To
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(To));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(To), value);
}

/// <summary>
/// The sender ID which can be alphanumeric or numeric (e.g., CompanyName).
/// </summary>
public string? From
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(From));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(From), value);
}

/// <summary>
/// Date and time when the message was scheduled to be sent.
/// Has the following format: yyyy-MM-dd'T'HH:mm:ss.SSSZ.
/// </summary>
public DateTimeOffset? SentAt
{
get => _extendedProperties.GetValue<DateTimeOffset?>(ProviderKey, nameof(SentAt));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(SentAt), value);
}

/// <summary>
/// Date and time when the Infobip services finished processing the message
/// (i.e., delivered to the destination, delivered to the destination network, etc.).
/// Has the following format: yyyy-MM-dd'T'HH:mm:ss.SSSZ.
/// </summary>
public DateTimeOffset? DoneAt
{
get => _extendedProperties.GetValue<DateTimeOffset?>(ProviderKey, nameof(DoneAt));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(DoneAt), value);
}

/// <summary>
/// The number of parts the message content was split into.
/// </summary>
public int? SmsCount
{
get => _extendedProperties.GetValue<int?>(ProviderKey, nameof(SmsCount));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(SmsCount), value);
}

/// <summary>
/// Mobile country and network codes.
/// </summary>
public string? MccMnc
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(MccMnc));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(MccMnc), value);
}

/// <summary>
/// Custom data sent over to the notifyUrl.
/// </summary>
public string? CallbackData
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(CallbackData));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(CallbackData), value);
}

/// <summary>
/// Sent SMS price.
/// </summary>
public SmsPrice? Price
{
get => _extendedProperties.GetValue<SmsPrice?>(ProviderKey, nameof(Price));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(Price), value);
}
/// <summary>
/// Indicates the status of the message and how to recover from an error should there be any.
/// </summary>
public CallbackStatus? Status
{
get => _extendedProperties.GetValue<CallbackStatus?>(ProviderKey, nameof(Status));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(Status), value);
}

/// <summary>
/// Indicates whether an error occurred during the query execution.
/// </summary>
public ErrorStatus? Error
{
get => _extendedProperties.GetValue<ErrorStatus?>(ProviderKey, nameof(Error));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(Error), value);
}

/// <summary>
/// The entity used when sending the message. For more details,
/// see our <see href="https://www.infobip.com/docs/cpaas-x/application-and-entity-management">documentation</see>.
/// </summary>
public string? EntityId
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(EntityId));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(EntityId), value);
}

/// <summary>
/// The application used when sending the message. For more details,
/// see our <see href="https://www.infobip.com/docs/cpaas-x/application-and-entity-management">documentation</see>.
/// </summary>
public string? ApplicationId
{
get => _extendedProperties.GetValue<string?>(ProviderKey, nameof(ApplicationId));
set => _extendedProperties.AddOrUpdate(ProviderKey, nameof(ApplicationId), value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ internal sealed class SmsChannelProviderClient(InfobipChannelProviderConfigurati
private const string SendAdvancedSmsMessage = "sms/2/text/advanced";
private readonly InfobipChannelProviderConfiguration _configuration = configuration;

public override IReadOnlyCollection<string>? RegisteredEvents => [DeliveryReportEvent.Name.Dispatched(), DeliveryReportEvent.Name.Error()];

protected override async Task<IReadOnlyCollection<IDispatchResult?>> DispatchAsync(HttpClient restClient, ISms communication, IDispatchCommunicationContext communicationContext, CancellationToken cancellationToken)
{
Guard.AgainstNull(communication);
Expand Down Expand Up @@ -108,11 +106,11 @@ private async Task<HttpContent> CreateSingleMessageRequestContent(IAudienceAddre

private static async Task<string?> GetNotifyUrl(string messageId, ExtendedSmsChannelProperties voiceProperties, ISms sms, IDispatchCommunicationContext context)
{
var urlResolver = voiceProperties.NotifyUrlResolver ?? sms.StatusCallbackUrlResolver;
var urlResolver = voiceProperties.NotifyUrlResolver ?? sms.DeliveryReportCallbackUrlResolver;
if (urlResolver != null)
return await urlResolver(context).ConfigureAwait(false);

string? url = voiceProperties.NotifyUrl ?? sms.StatusCallbackUrl;
string? url = voiceProperties.NotifyUrl ?? sms.DeliveryReportCallbackUrl;
if (string.IsNullOrWhiteSpace(url))
return null;
return AddParameter(new Uri(url), "resourceId", messageId).ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Code Impressions, LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

namespace Transmitly.ChannelProvider.Infobip.Sms
{
sealed class SmsDeliveryStatusReportAdaptor : IChannelProviderDeliveryReportRequestAdaptor
{
public async Task<IReadOnlyCollection<DeliveryReport>?> AdaptAsync(Stream requestStream)
{
var statuses = await JsonSerializer.DeserializeAsync<SmsStatusReports>(requestStream);

if (statuses?.Results == null)
return null;

var ret = new List<DeliveryReport>(statuses.Results.Count);
foreach (var smsReport in statuses.Results)
{
var report = new DeliveryReport(
DeliveryReport.Event.StatusChanged(),
Id.Channel.Sms(),
Id.ChannelProvider.Infobip(),
null,
smsReport.MessageId,
Util.ToDispatchStatus(smsReport.Status?.GroupId),
null
);

new ExtendedSmsDeliveryReportProperties(report).Apply(smsReport);
ret.Add(report);
}

return ret;
}
}
}
Loading

0 comments on commit d7cf0bf

Please sign in to comment.