Skip to content

Commit c06d6d1

Browse files
Edward BannerEdward Banner
authored andcommitted
Clean up code
1 parent fb0112f commit c06d6d1

File tree

1 file changed

+45
-14
lines changed
  • functions/zoom-meeting-webhook-handler

1 file changed

+45
-14
lines changed

functions/zoom-meeting-webhook-handler/index.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,25 @@ async function getCallIdFromChannel() {
6767
}
6868

6969

70-
async function addParticipant(user) {
70+
async function addParticipantToCall(zoomEvent) {
71+
const slackUser = toSlackUser(zoomEvent);
7172
const call_id = await getCallIdFromChannel();
72-
await slack.calls.participants.add({ id: call_id, users: [user] });
73+
await slack.calls.participants.add({ id: call_id, users: [slackUser] });
7374
}
7475

7576

76-
async function removeParticipant(user) {
77+
async function removeParticipantFromCall(zoomEvent) {
78+
const slackUser = toSlackUser(zoomEvent);
7779
const call_id = await getCallIdFromChannel();
78-
await slack.calls.participants.remove({ id: call_id, users: [user] });
80+
await slack.calls.participants.remove({ id: call_id, users: [slackUser] });
7981
}
8082

8183

8284
function toSlackUser(zoomEvent) {
83-
const zoomName = zoomEvent?.payload?.object?.participant?.user_name;
85+
const zoomDisplayName = zoomEvent?.payload?.object?.participant?.user_name;
8486
return {
85-
external_id: zoomName,
86-
display_name: zoomName,
87+
external_id: zoomDisplayName,
88+
display_name: zoomDisplayName,
8789
};
8890
}
8991

@@ -94,36 +96,65 @@ function isSlashCommand(event) {
9496
}
9597

9698

99+
function getSlashCommand(event) {
100+
const body = parseBody(event);
101+
return typeof body?.command === "string" && body.command;
102+
}
103+
104+
105+
function getSlashText(event) {
106+
const body = parseBody(event);
107+
return typeof body?.text === "string" && body.text;
108+
}
109+
110+
97111
async function getActiveParticipants() {
98112
const resp = await slack.conversations.history({ channel: SLACK_COWORKING_CHANNEL_ID, limit: 1 });
99113
return resp?.messages?.[0]?.blocks?.[0]?.call?.v1?.active_participants ?? [];
100114
}
101115

102116

117+
async function endCall() {
118+
const call_id = await getCallIdFromChannel();
119+
await slack.calls.end({ id: call_id });
120+
}
121+
122+
103123
exports.handler = async function(event) {
104124
if (isSlashCommand(event)) {
105-
const call_id = await handleStartCall();
106-
return { statusCode: 200, body: JSON.stringify(call_id) }
125+
const slashCommand = getSlashCommand(event);
126+
if (slashCommand === "/co-working-room") {
127+
const slashText = getSlashText(event);
128+
if (slashText == "end") {
129+
await endCall();
130+
}
131+
const call_id = await handleStartCall();
132+
return { statusCode: 200, body: JSON.stringify(call_id) }
133+
}
107134
}
108135

109136
// Zoom webhooks
110137
const zoomEvent = parseBody(event);
111138
const zoomEventName = zoomEvent?.event;
139+
112140
if (zoomEventName === "endpoint.url_validation") {
113-
return json(200, handleValidation(zoomEvent));
141+
return {
142+
statusCode: 200,
143+
headers: { "Content-Type": "application/json" },
144+
body: JSON.stringify(handleValidation(zoomEvent))
145+
}
114146
}
115147

116148
else if (zoomEventName === "meeting.participant_joined") {
117-
await addParticipant(toSlackUser(zoomEvent));
149+
await addParticipantToCall(zoomEvent);
118150
return { statusCode: 204 };
119151
}
120152

121153
else if (zoomEventName === "meeting.participant_left") {
122-
await removeParticipant(toSlackUser(zoomEvent));
154+
await removeParticipantFromCall(zoomEvent);
123155
const active = await getActiveParticipants();
124156
if (active.length === 0) {
125-
const call_id = await getCallIdFromChannel();
126-
await slack.calls.end({ id: call_id });
157+
await endCall();
127158
}
128159
return { statusCode: 204 };
129160
}

0 commit comments

Comments
 (0)