Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Switchbot Party

We hosted a party in the Val Town office and I texted a link to this site to all guests:

Screenshot 2024-08-05 at 12.30.16@2x.png

You can see a demo here: https://x.com/stevekrouse/status/1819018859099132128

After the party was over, I commented out the lines that open the door so folks can't use it anymore.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource npm:hono/jsx */
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { email } from "https://esm.town/v/std/email?v=12";
import { Buffer } from "node:buffer";
import { createHmac } from "node:crypto";
import { Hono } from "npm:hono";
const app = new Hono();
const ValTownGroundFloorDeviceId = "D2353438164D";
const ValTownOfficeDeviceId = "CD6F3A810848";
async function switchbotRequest(path, args) {
const token = Deno.env.get("SWITCHBOT_TOKEN");
const secret = Deno.env.get("SWITCHBOT_KEY");
const t = Date.now();
const nonce = Math.floor(Math.random() * 1000000);
const sign = createHmac("sha256", secret)
.update(Buffer.from(token + t + nonce, "utf-8"))
.digest()
.toString("base64");
const response = await fetch(`https://api.switch-bot.com/${path}`, {
headers: {
"Authorization": token,
t,
nonce,
sign,
"Content-Type": "application/json",
},
...args,
});
return response.json();
}
function botPress(device) {
return switchbotRequest(`v1.1/devices/${device}/commands`, {
method: "POST",
body: JSON.stringify({
"command": "press",
"parameter": "default",
"commandType": "command",
}),
});
}
const styles = `
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.1);
padding: 2rem;
border-radius: 15px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
backdrop-filter: blur(4px);
border: 1px solid rgba(255, 255, 255, 0.18);
}
h1 {
font-size: 2.5rem;
margin-bottom: 1.5rem;
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}
button {
font-size: 1.5rem;
padding: 15px 30px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 4px 15px 0 rgba(49, 196, 190, 0.75);
}
button:hover {
background-color: #45a049;
transform: translateY(-2px);
box-shadow: 0 6px 20px 0 rgba(49, 196, 190, 0.75);
}
a {
color: #ffffff;
text-decoration: none;
font-weight: bold;
transition: all 0.3s;
}
a:hover {
text-decoration: underline;
}
.val-link {
margin-top: 20px;
display: inline-block;
just_be-switchbot_party.web.val.run
September 14, 2024