Skip to content

Commit

Permalink
Location Request Message Added
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed Jan 22, 2024
1 parent 05b52a2 commit d5bc0ac
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ public IActionResult SendWhatsAppInteractiveMessage()
sendInteractiveMessageViewModel.InteractiveType = new List<SelectListItem>()
{
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);
}
Expand Down Expand Up @@ -377,6 +378,18 @@ public async Task<IActionResult> 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");
Expand Down
18 changes: 18 additions & 0 deletions WhatsappBusiness.CloudApi/Interfaces/IWhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,24 @@ public interface IWhatsAppBusinessClient
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
WhatsAppResponse SendGenericMessage(object whatsAppMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default);

/// <summary>
/// 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.
/// </summary>
/// <param name="interactiveLocationMessageRequest">interactiveLocationMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
Task<WhatsAppResponse> SendLocationRequestMessageAsync(InteractiveLocationMessageRequest interactiveLocationMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default);

/// <summary>
/// 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.
/// </summary>
/// <param name="interactiveLocationMessageRequest">interactiveLocationMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
WhatsAppResponse SendLocationRequestMessage(InteractiveLocationMessageRequest interactiveLocationMessageRequest, WhatsAppBusinessCloudApiConfig? cloudApiConfig = null, CancellationToken cancellationToken = default);
#endregion

#region Two step verification code function
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
52 changes: 44 additions & 8 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,14 +2194,50 @@ public WhatsAppResponse SendGenericMessage(object whatsAppMessageRequest, WhatsA
return WhatsAppBusinessPostAsync<WhatsAppResponse>(whatsAppMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
/// 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.
/// </summary>
/// <param name="twoStepVerificationRequest">TwoStepVerificationRequest object</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>BaseSuccessResponse</returns>
public BaseSuccessResponse SetTwoStepVerificatiion(TwoStepVerificationRequest twoStepVerificationRequest, CancellationToken cancellationToken = default)
/// <summary>
/// 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.
/// </summary>
/// <param name="interactiveLocationMessageRequest">interactiveLocationMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
public async Task<WhatsAppResponse> 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<WhatsAppResponse>(interactiveLocationMessageRequest, formattedWhatsAppEndpoint, cancellationToken);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="interactiveLocationMessageRequest">interactiveLocationMessageRequest object</param>
/// <param name="cloudApiConfig">Custom WhatsAppBusinessCloudApiConfig</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
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<WhatsAppResponse>(interactiveLocationMessageRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
/// 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.
/// </summary>
/// <param name="twoStepVerificationRequest">TwoStepVerificationRequest object</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>BaseSuccessResponse</returns>
public BaseSuccessResponse SetTwoStepVerificatiion(TwoStepVerificationRequest twoStepVerificationRequest, CancellationToken cancellationToken = default)
{
var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.SetTwoFactor.Replace("{{Phone-Number-ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId);
return WhatsAppBusinessPostAsync<BaseSuccessResponse>(twoStepVerificationRequest, formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
Expand Down

0 comments on commit d5bc0ac

Please sign in to comment.