Skip to content

Commit

Permalink
QR Code Message and account metrics implementation added
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed Dec 31, 2022
1 parent b7b0986 commit 82812a7
Show file tree
Hide file tree
Showing 14 changed files with 782 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace WhatsappBusiness.CloudApi.AccountMetrics
{
public static class ConversationDirection
{
public const string BusinessInitiated = "business_initiated";
public const string UserInitiated = "user_initiated";
}
}
9 changes: 9 additions & 0 deletions WhatsappBusiness.CloudApi/AccountMetrics/ConversationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WhatsappBusiness.CloudApi.AccountMetrics
{
public static class ConversationType
{
public const string FREE_ENTRY = "FREE_ENTRY";
public const string FREE_TIER = "FREE_TIER";
public const string REGULAR = "REGULAR";
}
}
10 changes: 10 additions & 0 deletions WhatsappBusiness.CloudApi/AccountMetrics/Dimension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace WhatsappBusiness.CloudApi.AccountMetrics
{
public static class Dimension
{
public const string Phone = "phone";
public const string Country = "country";
public const string ConversationType = "conversation_type";
public const string COnversationDirection = "conversation_direction";
}
}
19 changes: 19 additions & 0 deletions WhatsappBusiness.CloudApi/AccountMetrics/Granularity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace WhatsappBusiness.CloudApi.AccountMetrics
{
public static class Granularity
{
public static class AnalyticsGranularity
{
public const string HALF_HOUR = "HALF_HOUR";
public const string DAY = "DAY";
public const string MONTH = "MONTH";
}

public static class ConversationGranularity
{
public const string HALF_HOUR = "HALF_HOUR";
public const string DAILY = "DAILY";
public const string MONTHLY = "MONTHLY";
}
}
}
8 changes: 8 additions & 0 deletions WhatsappBusiness.CloudApi/AccountMetrics/MetricType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace WhatsappBusiness.CloudApi.AccountMetrics
{
public static class MetricType
{
public const string COST = "COST";
public const string CONVERSATION = "CONVERSATION";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection se

services.AddHttpClient<IWhatsAppBusinessClient, WhatsAppBusinessClient>(options =>
{
options.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress;
options.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;
options.Timeout = TimeSpan.FromMinutes(10);
}).ConfigurePrimaryHttpMessageHandler(messageHandler =>
{
Expand Down Expand Up @@ -76,7 +76,7 @@ public static void AddWhatsAppBusinessCloudApiService<THandler>(this IServiceCol

services.AddHttpClient<IWhatsAppBusinessClient, WhatsAppBusinessClient>(options =>
{
options.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress;
options.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;
options.Timeout = TimeSpan.FromMinutes(10);
}).SetHandlerLifetime(Timeout.InfiniteTimeSpan)
.ConfigurePrimaryHttpMessageHandler<THandler>()
Expand Down
150 changes: 149 additions & 1 deletion WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using WhatsappBusiness.CloudApi.AccountMigration.Requests;
using WhatsappBusiness.CloudApi.BusinessProfile.Requests;
Expand Down Expand Up @@ -783,5 +785,151 @@ public interface IWhatsAppBusinessClient
/// <returns>BaseSuccessResponse</returns>
BaseSuccessResponse DeleteWABASubscription(string whatsAppBusinessAccountId, CancellationToken cancellationToken = default);
#endregion

#region Account Metrics
/// <summary>
/// The analytics field provides the number and type of messages sent and delivered by the phone numbers associated with a specific WABA
/// </summary>
/// <param name="whatsAppBusinessAccountId">Your WhatsApp Business Account (WABA) ID.</param>
/// <param name="startDate">The start date for the date range you are retrieving analytics for</param>
/// <param name="endDate">The end date for the date range you are retrieving analytics for</param>
/// <param name="granularity">The granularity by which you would like to retrieve the analytics</param>
/// <param name="phoneNumbers">An array of phone numbers for which you would like to retrieve analytics. If not provided, all phone numbers added to your WABA are included.</param>
/// <param name="productTypes">The types of messages (notification messages and/or customer support messages) for which you want to retrieve notifications. Provide an array and include 0 for notification messages, and 2 for customer support messages. If not provided, analytics will be returned for all messages together</param>
/// <param name="countryCodes">The countries for which you would like to retrieve analytics. Provide an array with 2 letter country codes for the countries you would like to include. If not provided, analytics will be returned for all countries you have communicated with</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>AnalyticsResponse</returns>
AnalyticsResponse GetAnalyticMetrics(string whatsAppBusinessAccountId, DateTime startDate, DateTime endDate, string granularity, List<string>? phoneNumbers = null, List<string>? productTypes = null, List<string>? countryCodes = null, CancellationToken cancellationToken = default);

/// <summary>
/// The analytics field provides the number and type of messages sent and delivered by the phone numbers associated with a specific WABA
/// </summary>
/// <param name="whatsAppBusinessAccountId">Your WhatsApp Business Account (WABA) ID.</param>
/// <param name="startDate">The start date for the date range you are retrieving analytics for</param>
/// <param name="endDate">The end date for the date range you are retrieving analytics for</param>
/// <param name="granularity">The granularity by which you would like to retrieve the analytics</param>
/// <param name="phoneNumbers">An array of phone numbers for which you would like to retrieve analytics. If not provided, all phone numbers added to your WABA are included.</param>
/// <param name="productTypes">The types of messages (notification messages and/or customer support messages) for which you want to retrieve notifications. Provide an array and include 0 for notification messages, and 2 for customer support messages. If not provided, analytics will be returned for all messages together</param>
/// <param name="countryCodes">The countries for which you would like to retrieve analytics. Provide an array with 2 letter country codes for the countries you would like to include. If not provided, analytics will be returned for all countries you have communicated with</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>AnalyticsResponse</returns>
Task<AnalyticsResponse> GetAnalyticMetricsAsync(string whatsAppBusinessAccountId, DateTime startDate, DateTime endDate, string granularity, List<string>? phoneNumbers = null, List<string>? productTypes = null, List<string>? countryCodes = null, CancellationToken cancellationToken = default);

/// <summary>
/// The conversation_analytics field provides cost and conversation information for a specific WABA.
/// </summary>
/// <param name="whatsAppBusinessAccountId">Your WhatsApp Business Account (WABA) ID.</param>
/// <param name="startDate">The start date for the date range you are retrieving analytics for</param>
/// <param name="endDate">The end date for the date range you are retrieving analytics for</param>
/// <param name="granularity">The granularity by which you would like to retrieve the analytics</param>
/// <param name="phoneNumbers">An array of phone numbers for which you would like to retrieve analytics. If not provided, all phone numbers added to your WABA are included.</param>
/// <param name="metricTypes">List of metrics you would like to receive. If you send an empty list, we return results for all metric types.</param>
/// <param name="conversationTypes">List of conversation types. If you send an empty list, we return results for all conversation types.</param>
/// <param name="conversationDirections">List of conversation directions. If you send an empty list, we return results for all conversation directions</param>
/// <param name="dimensions">List of breakdowns you would like to apply to your metrics. If you send an empty list, we return results without any breakdowns.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>ConversationAnalyticsResponse</returns>
ConversationAnalyticsResponse GetConversationAnalyticMetrics(string whatsAppBusinessAccountId, DateTime startDate, DateTime endDate, string granularity, List<string>? phoneNumbers = null, List<string>? metricTypes = null, List<string>? conversationTypes = null, List<string>? conversationDirections = null, List<string>? dimensions = null, CancellationToken cancellationToken = default);

/// <summary>
/// The conversation_analytics field provides cost and conversation information for a specific WABA.
/// </summary>
/// <param name="whatsAppBusinessAccountId">Your WhatsApp Business Account (WABA) ID.</param>
/// <param name="startDate">The start date for the date range you are retrieving analytics for</param>
/// <param name="endDate">The end date for the date range you are retrieving analytics for</param>
/// <param name="granularity">The granularity by which you would like to retrieve the analytics</param>
/// <param name="phoneNumbers">An array of phone numbers for which you would like to retrieve analytics. If not provided, all phone numbers added to your WABA are included.</param>
/// <param name="metricTypes">List of metrics you would like to receive. If you send an empty list, we return results for all metric types.</param>
/// <param name="conversationTypes">List of conversation types. If you send an empty list, we return results for all conversation types.</param>
/// <param name="conversationDirections">List of conversation directions. If you send an empty list, we return results for all conversation directions</param>
/// <param name="dimensions">List of breakdowns you would like to apply to your metrics. If you send an empty list, we return results without any breakdowns.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>ConversationAnalyticsResponse</returns>
Task<ConversationAnalyticsResponse> GetConversationAnalyticMetricsAsync(string whatsAppBusinessAccountId, DateTime startDate, DateTime endDate, string granularity, List<string>? phoneNumbers = null, List<string>? metricTypes = null, List<string>? conversationTypes = null, List<string>? conversationDirections = null, List<string>? dimensions = null, CancellationToken cancellationToken = default);
#endregion

#region QR Code Message
/// <summary>
/// To create a QR code for a business, send a POST request to the /{phone-number-ID}/message_qrdls endpoint with the prefilled_message parameter set to your message text and generate_qr_image parameter set to your preferred image format, either SVG or PNG.
/// </summary>
/// <param name="messageText"></param>
/// <param name="qrImageFormat"></param>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageResponse</returns>
QRCodeMessageResponse CreateQRCodeMessage(string messageText, string qrImageFormat, CancellationToken cancellationToken = default);

/// <summary>
/// To create a QR code for a business, send a POST request to the /{phone-number-ID}/message_qrdls endpoint with the prefilled_message parameter set to your message text and generate_qr_image parameter set to your preferred image format, either SVG or PNG.
/// </summary>
/// <param name="messageText"></param>
/// <param name="qrImageFormat"></param>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageResponse</returns>
Task<QRCodeMessageResponse> CreateQRCodeMessageAsync(string messageText, string qrImageFormat, CancellationToken cancellationToken = default);

/// <summary>
/// To get a list of all the QR codes messages for a business
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageFilterResponse</returns>
QRCodeMessageFilterResponse GetQRCodeMessageList(CancellationToken cancellationToken = default);

/// <summary>
/// To get a list of all the QR codes messages for a business
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageFilterResponse</returns>
Task<QRCodeMessageFilterResponse> GetQRCodeMessageListAsync(CancellationToken cancellationToken = default);

/// <summary>
/// To get information about a specific QR code message
/// </summary>
/// <param name="qrCodeId"></param>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageFilterResponse</returns>
QRCodeMessageFilterResponse GetQRCodeMessageById(string qrCodeId, CancellationToken cancellationToken = default);

/// <summary>
/// To get information about a specific QR code message
/// </summary>
/// <param name="qrCodeId"></param>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageFilterResponse</returns>
Task<QRCodeMessageFilterResponse> GetQRCodeMessageByIdAsync(string qrCodeId, CancellationToken cancellationToken = default);

/// <summary>
/// To update a QR code for a business, send a POST request to the /{phone-number-ID}/message_qrdls/{qr-code-id} endpoint and include the parameter you wish to update.
/// </summary>
/// <param name="qrCodeId"></param>
/// <param name="messageText"></param>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageResponse</returns>
QRCodeMessageResponse UpdateQRCodeMessage(string qrCodeId, string messageText, CancellationToken cancellationToken = default);

/// <summary>
/// To update a QR code for a business, send a POST request to the /{phone-number-ID}/message_qrdls/{qr-code-id} endpoint and include the parameter you wish to update.
/// </summary>
/// <param name="qrCodeId"></param>
/// <param name="messageText"></param>
/// <param name="cancellationToken"></param>
/// <returns>QRCodeMessageResponse</returns>
Task<QRCodeMessageResponse> UpdateQRCodeMessageAsync(string qrCodeId, string messageText, CancellationToken cancellationToken = default);

/// <summary>
/// QR codes do not expire. You must delete a QR code in order to retire it.
/// </summary>
/// <param name="qrCodeId"></param>
/// <param name="cancellationToken"></param>
/// <returns>BaseSuccessResponse</returns>
BaseSuccessResponse DeleteQRCodeMessage(string qrCodeId, CancellationToken cancellationToken = default);

/// <summary>
/// QR codes do not expire. You must delete a QR code in order to retire it.
/// </summary>
/// <param name="qrCodeId"></param>
/// <param name="cancellationToken"></param>
/// <returns>BaseSuccessResponse</returns>
Task<BaseSuccessResponse> DeleteQRCodeMessageAsync(string qrCodeId, CancellationToken cancellationToken = default);
#endregion
}
}
44 changes: 44 additions & 0 deletions WhatsappBusiness.CloudApi/Response/AnalyticsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace WhatsappBusiness.CloudApi.Response
{
public class AnalyticsResponse
{
[JsonProperty("analytics")]
public Analytics Analytics { get; set; }

[JsonProperty("id")]
public string Id { get; set; }
}

public class Analytics
{
[JsonProperty("phone_numbers")]
public List<string> PhoneNumbers { get; set; }

[JsonProperty("country_codes")]
public List<string> CountryCodes { get; set; }

[JsonProperty("granularity")]
public string Granularity { get; set; }

[JsonProperty("data_points")]
public List<AnalyticsDataPoint> DataPoints { get; set; }
}

public class AnalyticsDataPoint
{
[JsonProperty("start")]
public long Start { get; set; }

[JsonProperty("end")]
public long End { get; set; }

[JsonProperty("sent")]
public long Sent { get; set; }

[JsonProperty("delivered")]
public long Delivered { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace WhatsappBusiness.CloudApi.Response
{
public class ConversationAnalyticsResponse
{
[JsonProperty("conversation_analytics")]
public ConversationAnalytics ConversationAnalytics { get; set; }
}

public class ConversationAnalytics
{
[JsonProperty("data")]
public List<ConversationAnalyticsData> Data { get; set; }
}

public class ConversationAnalyticsData
{
[JsonProperty("data_points")]
public List<ConversationAnalyticsDataPoint> DataPoints { get; set; }
}

public class ConversationAnalyticsDataPoint
{
[JsonProperty("start")]
public long Start { get; set; }

[JsonProperty("end")]
public long End { get; set; }

[JsonProperty("conversation")]
public long Conversation { get; set; }

[JsonProperty("phone_number")]
public string PhoneNumber { get; set; }

[JsonProperty("conversation_type")]
public string ConversationType { get; set; }

[JsonProperty("conversation_direction")]
public string ConversationDirection { get; set; }

[JsonProperty("cost")]
public double Cost { get; set; }
}
}
11 changes: 11 additions & 0 deletions WhatsappBusiness.CloudApi/Response/QRCodeMessageFilterResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace WhatsappBusiness.CloudApi.Response
{
public class QRCodeMessageFilterResponse
{
[JsonProperty("data")]
public List<QRCodeMessageResponse> Data { get; set; }
}
}
19 changes: 19 additions & 0 deletions WhatsappBusiness.CloudApi/Response/QRCodeMessageResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace WhatsappBusiness.CloudApi.Response
{
public class QRCodeMessageResponse
{
[JsonProperty("code")]
public string Code { get; set; }

[JsonProperty("prefilled_message")]
public string PrefilledMessage { get; set; }

[JsonProperty("deep_link_url")]
public string DeepLinkUrl { get; set; }

[JsonProperty("qr_image_url")]
public string QrImageUrl { get; set; }
}
}
Loading

0 comments on commit 82812a7

Please sign in to comment.