Skip to content

Commit

Permalink
Optional media ownership using phone number id
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed Dec 23, 2022
1 parent 983bebf commit e894adc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
16 changes: 10 additions & 6 deletions WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,40 @@ public interface IWhatsAppBusinessClient
MediaUploadResponse UploadMedia(UploadMediaRequest uploadMediaRequest, CancellationToken cancellationToken = default);

/// <summary>
/// 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=<PHONE_NUMBER_ID>. Later, you can use this URL to download the media file.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>MediaUrlResponse</returns>
Task<MediaUrlResponse> GetMediaUrlAsync(string mediaId, CancellationToken cancellationToken = default);
Task<MediaUrlResponse> GetMediaUrlAsync(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default);

/// <summary>
/// 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=<PHONE_NUMBER_ID>. Later, you can use this URL to download the media file.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>MediaUrlResponse</returns>
MediaUrlResponse GetMediaUrl(string mediaId, CancellationToken cancellationToken = default);
MediaUrlResponse GetMediaUrl(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default);

/// <summary>
/// To delete media, make a DELETE call to the ID of the media you want to delete.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>BaseSuccessResponse</returns>
Task<BaseSuccessResponse> DeleteMediaAsync(string mediaId, CancellationToken cancellationToken = default);
Task<BaseSuccessResponse> DeleteMediaAsync(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default);

/// <summary>
/// To delete media, make a DELETE call to the ID of the media you want to delete.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>BaseSuccessResponse</returns>
BaseSuccessResponse DeleteMedia(string mediaId, CancellationToken cancellationToken = default);
BaseSuccessResponse DeleteMedia(string mediaId, bool isMediaOwnershipVerified = false, CancellationToken cancellationToken = default);
#endregion

#region Phone Numbers functions
Expand Down
52 changes: 44 additions & 8 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,41 @@ public async Task<BaseSuccessResponse> CreateWABASubscriptionAsync(string whatsA
/// To delete media, make a DELETE call to the ID of the media you want to delete.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>BaseSuccessResponse</returns>
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<BaseSuccessResponse>(formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
/// To delete media, make a DELETE call to the ID of the media you want to delete.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>BaseSuccessResponse</returns>
public async Task<BaseSuccessResponse> DeleteMediaAsync(string mediaId, CancellationToken cancellationToken = default)
public async Task<BaseSuccessResponse> 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<BaseSuccessResponse>(formattedWhatsAppEndpoint, cancellationToken);
}

Expand Down Expand Up @@ -255,23 +273,41 @@ public async Task<BusinessProfileResponse> 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.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>MediaUrlResponse</returns>
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<MediaUrlResponse>(formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
/// To retrieve your media’s URL, make a GET call to /{{Media-ID}}. Later, you can use this URL to download the media file.
/// </summary>
/// <param name="mediaId">ID for the media to send a media message or media template message to your customers.</param>
/// <param name="isMediaOwnershipVerified">Verify the media ownership using PHONE_NUMBER_ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>MediaUrlResponse</returns>
public async Task<MediaUrlResponse> GetMediaUrlAsync(string mediaId, CancellationToken cancellationToken = default)
public async Task<MediaUrlResponse> 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<MediaUrlResponse>(formattedWhatsAppEndpoint, cancellationToken);
}

Expand Down
10 changes: 10 additions & 0 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,21 @@ public static class WhatsAppBusinessRequestEndpoint
/// </summary>
public static string GetMediaUrl { get; private set; } = "{{Media-ID}}";

/// <summary>
/// To retrieve media URL with phone number id ownership
/// </summary>
public static string GetMediaUrlOwnership { get; private set; } = "{{Media-ID}}?phone_number_id={{PHONE_NUMBER_ID}}";

/// <summary>
/// Delete media
/// </summary>
public static string DeleteMedia { get; private set; } = "{{Media-ID}}";

/// <summary>
/// Delete Media with phone number id ownership
/// </summary>
public static string DeleteMediaOwnership { get; private set; } = "{{Media-ID}}/?phone_number_id={{PHONE_NUMBER_ID}}";

/// <summary>
/// Endpoint to send WhatsApp Messages
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<RepositoryUrl>https://github.com/gabrieldwight/Whatsapp-Business-Cloud-Api-Net</RepositoryUrl>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>1.0.11</Version>
<Version>1.0.12</Version>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

</Project>

0 comments on commit e894adc

Please sign in to comment.