Skip to content

Commit

Permalink
Update outbox status on outbound Twilio webhook calls
Browse files Browse the repository at this point in the history
  • Loading branch information
beverloo committed Apr 17, 2024
1 parent ba5f009 commit 56a653e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/admin/system/outbox/TwilioDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ export function TwilioDataTable(props: TwilioDataTableProps) {
{
field: 'delivered',
display: 'flex',
headerName: 'Accepted',
headerName: 'Delivered',
headerAlign: 'center',
align: 'center',
description: 'Whether the message was accepted by Twilio',
description: 'Whether the message was successfully delivered',
sortable: true,
width: 100,

Expand Down
22 changes: 21 additions & 1 deletion app/api/webhook/twilio/outbound/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NextRequest } from 'next/server';

import { TwilioWebhookEndpoint } from '@lib/database/Types';
import { authenticateAndRecordTwilioRequest } from '../authenticateAndRecordTwilioRequest';
import db, { tOutboxTwilio } from '@lib/database';

/**
* Webhook invoked when the status of an outbound message has been updated. This may happen minutes,
Expand All @@ -14,7 +15,26 @@ export async function POST(request: NextRequest) {
const { authenticated, body } =
await authenticateAndRecordTwilioRequest(request, TwilioWebhookEndpoint.Outbound);

// TODO: Do something with the `body`
if (!authenticated || !body) {
return new Response(undefined, {
status: /* HTTP Forbidden= */ 403,
});
}

const from = body.get('From');
const messageSid = body.get('MessageSid');
const messageStatus = body.get('MessageStatus');

if (!!from && !!messageSid && !!messageStatus) {
const normalisedFrom = from.replace(/.*\:/, ''); // whatsapp:+44... -> +44...
await db.update(tOutboxTwilio)
.set({
outboxSender: normalisedFrom,
outboxResultStatus: messageStatus
})
.where(tOutboxTwilio.outboxResultSid.equals(messageSid))
.executeUpdate();
}

return new Response(undefined, {
status: /* HTTP OK= */ 200,
Expand Down

0 comments on commit 56a653e

Please sign in to comment.