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 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-color: #f0f0f0;
color: #333;
}
.container {
text-align: center;
background: white;
padding: 2rem;
border-radius: 15px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 2.5rem;
margin-bottom: 1.5rem;
}
button {
font-size: 1.5rem;
padding: 15px 30px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
a {
color: #4CAF50;
text-decoration: none;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
.val-link {
margin-top: 20px;
display: inline-block;