fix: rewrite health-sync.js for Scripting app API

- fetch() instead of new Request() + req.loadJSON() (Scriptable API)
- Notification.schedule({title, body}) instead of new Notification() + n.schedule()
- allowInsecureRequest: true for HTTP (Scripting app blocks HTTP by default)
- This was the real reason 'nothing happened' — Scriptable API doesn't exist in Scripting app
This commit is contained in:
Anthony Merlo 2026-08-22 22:51:28 +01:00
parent 067bf7a769
commit ac2d2ad0ff

View file

@ -2,10 +2,12 @@
// Run via Scripting app automation at 9:30am daily
// Server: http://chronos:9432/ingest
//
// Uses Scripting app's Health API:
// Uses Scripting app APIs:
// Health.queryQuantitySamples(type, options) — returns samples
// sample.quantityValue(unit) — extracts numeric value
// HealthUnit / HealthMetricPrefix — unit construction
// fetch() — standard Fetch API (with allowInsecureRequest for HTTP)
// Notification.schedule({ title, body }) — static method
//
// Docs: https://scriptingapp.github.io/guide/Device%20Capabilities/Health/
@ -77,10 +79,10 @@ function todayDate() {
async function notify(body) {
console.log(`notify: ${body}`);
const n = new Notification();
n.title = "Health Sync";
n.body = body;
await n.schedule();
await Notification.schedule({
title: "Health Sync",
body: body
});
}
// --- Read all health metrics ---
@ -162,16 +164,17 @@ if (samples.length === 0) {
await notify("No health data found today");
} else {
const req = new Request(SERVER_URL);
req.method = "POST";
req.headers = {
try {
const response = await fetch(SERVER_URL, {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json"
};
req.body = JSON.stringify({ samples: samples });
try {
const res = await req.loadJSON();
},
body: JSON.stringify({ samples: samples }),
allowInsecureRequest: true
});
const res = await response.json();
console.log(`Server response: ${JSON.stringify(res)}`);
await notify(`Sent ${res.ingested || samples.length} samples`);
} catch (e) {