Runs every 15 min
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
/**
* This val will create a daily weather email service.
* It uses the OpenWeatherMap API to fetch weather data and the Val Town email API to send emails.
* The val will be triggered daily using a cron job.
*/
import { email } from "https://esm.town/v/std/email";
import { OpenAI } from "https://esm.town/v/std/openai";
const OPENWEATHERMAP_API_KEY = Deno.env.get("WEATHER_API_KEY");
const USER_EMAIL = Deno.env.get("USER_EMAIL");
const CITY = "Córdoba, España"; // Change this to your desired city
async function getWeatherData() {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${CITY}&appid=${OPENWEATHERMAP_API_KEY}&units=metric`,
);
return await response.json();
}
async function generateEmailContent(weatherData) {
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You are a helpful assistant that creates engaging weather reports." },
{
role: "user",
content: `Create a brief, friendly weather report for ${CITY} based on this data: ${
JSON.stringify(weatherData)
}. Include the temperature, weather conditions, and a fun fact or tip related to the weather.`,
},
],
model: "gpt-4o-mini",
max_tokens: 150,
});
return completion.choices[0].message.content;
}
async function sendWeatherEmail() {
const weatherData = await getWeatherData();
const emailContent = await generateEmailContent(weatherData);
await email.send({
to: USER_EMAIL,
subject: `Daily Weather Report for ${CITY}`,
text: emailContent,
});
}
export default async function server(req: Request): Promise<Response> {
if (req.headers.get("User-Agent")?.includes("valtown-cron")) {
await sendWeatherEmail();
return new Response("Weather email sent successfully!");
}
return new Response(
`
<html>
<head>
<title>Daily Weather Email Service</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #2c3e50;
}
p {
margin-bottom: 20px;
}
.footer {
margin-top: 40px;
font-size: 0.8em;
color: #7f8c8d;
}
</style>
</head>
<body>
<h1>Daily Weather Email Service</h1>
<p>This service sends a daily weather report for ${CITY} to ${USER_EMAIL}.</p>
<p>The email includes:</p>
<ul>
<li>Current temperature</li>
<li>Weather conditions</li>
<li>A fun fact or tip related to the weather</li>
</ul>
<p>The service runs automatically every day. No action is required on this page.</p>
<div class="footer">
<p>View source: <a href="${import.meta.url.replace("esm.town", "val.town")}">${
import.meta.url.replace("esm.town", "val.town")
}</a></p>
</div>
</body>
</html>
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!
August 23, 2024