@@ -67,23 +67,25 @@ async function getCallIdFromChannel() {
67
67
}
68
68
69
69
70
- async function addParticipant ( user ) {
70
+ async function addParticipantToCall ( zoomEvent ) {
71
+ const slackUser = toSlackUser ( zoomEvent ) ;
71
72
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 ] } ) ;
73
74
}
74
75
75
76
76
- async function removeParticipant ( user ) {
77
+ async function removeParticipantFromCall ( zoomEvent ) {
78
+ const slackUser = toSlackUser ( zoomEvent ) ;
77
79
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 ] } ) ;
79
81
}
80
82
81
83
82
84
function toSlackUser ( zoomEvent ) {
83
- const zoomName = zoomEvent ?. payload ?. object ?. participant ?. user_name ;
85
+ const zoomDisplayName = zoomEvent ?. payload ?. object ?. participant ?. user_name ;
84
86
return {
85
- external_id : zoomName ,
86
- display_name : zoomName ,
87
+ external_id : zoomDisplayName ,
88
+ display_name : zoomDisplayName ,
87
89
} ;
88
90
}
89
91
@@ -94,36 +96,65 @@ function isSlashCommand(event) {
94
96
}
95
97
96
98
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
+
97
111
async function getActiveParticipants ( ) {
98
112
const resp = await slack . conversations . history ( { channel : SLACK_COWORKING_CHANNEL_ID , limit : 1 } ) ;
99
113
return resp ?. messages ?. [ 0 ] ?. blocks ?. [ 0 ] ?. call ?. v1 ?. active_participants ?? [ ] ;
100
114
}
101
115
102
116
117
+ async function endCall ( ) {
118
+ const call_id = await getCallIdFromChannel ( ) ;
119
+ await slack . calls . end ( { id : call_id } ) ;
120
+ }
121
+
122
+
103
123
exports . handler = async function ( event ) {
104
124
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
+ }
107
134
}
108
135
109
136
// Zoom webhooks
110
137
const zoomEvent = parseBody ( event ) ;
111
138
const zoomEventName = zoomEvent ?. event ;
139
+
112
140
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
+ }
114
146
}
115
147
116
148
else if ( zoomEventName === "meeting.participant_joined" ) {
117
- await addParticipant ( toSlackUser ( zoomEvent ) ) ;
149
+ await addParticipantToCall ( zoomEvent ) ;
118
150
return { statusCode : 204 } ;
119
151
}
120
152
121
153
else if ( zoomEventName === "meeting.participant_left" ) {
122
- await removeParticipant ( toSlackUser ( zoomEvent ) ) ;
154
+ await removeParticipantFromCall ( zoomEvent ) ;
123
155
const active = await getActiveParticipants ( ) ;
124
156
if ( active . length === 0 ) {
125
- const call_id = await getCallIdFromChannel ( ) ;
126
- await slack . calls . end ( { id : call_id } ) ;
157
+ await endCall ( ) ;
127
158
}
128
159
return { statusCode : 204 } ;
129
160
}
0 commit comments