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
20 changes: 8 additions & 12 deletions src/WhatsApp/AzureFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Azure.Data.Tables;
using Azure.Storage.Queues;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -25,7 +26,7 @@ public class AzureFunctions(
ILogger<AzureFunctions> logger)
{
[Function("whatsapp_message")]
public async Task<HttpResponseMessage> Message([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "whatsapp")] HttpRequest req)
public async Task<IActionResult> Message([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "whatsapp")] HttpRequest req)
{
using var reader = new StreamReader(req.Body, Encoding.UTF8);
var json = await reader.ReadToEndAsync();
Expand All @@ -39,7 +40,7 @@ public async Task<HttpResponseMessage> Message([HttpTrigger(AuthorizationLevel.A
if (await table.GetEntityIfExistsAsync<TableEntity>(message.From.Number, message.NotificationId) is { HasValue: true } existing)
{
logger.LogInformation("Skipping already handled message {Id}", message.Id);
return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
return new OkResult();
}

// Otherwise, queue the new message
Expand Down Expand Up @@ -68,7 +69,7 @@ public async Task<HttpResponseMessage> Message([HttpTrigger(AuthorizationLevel.A
logger.LogWarning("Unsupported message type received: \r\n{Payload}", json);
}

return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
return new OkResult();
}

[Function("whatsapp_process")]
Expand Down Expand Up @@ -100,23 +101,18 @@ public async Task Process([QueueTrigger("whatsapp", Connection = "AzureWebJobsSt
}

[Function("whatsapp_register")]
public HttpResponseMessage Register([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "whatsapp")] HttpRequest req)
public IActionResult Register([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "whatsapp")] HttpRequest req)
{
if (req.Query.TryGetValue("hub.mode", out var mode) && mode == "subscribe" &&
req.Query.TryGetValue("hub.verify_token", out var token) && token == options.Value.VerifyToken &&
req.Query.TryGetValue("hub.challenge", out var values) &&
values.ToString() is { } challenge)
{
logger.LogInformation("Registering webhook callback.");
return new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent(challenge, Encoding.UTF8, "text/plain")
};

return new OkObjectResult(challenge);
}

return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)
{
Content = new StringContent("Received verification token doesn't match the configured one.", Encoding.UTF8, "text/plain")
};
return new BadRequestObjectResult("Received verification token doesn't match the configured one.");
}
}
2 changes: 2 additions & 0 deletions src/WhatsApp/WhatsApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ItemGroup>
<PackageReference Include="Azure.Data.Tables" Version="12.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.2" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
Expand Down