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

Make ApifonIM service supports only Viber #472

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/Indice.Services/SmsServiceApifon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ internal class ResultInfo

internal class ApifonRequest
{
public ApifonRequest() { }

public ApifonRequest(string from, string[] to, string message) {
foreach (var subNumber in to) {
Subscribers.Add(new Subscriber { To = subNumber });
Expand Down
16 changes: 12 additions & 4 deletions src/Indice.Services/SmsServiceApifonIM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Indice.Globalization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using static Indice.Services.ApifonRequest;

namespace Indice.Services;

Expand Down Expand Up @@ -65,12 +66,19 @@ public async Task<SendReceipt> SendAsync(string destination, string subject, str
throw new ArgumentException("Invalid recipients. Recipients cannot contain letters.", nameof(recipients));
}
var senderId = sender?.Id ?? Options.Sender ?? Options.SenderName;
var payload = new ApifonIMRequest(senderId!, recipients, body!) {
var payload = new ApifonIMRequest() {
IMChannels = [new() {
SenderId = senderId,
Text = body
}]
};
foreach (var number in recipients) {
payload.Subscribers.Add(new Subscriber { To = number });
}
if (Options.ViberFallbackEnabled) {
payload.Message.From = senderId!;
payload.Message.Text = body!;
}
var signature = payload.Sign(Options.ApiKey!, HttpMethod.Post.ToString(), SERVICE_ENDPOINT);
var request = new HttpRequestMessage {
Content = new StringContent(payload.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json),
Expand Down Expand Up @@ -113,9 +121,7 @@ public async Task<SendReceipt> SendAsync(string destination, string subject, str
/// <summary>Checks the implementation if supports the given <paramref name="deliveryChannel"/>.</summary>
/// <param name="deliveryChannel">A string representing the delivery channel. i.e 'SMS'</param>
/// <returns></returns>
public bool Supports(string deliveryChannel) =>
"SMS".Equals(deliveryChannel, StringComparison.OrdinalIgnoreCase) ||
"Viber".Equals(deliveryChannel, StringComparison.OrdinalIgnoreCase);
public bool Supports(string deliveryChannel) => "Viber".Equals(deliveryChannel, StringComparison.OrdinalIgnoreCase);

/// <summary>Get default JSON serializer options: CamelCase, ignore null values.</summary>
protected static JsonSerializerOptions GetJsonSerializerOptions() => new JsonSerializerOptions {
Expand All @@ -125,6 +131,8 @@ public bool Supports(string deliveryChannel) =>
}

internal class ApifonIMRequest : ApifonRequest {
public ApifonIMRequest() { }

public ApifonIMRequest(string from, string[] to, string message) : base(from, to, message) { }

[JsonPropertyName("im_channels")]
Expand Down