Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ness-Cloud-Api-Net

# Conflicts:
#	Samples/WhatsAppBusinessCloudAPI.Web/Views/Home/SendWhatsAppTemplateMessage.cshtml
  • Loading branch information
Claudinei Nascimento committed Nov 3, 2022
2 parents 8434921 + 8a4d2e3 commit 90b6122
Show file tree
Hide file tree
Showing 55 changed files with 1,289 additions and 228 deletions.
45 changes: 23 additions & 22 deletions Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using WhatsappBusiness.CloudApi.Media.Requests;
using WhatsappBusiness.CloudApi.Messages.Requests;
using WhatsappBusiness.CloudApi.Response;
using WhatsAppBusinessCloudAPI.Web.Extensions.Alerts;
using WhatsAppBusinessCloudAPI.Web.Models;
using WhatsAppBusinessCloudAPI.Web.ViewModel;

Expand Down Expand Up @@ -54,12 +55,12 @@ public async Task<IActionResult> SendWhatsAppTextMessage(SendTextMessageViewMode

var results = await _whatsAppBusinessClient.SendTextMessageAsync(textMessageRequest);

return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent text message");
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return View();
return View().WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -206,7 +207,7 @@ public async Task<IActionResult> SendWhatsAppMediaMessage(SendMediaMessageViewMo

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent media message");
}
else
{
Expand All @@ -216,7 +217,7 @@ public async Task<IActionResult> SendWhatsAppMediaMessage(SendMediaMessageViewMo
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppMediaMessage));
return RedirectToAction(nameof(SendWhatsAppMediaMessage)).WithDanger("Error", ex.Message);
}
}

Expand All @@ -241,12 +242,12 @@ public async Task<IActionResult> SendWhatsAppLocationMessage(SendLocationMessage

var results = await _whatsAppBusinessClient.SendLocationMessageAsync(locationMessageRequest);

return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent location message");
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return View();
return View().WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -370,7 +371,7 @@ public async Task<IActionResult> SendWhatsAppInteractiveMessage(SendInteractiveM

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent interactive message");
}
else
{
Expand All @@ -380,7 +381,7 @@ public async Task<IActionResult> SendWhatsAppInteractiveMessage(SendInteractiveM
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppInteractiveMessage));
return RedirectToAction(nameof(SendWhatsAppInteractiveMessage)).WithDanger("Error", ex.Message);
}
}

Expand All @@ -406,7 +407,7 @@ public async Task<IActionResult> SendWhatsAppTemplateMessage(SendTemplateMessage

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent template text message");
}
else
{
Expand All @@ -416,7 +417,7 @@ public async Task<IActionResult> SendWhatsAppTemplateMessage(SendTemplateMessage
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
return RedirectToAction(nameof(SendWhatsAppTemplateMessage)).WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -456,7 +457,7 @@ public async Task<IActionResult> SendWhatsAppTextTemplateMessageWithParameters(S

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent template text message");
}
else
{
Expand All @@ -466,7 +467,7 @@ public async Task<IActionResult> SendWhatsAppTextTemplateMessageWithParameters(S
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
return RedirectToAction(nameof(SendWhatsAppTemplateMessage)).WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -501,7 +502,7 @@ public async Task<IActionResult> SendWhatsAppInteractiveTemplateMessageWithParam

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent interactive template message");
}
else
{
Expand All @@ -511,7 +512,7 @@ public async Task<IActionResult> SendWhatsAppInteractiveTemplateMessageWithParam
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
return RedirectToAction(nameof(SendWhatsAppTemplateMessage)).WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -592,7 +593,7 @@ public async Task<IActionResult> SendWhatsAppMediaTemplateMessageWithParameters(

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent image template message");
}
else
{
Expand All @@ -602,7 +603,7 @@ public async Task<IActionResult> SendWhatsAppMediaTemplateMessageWithParameters(
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
return RedirectToAction(nameof(SendWhatsAppTemplateMessage)).WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -654,7 +655,7 @@ public async Task<IActionResult> SendWhatsAppDocumentTemplateMessageWithParamete

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent document template message");
}
else
{
Expand All @@ -664,7 +665,7 @@ public async Task<IActionResult> SendWhatsAppDocumentTemplateMessageWithParamete
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
return RedirectToAction(nameof(SendWhatsAppTemplateMessage)).WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -773,7 +774,7 @@ public async Task<IActionResult> SendWhatsAppContactMessage(SendContactMessageVi

if (results != null)
{
return RedirectToAction(nameof(Index));
return RedirectToAction(nameof(Index)).WithSuccess("Success", "Successfully sent contact message");
}
else
{
Expand All @@ -783,7 +784,7 @@ public async Task<IActionResult> SendWhatsAppContactMessage(SendContactMessageVi
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return View();
return View().WithDanger("Error", ex.Message);
}
}

Expand Down Expand Up @@ -822,12 +823,12 @@ public async Task<IActionResult> UploadMedia(IFormFile mediaFile)

var uploadMediaResult = await _whatsAppBusinessClient.UploadMediaAsync(uploadMediaRequest);
ViewBag.MediaId = uploadMediaResult.MediaId;
return View();
return View().WithSuccess("Success", "Successfully upload media.");
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return View();
return View().WithDanger("Error", ex.Message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task<IActionResult> ReceiveWhatsAppTextMessage([FromBody] dynamic m
await _whatsAppBusinessClient.MarkMessageAsReadAsync(markMessageRequest);

TextMessageReplyRequest textMessageReplyRequest = new TextMessageReplyRequest();
textMessageReplyRequest.Context = new TextMessageContext();
textMessageReplyRequest.Context = new WhatsappBusiness.CloudApi.Messages.ReplyRequests.TextMessageContext();
textMessageReplyRequest.Context.MessageId = textMessage.SingleOrDefault().Id;
textMessageReplyRequest.To = textMessage.SingleOrDefault().From;
textMessageReplyRequest.Text = new WhatsAppText();
Expand Down Expand Up @@ -207,7 +207,7 @@ public async Task<IActionResult> ReceiveWhatsAppTextMessage([FromBody] dynamic m
await _whatsAppBusinessClient.MarkMessageAsReadAsync(markMessageRequest);

LocationMessageReplyRequest locationMessageReplyRequest = new LocationMessageReplyRequest();
locationMessageReplyRequest.Context = new LocationMessageContext();
locationMessageReplyRequest.Context = new WhatsappBusiness.CloudApi.Messages.ReplyRequests.LocationMessageContext();
locationMessageReplyRequest.Context.MessageId = locationMessage.SingleOrDefault().Id;
locationMessageReplyRequest.To = locationMessage.SingleOrDefault().From;
locationMessageReplyRequest.Location = new WhatsappBusiness.CloudApi.Messages.Requests.Location();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Mvc.Rendering;

namespace WhatsAppBusinessCloudAPI.Web.Extensions
{
public static class ActiveRouteTagHelper
{
public static string IsActive(this IHtmlHelper html, string controller, string action, string cssClass = "active")
{
var routeData = html.ViewContext.RouteData;

var currentRouteAction = routeData.Values["action"] as string;
var currentRouteController = routeData.Values["controller"] as string;

IEnumerable<string> acceptedActions = (action ?? currentRouteAction).Split(',');
IEnumerable<string> acceptedControllers = (controller ?? currentRouteController).Split(',');

return acceptedActions.Contains(currentRouteAction) && acceptedControllers.Contains(currentRouteController) ?
cssClass : string.Empty;
}

public static string IsActive(this IHtmlHelper html, string controller)
{
string cssClass = "active";
var routeData = html.ViewContext.RouteData;

var currentRouteController = routeData.Values["controller"] as string;

IEnumerable<string> acceptedControllers = (controller ?? currentRouteController).Split(',');

return acceptedControllers.Contains(currentRouteController) ?
cssClass : string.Empty;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace WhatsAppBusinessCloudAPI.Web.Extensions.Alerts
{
public class AlertDecoratorResult : IActionResult
{
public IActionResult Result { get; }
public string Type { get; }
public string Title { get; }
public string Body { get; }

public AlertDecoratorResult(IActionResult result, string type, string title, string body)
{
Result = result;
Type = type;
Title = title;
Body = body;
}


public async Task ExecuteResultAsync(ActionContext context)
{
if (Result is StatusCodeResult || Result is OkObjectResult)
{
AddAlertMessageToApiResult(context);
}
else
{
AddAlertMessageToMvcResult(context);
}

await Result.ExecuteResultAsync(context);
}

private void AddAlertMessageToApiResult(ActionContext context)
{
context.HttpContext.Response.Headers.Add("x-alert-type", Type);
context.HttpContext.Response.Headers.Add("x-alert-title", Title);
context.HttpContext.Response.Headers.Add("x-alert-body", Body);
}

private void AddAlertMessageToMvcResult(ActionContext context)
{
var factory = context.HttpContext.RequestServices.GetService<ITempDataDictionaryFactory>();

var tempData = factory.GetTempData(context.HttpContext);
tempData["_alert.type"] = Type;
tempData["_alert.title"] = Title;
tempData["_alert.body"] = Body;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Mvc;

namespace WhatsAppBusinessCloudAPI.Web.Extensions.Alerts
{
public static class AlertExtension
{
public static IActionResult WithSuccess(this IActionResult result, string title, string body)
{
return Alert(result, "success", title, body);
}

public static IActionResult WithInfo(this IActionResult result, string title, string body)
{
return Alert(result, "info", title, body);
}

public static IActionResult WithWarning(this IActionResult result, string title, string body)
{
return Alert(result, "warning", title, body);
}

public static IActionResult WithDanger(this IActionResult result, string title, string body)
{
return Alert(result, "danger", title, body);
}

private static IActionResult Alert(IActionResult result, string type, string title, string body)
{
return new AlertDecoratorResult(result, type, title, body);
}
}
}
4 changes: 4 additions & 0 deletions Samples/WhatsAppBusinessCloudAPI.Web/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@{
ViewData["Title"] = "Home Page";
ViewData["CurrentPage"] = "Home Page";
Layout = "~/Views/Shared/AdminLTE/_AdminLayout.cshtml";
ViewData["ControllerName"] = nameof(HomeController).Replace("Controller", "");
ViewData["ActionName"] = nameof(HomeController.Index);
}

<div class="text-center">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@{
ViewData["Title"] = "Privacy Policy";
ViewData["CurrentPage"] = "Privacy Policy";
Layout = "~/Views/Shared/AdminLTE/_AdminLayout.cshtml";
ViewData["ControllerName"] = nameof(HomeController).Replace("Controller", "");
ViewData["ActionName"] = nameof(HomeController.Privacy);
}
<h1>@ViewData["Title"]</h1>

Expand Down
Loading

0 comments on commit 90b6122

Please sign in to comment.