Skip to content
Merged
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
14 changes: 13 additions & 1 deletion src/WhatsApp/AzureFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public async Task<HttpResponseMessage> Message([HttpTrigger(AuthorizationLevel.A

if (await WhatsApp.Message.DeserializeAsync(json) is { } message)
{
// Ensure idempotent processing
var table = tableClient.GetTableClient("whatsapp");
await table.CreateIfNotExistsAsync();
if (await table.GetEntityIfExistsAsync<TableEntity>(message.From.Number, message.Id) is { HasValue: true } existing)
{
logger.LogInformation("Skipping already handled message {Id}", message.Id);
return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
}

// Otherwise, queue the new message
var queue = queueClient.GetQueueClient("whatsapp");
await queue.CreateIfNotExistsAsync();
await queue.SendMessageAsync(json);
Expand All @@ -78,7 +88,9 @@ public async Task Process([QueueTrigger("whatsapp", Connection = "AzureWebJobsSt

if (await WhatsApp.Message.DeserializeAsync(json) is { } message)
{
// Ensure idempotent processing
// Ensure idempotent processing at dequeue time, since we might have been called
// multiple times for the same message by WhatsApp (Message method) while processing was still
// happening (and therefore we didn't save the entity yet).
var table = tableClient.GetTableClient("whatsapp");
await table.CreateIfNotExistsAsync();
if (await table.GetEntityIfExistsAsync<TableEntity>(message.From.Number, message.Id) is { HasValue: true } existing)
Expand Down