Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SampleApp/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
builder.Configuration["EventGrid:Key"] is { Length: > 0 } key)
{
whatsapp.UseEventGridProcessor(new EventGridPublisherClient(
new Uri(topic), new Azure.AzureKeyCredential(key)));
new Uri(topic), new Azure.AzureKeyCredential(key)),
options => builder.Configuration.Bind("EventGrid", options));
}
}
else
Expand Down
35 changes: 30 additions & 5 deletions src/WhatsApp/AzureFunctionsProcessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,43 @@ class AzureFunctionsProcessors(Func<PipelineRunner> runner, IOptions<WhatsAppOpt
public Task DequeueAsync([QueueTrigger("whatsappwebhook", Connection = "AzureWebJobsStorage")] string json)
=> runner().ProcessAsync(json);

#if CI || RELEASE
[Function("whatsapp_eventgrid")]
public async Task<IActionResult> HandleEventGrid(
#if CI || RELEASE
[EventGridTrigger] EventGridEvent e)
#else
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "whatsapp/eventgrid")]
[Microsoft.Azure.Functions.Worker.Http.FromBody] EventGridEvent e)
#endif
{
await runner().ProcessAsync(Regex.Unescape(e.Data.ToString()).Trim('"'));
return new OkResult();
}
#else
[Function("whatsapp_eventgrid")]
public async Task<IActionResult> HandleEventGrid(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "whatsapp/eventgrid")] HttpRequest request)
{
using var sr = new StreamReader(request.Body);
var json = await sr.ReadToEndAsync();
var events = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonObject[]>(json);

// Validation handshake?
if (request.Headers.TryGetValue("aeg-event-type", out var aeg) && aeg.ToString() == "SubscriptionValidation" &&
events?[0]?["data"]?["validationCode"]?.ToString() is string code)
{
return new OkObjectResult(new { validationResponse = code }); // 200 with the code
}

// Normal events here...
var data = System.Text.Json.JsonSerializer.Deserialize<EventGridEvent[]>(json);
if (data == null)
return new OkResult();

foreach (var item in data)
{
await runner().ProcessAsync(Regex.Unescape(item.Data.ToString()).Trim('"'));
}

return new OkResult();
}
#endif

[Function("whatsapp_process")]
public async Task<IActionResult> ProcessAsync(
Expand Down
3 changes: 2 additions & 1 deletion src/WhatsApp/EventGridProcessorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Messaging.EventGrid;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Devlooped.WhatsApp;

Expand Down Expand Up @@ -27,7 +28,7 @@ public static WhatsAppHandlerBuilder UseEventGridProcessor(this WhatsAppHandlerB

builder.Services.AddSingleton<IMessageProcessor>(services =>
{
var options = new EventGridOptions();
var options = services.GetService<IOptions<EventGridOptions>>()?.Value ?? new();
configure?.Invoke(options);
return new EventGridProcessor(publisher ??
services.GetRequiredService<EventGridPublisherClient>(),
Expand Down