Skip to content

Instantly share code, notes, and snippets.

@svenk
Last active December 31, 2020 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenk/bfec70b749514539dab5b502cb4be21b to your computer and use it in GitHub Desktop.
Save svenk/bfec70b749514539dab5b502cb4be21b to your computer and use it in GitHub Desktop.
"Spin the bottle" with Zoom using the Zoom Webhook API
<html>
<meta charset="utf-8">
<title>Genie in a Bottle</title>
<meta author="SvenK">
<meta date="31.12.2020">
<?php
# First of all, this is supposed only to work for a specific
# room. Fix the room id here:
$do_require_room = False;
$required_room_id = "..."; # 83937890291
/* Participant list is just serialized in a plain text file */
$users = array_filter(explode("\n", file_get_contents("users.txt")));
function log_data($data) {
$log = "-------- New entry recieved on " . date("r") . "-----\n";
$log .= var_export($data, /* return = */ true);
file_put_contents("log.txt", $log, FILE_APPEND);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$post = file_get_contents("php://input");
$payload = json_decode($post, true);
if($payload) { // and in_array("event", $payload)) {
# Expect this to be a correctly formed Zoom Webhook info
log_data($payload);
$event = $payload["event"];
$room_id = $payload["payload"]["object"]["id"];
if($do_require_room && ($room_id != $required_room_id)) {
log_data("Skipping above message, wrong room ($room_id != $required_room_id !");
exit;
}
if($event == "meeting.participant_joined") {
$users[] = htmlentities($payload["payload"]["object"]["participant"]["user_name"]);
}
if($event == "meeting.participant_left") {
$username = htmlentities($payload["payload"]["object"]["participant"]["user_name"]);
// horrible PHP
if (($key = array_search($username, $users)) !== false) {
unset($users[$key]);
}
}
file_put_contents("users.txt", join("\n", $users));
}
echo "Done";
exit;
}
$login = file_get_contents("login.txt");
$old_login = $login;
if(isset($_GET["login"])) {
$login = $_GET["login"];
$login = htmlentities($login); // basic sanatizing
if($login != $old_login) {
file_put_contents("login.txt", $login);
log_data("Neuer Login wurde gesetzt: $login");
}
}
if(!$login) $login = "niemand!";
?>
<style type="text/css">
body { font-family: Arial, sans-serif;
font-size: 120%;}
h1, h2 { text-align: center; }
h3 { margin: 0; }
section {
border: 5px solid #dbba6c;
background-color: #fff6f8;
padding: 2em;
max-width: 60%;
margin: .2em auto;
}
section.master {
text-align: center;
background-color: #fcfdfc;
font-size: 110%;
border-color: green;
}
#login, a:link, a:visited {
display: inline-block;
padding: 4px;
border: 3px solid green;
text-decoration: none;
color: black;
}
a:hover {
background-color: #cff7cf;
}
#login {
font-size: 130%;
font-weight: Bold;
text-decoration: underline;
}
ul, li {
list-style: none;
display: inline-block;
margin: 0; padding: 0;
}
li a {
margin: 0 .2em;
}
div.half section {
width: 40%;
float: left;
margin: 1em;
}
</style>
<body>
<h1>Genie in a Bottle</h1>
<section class="master">
<p>Unter folgenden Teilnehmern:
<ul>
<?php foreach($users as $user) echo "<li><a href='?login=$user'>$user</a></li>"; ?>
</ul>
<p>zeigt die Flasche auf:
<div id="login"><?=$login; ?></div>
</section>
<h2>Flasche neu drehen</h2>
<div class="half">
<section class="left">
<h3>Ziehen mit Zurücklegen</h3>
<?php
if(count($users)) {
$users_rand = $users[array_rand($users)];
} else {
$users_rand = "<em>Niemand zur Auswahl!</em>";
}
?>
<!--<br>Es wurde zufällig ausgewählt: <strong><?=$users_rand; ?></strong>-->
<?php if(count($users)) echo "<a href='?login=$users_rand'>Flasche neu drehen</a>"; ?>
</section>
<section class="right">
<h3>Ziehen ohne Zurücklegen</h3>
<?php
if(isset($_GET["reset"])) {
file_put_contents("urne.txt", join("\n", $users));
log_data("Urne: Reset durchgeführt");
$login = false;
}
$urners = array_filter(explode("\n", file_get_contents("urne.txt")));
if(count($urners)) {
$urne_rand = $urners[array_rand($urners)];
} else {
$urne_rand = "<em>Urne ist leer!</em>";
}
// horrible PHP
if (($key = array_search($login, $urners)) !== false) {
unset($urners[$key]);
file_put_contents("urne.txt", join("\n", $urners));
log_data("Aus Urne wurde gezogen");
}
?>
<br>Urneninhalt: <?=count($urners); ?> Teilnehmer von <?=count($users); ?> Verfügbaren.
<a href="?reset">Urne zurücksetzen</a>
<!--<br>Es wurde zufällig ausgewählt: <strong><?=$urne_rand; ?></strong>-->
<?php if(count($urners)) echo "<a href='?login=$urne_rand'>KandidatIn aus Urne ziehen</a>"; ?>
</section>
</div>
<script type="text/javascript">
/* Poll the log file (=fastest changing file) for new Zoom API information */
var curtag = "";
etag = () => fetch("log.txt", { method: 'HEAD' }).then(resp => resp.headers.get("etag"));
check = () => etag().then(nt => { if(curtag && (nt != curtag)) location.href = '?'; })
// debug:
// etag().then(console.log)
window.addEventListener("load", () => {
etag().then(nt => {
curtag = nt;
setInterval(check, 1000 /* ms */);
})
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment