Readme

sending sam a random poem from poetrydb.org every day forever...

-> cron val

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
import { fetch } from "https://esm.town/v/std/fetch";
async function getRandomPoem() {
const response = await fetch("https://poetrydb.org/random");
const [poem] = await response.json();
return poem;
}
export async function sendDailyPoem() {
try {
const poem = await getRandomPoem();
const title = poem.title;
const author = poem.author;
const lines = poem.lines.join("\n");
const message = `Daily Poem:\n\n${title}\nby ${author}\n\n${lines}`;
const response = await fetch('https://samwho-notify.web.val.run', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: "Daily Poem from Thomas",
message
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to send message: ${response.status} ${response.statusText}, ${errorText}`);
}
console.log("Daily poem sent successfully");
} catch (error) {
console.error("Error sending daily poem:", error);
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 9, 2024