import { Hono } from "npm:hono";
const app = new Hono();
app.get("/", (c) => {
return c.html(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Randomize Notion Block Color</title>
<style>
body { margin: 0; padding: 20px; }
button {
padding: 10px 20px;
font-size: 18px;
background-color: #4a90e2;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
border: 1px solid rgba(255,255,255,0.3);
}
</style>
</head>
<body>
<button id="randomizeBtn">RANDOMIZE</button>
<script>
document.getElementById('randomizeBtn').addEventListener('click', async () => {
const response = await fetch('/randomize', { method: 'POST' });
const result = await response.json();
alert(result.message);
});
</script>
</body>
</html>
`);
});
app.post("/randomize", async (c) => {
const blockId = Deno.env.get("TOWNIE_BLOCK_ID");
const notionApiKey = Deno.env.get("TOWNIE_NOTION_API_KEY");
if (!blockId || !notionApiKey) {
return c.json({ message: "Missing environment variables" }, 500);
}
const colors = ["blue_background", "brown_background", "gray_background", "green_background", "orange_background", "pink_background", "purple_background", "red_background", "yellow_background"];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
const response = await fetch(`https://api.notion.com/v1/blocks/${blockId}`, {
method: "PATCH",
headers: {
"Authorization": `Bearer ${notionApiKey}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
},
body: JSON.stringify({
callout: { color: randomColor }
}),
});
if (response.ok) {
return c.json({ message: `Block color changed to ${randomColor}` });
} else {
return c.json({ message: "Failed to update block color" }, 500);
}
});
export default app.fetch;