Developer

Limba: Română
Aspect

Tema

Culoarea de accent

Tema și culoarea te urmează pe satelit.md și în aplicație.

Intră

Unde schimbi tema?

Alege unde se aplică tema nouă.

Poți schimba oricând din antet.

Ghid · Node.js 18+

Alertă de cutremur înaintea undei

Satelit detectează singur cutremurele adânci din Vrancea, în primele secunde. Unda care zguduie are nevoie de circa un minut până la Chișinău. În acest interval webhook-ul earthquake.early ajunge la serverul tău. Redat pe 84 de cutremure reale din 2014 încoace: toate cele de magnitudine 5 și mai mult au fost alertate la 17–24 de secunde de la producere, adică 35–44 de secunde înaintea undei S la Chișinău.

Rulează pe
Node.js 18+
O rulare
0 credite
Pe lună
0 credite
Pornit în
15 min
$ CITY=chisinau MIN_INTENSITY=3 PORT=8080 node quake_alert.mjsEARTHQUAKE ALERT: Vrancea, M 5.5 (estimate, +-0.28). S wave in chisinau in 44 s, estimated intensity IV.

Cum funcționează

  1. Adaugă adresa serverului tău în panou, cu evenimentele earthquake.early și earthquake. Primul cere planul Pro sau mai mare.
  2. Scriptul verifică semnătura din Satelit-Signature și răspunde 200 înainte de orice altceva.
  3. Ia orașul tău din data.moldova și calculează secundele rămase din s_wave_at, nu din momentul trimiterii.
  4. Anunță canalul imediat, apoi încă o dată la stage: 2, cu estimarea mai bună. Cutremurul localizat vine ca earthquake.

Cât costă: Webhook-urile nu consumă credite. earthquake.early e pe planurile Pro, Business și Enterprise; earthquake, după localizare, pe orice plan cu webhook-uri. Planul potrivit: Start nu are webhook-uri.

Cum îl rulezi

bash
export SATELIT_WEBHOOK_SECRET=whsec_...
export TELEGRAM_TOKEN=... TELEGRAM_CHAT=@canalul_tau
CITY=chisinau MIN_INTENSITY=3 PORT=8080 node quake_alert.mjs

Ce scoate

stdout
EARTHQUAKE ALERT: Vrancea, M 5.5 (estimate, +-0.28). S wave in chisinau in 44 s, estimated intensity IV.

Scriptul complet

quake_alert.mjs
#!/usr/bin/env node
// Receives earthquake.early from Satelit (Pro, Business, Enterprise): a deep Vrancea earthquake seen about 20 seconds
// after it starts, tens of seconds before its S wave reaches Moldova. Checks the signature, works out the seconds left
// for your town and tells your channel at once. The located earthquake follows as `earthquake`.
//   SATELIT_WEBHOOK_SECRET=... CITY=chisinau MIN_INTENSITY=3 PORT=8080 node quake_alert.mjs
//   TELEGRAM_TOKEN=... TELEGRAM_CHAT=@your_channel   (without them the alert is printed)
// Put it behind HTTPS and add the address in the dashboard with the events earthquake.early and earthquake.
import { createHmac, timingSafeEqual } from "node:crypto";
import { createServer } from "node:http";

const SECRET = process.env.SATELIT_WEBHOOK_SECRET;
if (!SECRET) throw new Error("Set SATELIT_WEBHOOK_SECRET");
const CITY = process.env.CITY ?? "chisinau";
const MIN_INTENSITY = Number(process.env.MIN_INTENSITY ?? 3);
const { TELEGRAM_TOKEN, TELEGRAM_CHAT } = process.env;
const seen = new Set();

function verified(rawBody, header = "") {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  const t = Number(parts.t);
  if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
  const expected = Buffer.from(createHmac("sha256", SECRET).update(`${t}.${rawBody}`).digest("hex"));
  const given = Buffer.from(parts.v1 ?? "");
  return expected.length === given.length && timingSafeEqual(expected, given);
}

async function tell(text) {
  console.log(text);
  if (!TELEGRAM_TOKEN || !TELEGRAM_CHAT) return;
  // no waiting on Telegram's answer: every second counts, and a failed post must not stop the next alert
  fetch(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ chat_id: TELEGRAM_CHAT, text }),
  }).catch((e) => console.error(`telegram: ${e.message}`));
}

function early(data) {
  const town = data.moldova.find((t) => t.key === CITY) ?? data.strongest;
  if (town.intensity_estimate < MIN_INTENSITY) return;
  // the seconds left now, from the wave's own arrival time: the event took a moment to reach us
  const left = Math.max(0, Math.round((new Date(town.s_wave_at) - Date.now()) / 1000));
  const head = data.stage === 1 ? "EARTHQUAKE ALERT" : "Earthquake alert, updated";
  tell(`${head}: Vrancea, M ${data.magnitude_estimate} (estimate, +-${data.magnitude_sd}). ` +
    `S wave in ${town.key} in ${left} s, estimated intensity ${town.intensity_roman}. Preliminary. ${data.attribution}`);
}

function located(data) {
  tell(`Earthquake located: M ${data.magnitude} ${data.region}, depth ${data.depth_km} km. ` +
    `Estimated in ${data.moldova.strongest_in_name}: ${data.moldova.intensity_roman}. ${data.url}`);
}

const server = createServer((req, res) => {
  const chunks = [];
  req.on("data", (c) => chunks.push(c));
  req.on("end", () => {
    const raw = Buffer.concat(chunks).toString("utf8");
    if (!verified(raw, req.headers["satelit-signature"])) {
      res.writeHead(401).end();
      return;
    }
    const event = JSON.parse(raw);
    res.writeHead(200).end(); // answer first: Satelit does not wait for your work
    if (seen.has(event.id)) return; // a retry of what we already handled
    seen.add(event.id);
    if (event.type === "earthquake.early") early(event.data);
    else if (event.type === "earthquake") located(event.data);
  });
});
server.listen(Number(process.env.PORT ?? 8080), () => console.log(`listening on ${server.address().port}`));

Rutele folosite

Datele din ghid

Actualizat:

Întrebări despre acest ghid

Cât de devreme vine alerta?

La circa 20 de secunde de la producere pentru un cutremur adânc din Vrancea. Unda S ajunge la Cahul după circa 46 de secunde și la Chișinău după 57–66, deci rămân în jur de 24, respectiv 35–44 de secunde.

Cât de sigură e magnitudinea din alertă?

E o estimare din primele secunde ale cutremurului, cu o abatere standard de 0,24–0,28. Peste magnitudinea 6 e un prag minim: primele secunde ale unui cutremur mare nu îi arată toată mărimea. Câmpul preliminary e mereu true.

Ce cutremure nu sunt alertate devreme?

Cele care nu sunt adânci și din Vrancea: un cutremur crustal sau din altă zonă vine doar ca earthquake, după localizare. Nu e un sistem oficial de avertizare.