From e894adcde916054566ea6090fcdc296c9b539762 Mon Sep 17 00:00:00 2001 From: Gabriel Odero Date: Fri, 23 Dec 2022 11:01:31 +0300 Subject: [PATCH] Optional media ownership using phone number id --- .../Interfaces/IWhatsAppBusinessClient.cs | 16 +++--- .../WhatsAppBusinessClient.cs | 52 ++++++++++++++++--- .../WhatsAppBusinessRequestEndpoint.cs | 10 ++++ .../WhatsappBusiness.CloudApi.csproj | 6 +-- 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs index a1d3073..3e2b411 100644 --- a/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs @@ -146,36 +146,40 @@ public interface IWhatsAppBusinessClient MediaUploadResponse UploadMedia(UploadMediaRequest uploadMediaRequest, CancellationToken cancellationToken = default); /// - /// To retrieve your media’s URL, make a GET call to /{{Media-ID}}. Later, you can use this URL to download the media file. + /// To retrieve your media’s URL, make a GET call to /{{Media-ID}}?phone_number_id=. Later, you can use this URL to download the media file. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// MediaUrlResponse - Task GetMediaUrlAsync(string mediaId, CancellationToken cancellationToken = default); + Task GetMediaUrlAsync(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default); /// - /// To retrieve your media’s URL, make a GET call to /{{Media-ID}}. Later, you can use this URL to download the media file. + /// To retrieve your media’s URL, make a GET call to /{{Media-ID}}?phone_number_id=. Later, you can use this URL to download the media file. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// MediaUrlResponse - MediaUrlResponse GetMediaUrl(string mediaId, CancellationToken cancellationToken = default); + MediaUrlResponse GetMediaUrl(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default); /// /// To delete media, make a DELETE call to the ID of the media you want to delete. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// BaseSuccessResponse - Task DeleteMediaAsync(string mediaId, CancellationToken cancellationToken = default); + Task DeleteMediaAsync(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default); /// /// To delete media, make a DELETE call to the ID of the media you want to delete. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// BaseSuccessResponse - BaseSuccessResponse DeleteMedia(string mediaId, CancellationToken cancellationToken = default); + BaseSuccessResponse DeleteMedia(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default); #endregion #region Phone Numbers functions diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs index 5436b78..7932f29 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs @@ -159,11 +159,20 @@ public async Task CreateWABASubscriptionAsync(string whatsA /// To delete media, make a DELETE call to the ID of the media you want to delete. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// BaseSuccessResponse - public BaseSuccessResponse DeleteMedia(string mediaId, CancellationToken cancellationToken = default) + public BaseSuccessResponse DeleteMedia(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default) { - var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DeleteMedia.Replace("{{Media-ID}}", mediaId); + string formattedWhatsAppEndpoint; + if (isMediaOwnershipVerified) + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DeleteMediaOwnership.Replace("{{Media-ID}}", mediaId).Replace("{{PHONE_NUMBER_ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId); + } + else + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DeleteMedia.Replace("{{Media-ID}}", mediaId); + } return WhatsAppBusinessDeleteAsync(formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult(); } @@ -171,11 +180,20 @@ public BaseSuccessResponse DeleteMedia(string mediaId, CancellationToken cancell /// To delete media, make a DELETE call to the ID of the media you want to delete. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// BaseSuccessResponse - public async Task DeleteMediaAsync(string mediaId, CancellationToken cancellationToken = default) + public async Task DeleteMediaAsync(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default) { - var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DeleteMedia.Replace("{{Media-ID}}", mediaId); + string formattedWhatsAppEndpoint; + if (isMediaOwnershipVerified) + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DeleteMediaOwnership.Replace("{{Media-ID}}", mediaId).Replace("{{PHONE_NUMBER_ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId); + } + else + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DeleteMedia.Replace("{{Media-ID}}", mediaId); + } return await WhatsAppBusinessDeleteAsync(formattedWhatsAppEndpoint, cancellationToken); } @@ -255,11 +273,20 @@ public async Task GetBusinessProfileIdAsync(string what /// To retrieve your media’s URL, make a GET call to /{{Media-ID}}. Later, you can use this URL to download the media file. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// MediaUrlResponse - public MediaUrlResponse GetMediaUrl(string mediaId, CancellationToken cancellationToken = default) + public MediaUrlResponse GetMediaUrl(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default) { - var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.GetMediaUrl.Replace("{{Media-ID}}", mediaId); + string formattedWhatsAppEndpoint; + if (isMediaOwnershipVerified) + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.GetMediaUrlOwnership.Replace("{{Media-ID}}", mediaId).Replace("{{PHONE_NUMBER_ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId); + } + else + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.GetMediaUrl.Replace("{{Media-ID}}", mediaId); + } return WhatsAppBusinessGetAsync(formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult(); } @@ -267,11 +294,20 @@ public MediaUrlResponse GetMediaUrl(string mediaId, CancellationToken cancellati /// To retrieve your media’s URL, make a GET call to /{{Media-ID}}. Later, you can use this URL to download the media file. /// /// ID for the media to send a media message or media template message to your customers. + /// Verify the media ownership using PHONE_NUMBER_ID /// Cancellation token /// MediaUrlResponse - public async Task GetMediaUrlAsync(string mediaId, CancellationToken cancellationToken = default) + public async Task GetMediaUrlAsync(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default) { - var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.GetMediaUrl.Replace("{{Media-ID}}", mediaId); + string formattedWhatsAppEndpoint; + if (isMediaOwnershipVerified) + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.GetMediaUrlOwnership.Replace("{{Media-ID}}", mediaId).Replace("{{PHONE_NUMBER_ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId); + } + else + { + formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.GetMediaUrl.Replace("{{Media-ID}}", mediaId); + } return await WhatsAppBusinessGetAsync(formattedWhatsAppEndpoint, cancellationToken); } diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs index 1ef3315..7e16290 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs @@ -65,11 +65,21 @@ public static class WhatsAppBusinessRequestEndpoint /// public static string GetMediaUrl { get; private set; } = "{{Media-ID}}"; + /// + /// To retrieve media URL with phone number id ownership + /// + public static string GetMediaUrlOwnership { get; private set; } = "{{Media-ID}}?phone_number_id={{PHONE_NUMBER_ID}}"; + /// /// Delete media /// public static string DeleteMedia { get; private set; } = "{{Media-ID}}"; + /// + /// Delete Media with phone number id ownership + /// + public static string DeleteMediaOwnership { get; private set; } = "{{Media-ID}}/?phone_number_id={{PHONE_NUMBER_ID}}"; + /// /// Endpoint to send WhatsApp Messages /// diff --git a/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj b/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj index 9737ed6..6ec95ca 100644 --- a/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj +++ b/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj @@ -12,14 +12,14 @@ https://github.com/gabrieldwight/Whatsapp-Business-Cloud-Api-Net enable latest - 1.0.11 + 1.0.12 - - + +