From 458d29acc7cc1564449f349e395bee41f56ff14a Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Wed, 1 May 2024 14:56:43 -0400 Subject: [PATCH 1/3] Put `participant.joined` and `participant.left` retry logic into a background function This change makes it so we fire off a request to the participant joined/left retry logic in a background function then return immediately to zoom in zoom-meeting-webhook-handler so zoom doesn't send a retry request Currently, we perform retry logic within the initial zoom request in zoom-meeting-webhook-handler, which is ostensibly causing zoom to time out --- .../index.js | 72 +++++++++++++++++++ .../zoom-meeting-webhook-handler/index.js | 24 +++---- netlify.toml | 5 ++ 3 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 functions/handle-participant-joined-left-background/index.js diff --git a/functions/handle-participant-joined-left-background/index.js b/functions/handle-participant-joined-left-background/index.js new file mode 100644 index 0000000..26881b0 --- /dev/null +++ b/functions/handle-participant-joined-left-background/index.js @@ -0,0 +1,72 @@ +require('dotenv').config(); + +const crypto = require('crypto'); + +const { updateMeetingStatus, updateMeetingAttendence } = require('../zoom-meeting-webhook-handler/slack.js'); + +const rooms = require('../../data/rooms.json'); + +const EVENT_MEETING_STARTED = 'meeting.started'; +const EVENT_MEETING_ENDED = 'meeting.ended'; +const EVENT_PARTICIPANT_JOINED = 'meeting.participant_joined'; +const EVENT_PARTICIPANT_LEFT = 'meeting.participant_left'; + +const ZOOM_SECRET = + process.env.TEST_ZOOM_WEBHOOK_SECRET_TOKEN || + process.env.ZOOM_WEBHOOK_SECRET_TOKEN; + +const ZOOM_AUTH = + process.env.TEST_ZOOM_WEBHOOK_AUTH || process.env.ZOOM_WEBHOOK_AUTH; + +const handler = async function (event, context) { + try { + const request = JSON.parse(event.body); + + // check our meeting ID. The meeting ID never changes, but the uuid is different for each instance + + const room = rooms.find( + (room) => room.ZoomMeetingId === request.payload.object.id + ); + + if (room) { + const Airtable = require('airtable'); + const base = new Airtable().base(process.env.AIRTABLE_COWORKING_BASE); + + const { findRoomInstance } = require('../zoom-meeting-webhook-handler/airtable'); + + let roomInstance = await findRoomInstance( + room, + base, + request.payload.object.uuid + ); + + if (roomInstance) { + // create room event record + console.log(`found room instance ${roomInstance.getId()}`); + + const updatedMeeting = await updateMeetingAttendence( + room, + roomInstance.get('slack_thread_timestamp'), + request + ); + } + } else { + console.log('meeting ID is not co-working meeting'); + } + + return { + statusCode: 200, + body: '', + }; + } catch (error) { + // output to netlify function log + console.log(error); + return { + statusCode: 500, + // Could be a custom message or object i.e. JSON.stringify(err) + body: JSON.stringify({ msg: error.message }), + }; + } +}; + +module.exports = { handler }; \ No newline at end of file diff --git a/functions/zoom-meeting-webhook-handler/index.js b/functions/zoom-meeting-webhook-handler/index.js index 76fa365..7fa738f 100644 --- a/functions/zoom-meeting-webhook-handler/index.js +++ b/functions/zoom-meeting-webhook-handler/index.js @@ -18,6 +18,8 @@ const ZOOM_SECRET = const ZOOM_AUTH = process.env.TEST_ZOOM_WEBHOOK_AUTH || process.env.ZOOM_WEBHOOK_AUTH; +const APP_HOST = process.env.TEST_APP_HOST || process.env.APP_HOST; + const handler = async function (event, context) { try { /** @@ -96,22 +98,14 @@ const handler = async function (event, context) { switch (request.event) { case EVENT_PARTICIPANT_JOINED: case EVENT_PARTICIPANT_LEFT: - let roomInstance = await findRoomInstance( - room, - base, - request.payload.object.uuid - ); + console.log('CALLING handle-participant-joined-left-background') - if (roomInstance) { - // create room event record - console.log(`found room instance ${roomInstance.getId()}`); + response = await fetch(`${APP_HOST}/handle-participant-joined-left-background`, { + method: 'POST', + body: event.body, + }); - const updatedMeeting = await updateMeetingAttendence( - room, - roomInstance.get('slack_thread_timestamp'), - request - ); - } + console.log('EXITING IMMEDIATELY FROM zoom-meeting-webhook-handler') break; @@ -190,4 +184,4 @@ const handler = async function (event, context) { } }; -module.exports = { handler }; +module.exports = { handler }; \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index 287f05a..227216e 100644 --- a/netlify.toml +++ b/netlify.toml @@ -26,3 +26,8 @@ from = "/event-reminders" to = "/.netlify/functions/event-reminders-background" status = 200 + +[[redirects]] + from = "/handle-participant-joined-left-background" + to = "/.netlify/functions/handle-participant-joined-left-background" + status = 200 From 9bc67880924251d6c8bf274f82edec875e00711f Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Wed, 1 May 2024 15:18:35 -0400 Subject: [PATCH 2/3] Remove unused imports in handle-participant-joined-left-background --- .../index.js | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/functions/handle-participant-joined-left-background/index.js b/functions/handle-participant-joined-left-background/index.js index 26881b0..2312907 100644 --- a/functions/handle-participant-joined-left-background/index.js +++ b/functions/handle-participant-joined-left-background/index.js @@ -1,23 +1,9 @@ require('dotenv').config(); -const crypto = require('crypto'); - -const { updateMeetingStatus, updateMeetingAttendence } = require('../zoom-meeting-webhook-handler/slack.js'); +const { updateMeetingAttendence } = require('../zoom-meeting-webhook-handler/slack.js'); const rooms = require('../../data/rooms.json'); -const EVENT_MEETING_STARTED = 'meeting.started'; -const EVENT_MEETING_ENDED = 'meeting.ended'; -const EVENT_PARTICIPANT_JOINED = 'meeting.participant_joined'; -const EVENT_PARTICIPANT_LEFT = 'meeting.participant_left'; - -const ZOOM_SECRET = - process.env.TEST_ZOOM_WEBHOOK_SECRET_TOKEN || - process.env.ZOOM_WEBHOOK_SECRET_TOKEN; - -const ZOOM_AUTH = - process.env.TEST_ZOOM_WEBHOOK_AUTH || process.env.ZOOM_WEBHOOK_AUTH; - const handler = async function (event, context) { try { const request = JSON.parse(event.body); From 2e9b3627384fb52fe3bb17476baa05335983c4cf Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Tue, 7 May 2024 10:35:47 -0400 Subject: [PATCH 3/3] Add error checking code --- functions/zoom-meeting-webhook-handler/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions/zoom-meeting-webhook-handler/index.js b/functions/zoom-meeting-webhook-handler/index.js index 7fa738f..4ae7300 100644 --- a/functions/zoom-meeting-webhook-handler/index.js +++ b/functions/zoom-meeting-webhook-handler/index.js @@ -105,6 +105,10 @@ const handler = async function (event, context) { body: event.body, }); + if (!response.ok) { + throw new Error(`Error: ${response.status} ${response.statusText}`); + } + console.log('EXITING IMMEDIATELY FROM zoom-meeting-webhook-handler') break;