Skip to content

Commit

Permalink
Added support for SPM Template Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed Oct 23, 2024
1 parent fa055a5 commit 3ed07cf
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
18 changes: 18 additions & 0 deletions WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,24 @@ public interface IWhatsAppBusinessClient
/// <returns>WhatsAppResponse</returns>
WhatsAppResponse SendMPMTemplate(MultiProductTemplateMessageRequest multiProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default);

/// <summary>
/// Send SPM Template Message
/// </summary>
/// <param name="singleProductTemplateMessageRequest">SingleProductTemplateMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
Task<WhatsAppResponse> SendSPMTemplateAsync(SingleProductTemplateMessageRequest singleProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default);

/// <summary>
/// Send SPM Template Message
/// </summary>
/// <param name="singleProductTemplateMessageRequest">SingleProductTemplateMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
WhatsAppResponse SendSPMTemplate(SingleProductTemplateMessageRequest singleProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default);

/// <summary>
/// Send Catalog Template Message
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace WhatsappBusiness.CloudApi.Messages.Requests
{
public class SingleProductTemplateMessageRequest
{
[JsonProperty("messaging_product")]
public string MessagingProduct { get; private set; } = "whatsapp";

[JsonProperty("recipient_type")]
public string RecipientType { get; private set; } = "individual";

[JsonProperty("to")]
public string To { get; set; }

[JsonProperty("type")]
public string Type { get; private set; } = "template";

[JsonProperty("template")]
public SPMTemplate Template { get; set; }
}

public class SPMTemplate
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("language")]
public SPMLanguage Language { get; set; }

[JsonProperty("components")]
public List<SPMComponent> Components { get; set; }
}

public class SPMComponent
{
[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("parameters")]
public List<SPMParameter> Parameters { get; set; }

[JsonProperty("format", NullValueHandling = NullValueHandling.Ignore)]
public string Format { get; set; }

[JsonProperty("text", NullValueHandling = NullValueHandling.Ignore)]
public string Text { get; set; }

[JsonProperty("buttons", NullValueHandling = NullValueHandling.Ignore)]
public List<SPMButtons> Buttons { get; set; }
}

public class SPMParameter
{
[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("text", NullValueHandling = NullValueHandling.Ignore)]
public string Text { get; set; }

[JsonProperty("product", NullValueHandling = NullValueHandling.Ignore)]
public SPMProduct Product { get; set; }
}

public class SPMProduct
{
[JsonProperty("product_retailer_id")]
public string ProductId { get; set; }

[JsonProperty("catalog_id")]
public string CatalogId { get; set; }
}

public class SPMButtons
{
[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("text")]
public string Text { get; set; }
}

public class SPMLanguage
{
[JsonProperty("policy")]
public string Policy { get; set; }

[JsonProperty("code")]
public string Code { get; set; }
}
}
36 changes: 36 additions & 0 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,42 @@ public WhatsAppResponse SendMPMTemplate(MultiProductTemplateMessageRequest multi
return WhatsAppBusinessPostAsync<WhatsAppResponse>(multiProductTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
/// Send SPM Template Message
/// </summary>
/// <param name="singleProductTemplateMessageRequest">SingleProductTemplateMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
public async Task<WhatsAppResponse> SendSPMTemplateAsync(SingleProductTemplateMessageRequest singleProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default)
{
if (cloudApiConfig is not null)
{
_whatsAppConfig = cloudApiConfig;
}

var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.SendMessage.Replace("{{Phone-Number-ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId);
return await WhatsAppBusinessPostAsync<WhatsAppResponse>(singleProductTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken);
}

/// <summary>
/// Send SPM Template Message
/// </summary>
/// <param name="singleProductTemplateMessageRequest">SingleProductTemplateMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
public WhatsAppResponse SendSPMTemplate(SingleProductTemplateMessageRequest singleProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default)
{
if (cloudApiConfig is not null)
{
_whatsAppConfig = cloudApiConfig;
}

var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.SendMessage.Replace("{{Phone-Number-ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId);
return WhatsAppBusinessPostAsync<WhatsAppResponse>(singleProductTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
/// Send Catalog Template Message
/// </summary>
Expand Down

0 comments on commit 3ed07cf

Please sign in to comment.