-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
80 lines (63 loc) · 2.78 KB
/
Copy pathcode.gs
File metadata and controls
80 lines (63 loc) · 2.78 KB
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
function sendSlackNotification(message) {
const SLACK_WEBHOOK_URL = PropertiesService.getScriptProperties().getProperty('SLACK_WEBHOOK_URL');
if (!SLACK_WEBHOOK_URL) {
throw new Error('SLACK_WEBHOOK_URL not configured in Script Properties');
}
const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify({ text: message })
};
UrlFetchApp.fetch(SLACK_WEBHOOK_URL, options);
}
function checkWeather() {
const properties = PropertiesService.getScriptProperties();
const OPENWEATHER_API_KEY = properties.getProperty('OPENWEATHER_API_KEY');
const CITY_1 = properties.getProperty('CITY_1') || 'Boston';
const CITY_2 = properties.getProperty('CITY_2') || 'Phoenix';
if (!OPENWEATHER_API_KEY) {
throw new Error('OPENWEATHER_API_KEY not configured in Script Properties');
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Initialize headers if needed
if (sheet.getLastRow() === 0) {
sheet.appendRow(['Timestamp', `${CITY_1} Temp (°F)`, `${CITY_2} Temp (°F)`, `${CITY_2} Colder?`, 'State Changed?']);
}
try {
// Fetch weather data
const city1Temp = getTemperature(CITY_1, OPENWEATHER_API_KEY);
const city2Temp = getTemperature(CITY_2, OPENWEATHER_API_KEY);
const timestamp = new Date().toLocaleString();
// Compare states
const wasCity2Colder = properties.getProperty('city2Colder') === 'true';
const isCity2Colder = city2Temp < city1Temp;
const stateChanged = isCity2Colder !== wasCity2Colder;
// Log to sheet
sheet.appendRow([timestamp, city1Temp, city2Temp, isCity2Colder, stateChanged]);
// Notify on state change
if (stateChanged) {
const message = isCity2Colder
? `🌡️ Temperature Inversion!\n${CITY_2} (${city2Temp}°F) is now colder than ${CITY_1} (${city1Temp}°F)\nTime: ${timestamp}`
: `🌡️ Back to Normal\n${CITY_2} (${city2Temp}°F) is no longer colder than ${CITY_1} (${city1Temp}°F)\nTime: ${timestamp}`;
sendSlackNotification(message);
}
properties.setProperty('city2Colder', isCity2Colder.toString());
} catch (error) {
Logger.log(`Error: ${error}`);
sheet.appendRow([new Date().toLocaleString(), 'ERROR', error.toString()]);
}
}
function getTemperature(city, apiKey) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&units=imperial&appid=${apiKey}`;
const response = UrlFetchApp.fetch(url);
return JSON.parse(response.getContentText()).main.temp;
}
function createTrigger() {
// Remove existing triggers
ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));
// Run every 10 minutes
ScriptApp.newTrigger('checkWeather')
.timeBased()
.everyMinutes(10)
.create();
}