Skip to content

Commit

Permalink
Merge pull request #21 from claudineibr/master
Browse files Browse the repository at this point in the history
Implement send video template message
  • Loading branch information
gabrieldwight authored Nov 3, 2022
2 parents ef49c21 + 90b6122 commit f7d40eb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
62 changes: 62 additions & 0 deletions Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,68 @@ public async Task<IActionResult> SendWhatsAppDocumentTemplateMessageWithParamete
}
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SendWhatsAppVideoTemplateMessageWithParameters(SendTemplateMessageViewModel sendTemplateMessageViewModel)
{
try
{
VideoTemplateMessageRequest videoTemplateMessage = new();
videoTemplateMessage.To = sendTemplateMessageViewModel.RecipientPhoneNumber;
videoTemplateMessage.Template = new();
videoTemplateMessage.Template.Name = sendTemplateMessageViewModel.TemplateName;
videoTemplateMessage.Template.Language = new();
videoTemplateMessage.Template.Language.Code = LanguageCode.English_US;
videoTemplateMessage.Template.Components = new List<VideoMessageComponent>()
{
new VideoMessageComponent()
{
Type = "header",
Parameters = new List<VideoMessageParameter>()
{
new VideoMessageParameter()
{
Type = "video",
Video = new Video()
{
//Id = sendTemplateMessageViewModel.MediaId,
Link = sendTemplateMessageViewModel.LinkUrl // Link point where your document can be downloaded or retrieved by WhatsApp
}
}
},
},
new VideoMessageComponent()
{
Type = "body",
Parameters = new List<VideoMessageParameter>()
{
new VideoMessageParameter()
{
Type = "text",
Text = "Video Information"
},
}
}
};

var results = await _whatsAppBusinessClient.SendVideoAttachmentTemplateMessageAsync(videoTemplateMessage);

if (results != null)
{
return RedirectToAction(nameof(Index));
}
else
{
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
}
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(SendWhatsAppTemplateMessage));
}
}

public IActionResult SendWhatsAppContactMessage()
{
return View();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class SendTemplateMessageViewModel
public string RecipientPhoneNumber { get; set; }
public string TemplateName { get; set; }
public string MediaId { get; set; }
public string LinkUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<input asp-for="MediaId" class="form-control" />
<span asp-validation-for="MediaId" class="form-control" />
</div>
<div class="form-group">
<label class="control-label">Link Url</label>
<input asp-for="MediaId" class="form-control" />
<span asp-validation-for="LinkUrl" class="form-control" />
</div>
</div> <!--/. card-body -->
<div class="card-footer">
<div class="form-group">
Expand All @@ -41,6 +46,7 @@
<button asp-action="SendWhatsAppInteractiveTemplateMessageWithParameters" asp-controller="Home" class="btn btn-primary">Send Interactive Template Message With Parameters</button>
<button asp-action="SendWhatsAppMediaTemplateMessageWithParameters" asp-controller="Home" class="btn btn-primary">Send Media Template Message With Parameters</button>
<button asp-action="SendWhatsAppDocumentTemplateMessageWithParameters" asp-controller="Home" class="btn btn-primary">Send Document Template Message With Parameters</button>
<button asp-action="SendWhatsAppVideoTemplateMessageWithParameters" asp-controller="Home" class="btn btn-primary">Send Video Template Message With Parameters</button>
</div>
</div>
</form>
Expand All @@ -53,4 +59,4 @@
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ public interface IWhatsAppBusinessClient
/// <returns>WhatsAppResponse</returns>
Task<WhatsAppResponse> SendVideoAttachmentMessageByIdAsync(VideoMessageByIdRequest videoMessage, CancellationToken cancellationToken = default);

/// <summary>
/// Send Video Message by Template
/// </summary>
/// <param name="videoMessage">VideoTemplateMessageRequest object</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
Task<WhatsAppResponse> SendVideoAttachmentTemplateMessageAsync(VideoTemplateMessageRequest videoTemplateMessageRequest, CancellationToken cancellationToken = default);

/// <summary>
/// Send Video Message by Media Id
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,18 @@ public async Task<WhatsAppResponse> SendVideoAttachmentMessageByIdAsync(VideoMes
return await WhatsAppBusinessPostAsync<WhatsAppResponse>(videoMessage, formattedWhatsAppEndpoint, cancellationToken);
}

/// <summary>
/// Send Video Template Message
/// </summary>
/// <param name="videoTemplateMessageRequest">VideoTemplateMessageRequest Object</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>WhatsAppResponse</returns>
public async Task<WhatsAppResponse> SendVideoAttachmentTemplateMessageAsync(VideoTemplateMessageRequest videoTemplateMessageRequest, CancellationToken cancellationToken = default)
{
var formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.SendMessage.Replace("{{Phone-Number-ID}}", _whatsAppConfig.WhatsAppBusinessPhoneNumberId);
return await WhatsAppBusinessPostAsync<WhatsAppResponse>(videoTemplateMessageRequest, formattedWhatsAppEndpoint, cancellationToken);
}

/// <summary>
/// Send Video Message using hyperlink
/// </summary>
Expand Down

0 comments on commit f7d40eb

Please sign in to comment.