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
const TELEGRAM_CHAT_ID = Deno.env.get("TELEGRAM_CHAT_ID");
const TELEGRAM_TOKEN = Deno.env.get("TELEGRAM_TOKEN");
const latitude = Deno.env.get("LATITUDE");
const longitude = Deno.env.get("LONGITUDE");
const openWeatherMapAppId = Deno.env.get("OPENWEATHERMAP_APP_ID");
type WeatherData = {
date: string;
minTemperature: string;
maxTemperature: string;
willItRain: string;
willItSnow: string;
totalRain: string;
};
type Forecast = {
today: WeatherData;
tomorrow: WeatherData;
dayAfterTomorrow: WeatherData;
};
const formatWeatherForecast = (forecast: Forecast): string => {
const formatDate = (data: WeatherData) => `
<b>Date:</b> ${data.date}%0A
<b>Min Temperature:</b> <i>${data.minTemperature} °C</i>%0A
<b>Max Temperature:</b> <i>${data.maxTemperature} °C</i>%0A
${data.willItRain === "Yes" ? `<b>Rain:</b> ${data.willItRain} (${data.totalRain})%0A` : ""}
${data.willItSnow === "Yes" ? `<b>Snow:</b> ${data.willItSnow}%0A` : ""}`;
return `
<b>Weather Forecast</b>🌤️%0A
<u>Today:</u>%0A
${formatDate(forecast.today)}
<u>Tomorrow:</u>%0A
${formatDate(forecast.tomorrow)}
<u>Day After Tomorrow:</u>%0A
${formatDate(forecast.dayAfterTomorrow)}
`;
};
const extractForecast = (weatherData, date) => {
const forecasts = weatherData.list.filter(entry => entry.dt_txt.startsWith(date));
let minTemp = forecasts[0].main.temp_min;
let maxTemp = forecasts[0].main.temp_max;
let willRain = false;
let willSnow = false;
let totalRain = 0; // Initialize total rain
// Iterate over the forecasts to find min, max temperatures and rain/snow status
forecasts.forEach(entry => {
if (entry.main.temp_min < minTemp) minTemp = entry.main.temp_min;
if (entry.main.temp_max > maxTemp) maxTemp = entry.main.temp_max;
if (entry.weather.some(w => w.main.toLowerCase() === "rain")) {
willRain = true;
if (entry.rain && entry.rain["3h"]) {
totalRain += entry.rain["3h"]; // Add the rain volume from this forecast to the total
}
}
if (entry.weather.some(w => w.main.toLowerCase() === "snow")) {
willSnow = true;
}
});
const result = {
date,
minTemperature: minTemp.toFixed(2),
maxTemperature: maxTemp.toFixed(2),
willItRain: willRain ? "Yes" : "No",
willItSnow: willSnow ? "Yes" : "No",
totalRain: willRain ? totalRain.toFixed(2) + " mm" : "0 mm", // Always include totalRain, set to "0 mm" if no rain
};
return result;
};
const fetchForecast = async () => {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&units=metric&appid=${openWeatherMapAppId}`,
);
const data = await response.json();
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
const dayAfterTomorrow = new Date(today);
dayAfterTomorrow.setDate(today.getDate() + 2);
const todayForecast = extractForecast(data, today.toISOString().slice(0, 10));
const tomorrowForecast = extractForecast(data, tomorrow.toISOString().slice(0, 10));
const dayAfterTomorrowForecast = extractForecast(data, dayAfterTomorrow.toISOString().slice(0, 10));
const forecastString = {
today: todayForecast,
tomorrow: tomorrowForecast,
dayAfterTomorrow: dayAfterTomorrowForecast,
};
return forecastString;
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!
May 3, 2024