From d5bc0acda393f7546b382c0a48ee4c83d2204478 Mon Sep 17 00:00:00 2001 From: Gabriel Odero Date: Tue, 23 Jan 2024 00:32:20 +0300 Subject: [PATCH] Location Request Message Added --- .../Controllers/HomeController.cs | 15 +++++- .../Interfaces/IWhatsAppBusinessClient.cs | 18 +++++++ .../InteractiveLocationMessageRequest.cs | 46 ++++++++++++++++ .../WhatsAppBusinessClient.cs | 52 ++++++++++++++++--- 4 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 WhatsappBusiness.CloudApi/Messages/Requests/InteractiveLocationMessageRequest.cs diff --git a/Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs b/Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs index f687909..b910d52 100644 --- a/Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs +++ b/Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs @@ -258,7 +258,8 @@ public IActionResult SendWhatsAppInteractiveMessage() sendInteractiveMessageViewModel.InteractiveType = new List() { new SelectListItem(){ Text = "List Message", Value = "List Message" }, - new SelectListItem(){ Text = "Reply Button", Value = "Reply Button" } + new SelectListItem(){ Text = "Reply Button", Value = "Reply Button" }, + new SelectListItem(){ Text = "Location Request Message", Value = "Location Request Message" } }; return View(sendInteractiveMessageViewModel); } @@ -377,6 +378,18 @@ public async Task SendWhatsAppInteractiveMessage(SendInteractiveM results = await _whatsAppBusinessClient.SendInteractiveReplyButtonMessageAsync(interactiveReplyButtonMessage); } + if (sendInteractiveMessageViewModel.SelectedInteractiveType.Equals("Location Request Message")) + { + InteractiveLocationMessageRequest interactiveLocationMessageRequest = new InteractiveLocationMessageRequest(); + interactiveLocationMessageRequest.To = sendInteractiveMessageViewModel.RecipientPhoneNumber; + interactiveLocationMessageRequest.Interactive = new InteractiveLocationRequestMessage(); + interactiveLocationMessageRequest.Interactive.Body = new InteractiveLocationBody(); + interactiveLocationMessageRequest.Interactive.Body.Text = (!string.IsNullOrWhiteSpace(sendInteractiveMessageViewModel.Message)) ? sendInteractiveMessageViewModel.Message : "Let us start with your pickup. You can either manually *enter an address* or *share your current location*."; + interactiveLocationMessageRequest.Interactive.Action = new InteractiveLocationAction(); + + results = await _whatsAppBusinessClient.SendLocationRequestMessageAsync(interactiveLocationMessageRequest); + } + if (results != null) { return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent interactive message"); diff --git a/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs index e5c403b..c3cff8b 100644 --- a/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs @@ -945,6 +945,24 @@ public interface IWhatsAppBusinessClient /// Cancellation token /// WhatsAppResponse WhatsAppResponse SendGenericMessage(object whatsAppMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default); + + /// + /// Location request messages are free-form messages displaying body text and a send location button. When a WhatsApp user taps the button, a location sharing screen appears which the user can then use to share their location. + /// + /// interactiveLocationMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + Task SendLocationRequestMessageAsync(InteractiveLocationMessageRequest interactiveLocationMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default); + + /// + /// Location request messages are free-form messages displaying body text and a send location button. When a WhatsApp user taps the button, a location sharing screen appears which the user can then use to share their location. + /// + /// interactiveLocationMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + WhatsAppResponse SendLocationRequestMessage(InteractiveLocationMessageRequest interactiveLocationMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default); #endregion #region Two step verification code function diff --git a/WhatsappBusiness.CloudApi/Messages/Requests/InteractiveLocationMessageRequest.cs b/WhatsappBusiness.CloudApi/Messages/Requests/InteractiveLocationMessageRequest.cs new file mode 100644 index 0000000..bf972b0 --- /dev/null +++ b/WhatsappBusiness.CloudApi/Messages/Requests/InteractiveLocationMessageRequest.cs @@ -0,0 +1,46 @@ +using Newtonsoft.Json; + +namespace WhatsappBusiness.CloudApi.Messages.Requests +{ + public class InteractiveLocationMessageRequest + { + [JsonProperty("messaging_product")] + public string MessagingProduct { get; private set; } = "whatsapp"; + + [JsonProperty("recipient_type")] + public string RecipientType { get; private set; } = "individual"; + + [JsonProperty("type")] + public string Type { get; private set; } = "interactive"; + + [JsonProperty("to")] + public string To { get; set; } + + [JsonProperty("interactive")] + public InteractiveLocationRequestMessage Interactive { get; set; } + } + + public class InteractiveLocationRequestMessage + { + [JsonProperty("type")] + public string Type { get; private set; } = "location_request_message"; + + [JsonProperty("body")] + public InteractiveLocationBody Body { get; set; } + + [JsonProperty("action")] + public InteractiveLocationAction Action { get; set; } + } + + public class InteractiveLocationAction + { + [JsonProperty("name")] + public string Name { get; private set; } = "send_location"; + } + + public class InteractiveLocationBody + { + [JsonProperty("text")] + public string Text { get; set; } + } +} diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs index 8f0d7f7..1ed2354 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs @@ -2194,14 +2194,50 @@ public WhatsAppResponse SendGenericMessage(object whatsAppMessageRequest, WhatsA return WhatsAppBusinessPostAsync(whatsAppMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult(); } - /// - /// You can use this endpoint to change two-step verification code associated with your account. After you change the verification code, future requests like changing the name, must use the new code. - /// You set up two-factor verification and register a phone number in the same API call. - /// - /// TwoStepVerificationRequest object - /// Cancellation token - /// BaseSuccessResponse - public BaseSuccessResponse SetTwoStepVerificatiion(TwoStepVerificationRequest twoStepVerificationRequest, CancellationToken cancellationToken = default) + /// + /// Location request messages are free-form messages displaying body text and a send location button. When a WhatsApp user taps the button, a location sharing screen appears which the user can then use to share their location. + /// + /// interactiveLocationMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + public async Task SendLocationRequestMessageAsync(InteractiveLocationMessageRequest interactiveLocationMessageRequest, 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(interactiveLocationMessageRequest, formattedWhatsAppEndpoint, cancellationToken); + } + + /// + /// Location request messages are free-form messages displaying body text and a send location button. When a WhatsApp user taps the button, a location sharing screen appears which the user can then use to share their location. + /// + /// interactiveLocationMessageRequest object + /// Custom WhatsAppBusinessCloudApiConfig + /// Cancellation token + /// WhatsAppResponse + public WhatsAppResponse SendLocationRequestMessage(InteractiveLocationMessageRequest interactiveLocationMessageRequest, 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(interactiveLocationMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult(); + } + + /// + /// You can use this endpoint to change two-step verification code associated with your account. After you change the verification code, future requests like changing the name, must use the new code. + /// You set up two-factor verification and register a phone number in the same API call. + /// + /// TwoStepVerificationRequest object + /// Cancellation token + /// BaseSuccessResponse + public BaseSuccessResponse SetTwoStepVerificatiion(TwoStepVerificationRequest twoStepVerificationRequest, CancellationToken cancellationToken = default) { var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.SetTwoFactor.Replace("{{Phone-Number-ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId); return WhatsAppBusinessPostAsync(twoStepVerificationRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();