From 3ed07cf90bf5a737f7ec276e9d7fc7a9295485f7 Mon Sep 17 00:00:00 2001 From: Gabriel Odero Date: Thu, 24 Oct 2024 01:56:56 +0300 Subject: [PATCH] Added support for SPM Template Messages --- .../Interfaces/IWhatsAppBusinessClient.cs | 18 ++++ .../SingleProductTemplateMessageRequest.cs | 92 +++++++++++++++++++ .../WhatsAppBusinessClient.cs | 36 ++++++++ 3 files changed, 146 insertions(+) create mode 100644 WhatsappBusiness.CloudApi/Messages/Requests/SingleProductTemplateMessageRequest.cs diff --git a/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs index 08b0a1b..27d501b 100644 --- a/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs @@ -858,6 +858,24 @@ public interface IWhatsAppBusinessClient /// WhatsAppResponse WhatsAppResponse SendMPMTemplate(MultiProductTemplateMessageRequest multiProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default); + /// + /// Send SPM Template Message + /// + /// SingleProductTemplateMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + Task SendSPMTemplateAsync(SingleProductTemplateMessageRequest singleProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default); + + /// + /// Send SPM Template Message + /// + /// SingleProductTemplateMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + WhatsAppResponse SendSPMTemplate(SingleProductTemplateMessageRequest singleProductTemplateMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default); + /// /// Send Catalog Template Message /// diff --git a/WhatsappBusiness.CloudApi/Messages/Requests/SingleProductTemplateMessageRequest.cs b/WhatsappBusiness.CloudApi/Messages/Requests/SingleProductTemplateMessageRequest.cs new file mode 100644 index 0000000..65fe7b9 --- /dev/null +++ b/WhatsappBusiness.CloudApi/Messages/Requests/SingleProductTemplateMessageRequest.cs @@ -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 Components { get; set; } + } + + public class SPMComponent + { + [JsonProperty("type")] + public string Type { get; set; } + + [JsonProperty("parameters")] + public List 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 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; } + } +} diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs index 06b6f2e..604aee4 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs @@ -2626,6 +2626,42 @@ public WhatsAppResponse SendMPMTemplate(MultiProductTemplateMessageRequest multi return WhatsAppBusinessPostAsync(multiProductTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult(); } + /// + /// Send SPM Template Message + /// + /// SingleProductTemplateMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + public async Task 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(singleProductTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken); + } + + /// + /// Send SPM Template Message + /// + /// SingleProductTemplateMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + 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(singleProductTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult(); + } + /// /// Send Catalog Template Message ///