Ghid · Node.js 18+
Alertă de dronă pentru orașul tău
Satelit urmărește dronele până la 300 km de granița Moldovei și le estimează traseul pe următoarele 30 de minute. Tu spui zonele: un oraș cu raza lui, un punct sau toată țara. Când o dronă e în zonă sau ajunge acolo în minutele alese, webhook-ul drone.zone vine la serverul tău, o singură dată pentru fiecare dronă.
- Rulează pe
- Node.js 18+
- O rulare
- 0 credite
- Pe lună
- 0 credite
- Pornit în
- 15 min
$ PORT=8080 node drone_alert.mjsDRONE ALERT: A drone may reach chisinau (30 km) in about 12 min, near Ocnița, heading S at 180 km/h.Cum funcționează
- Adaugă adresa serverului în panou, cu evenimentele
drone.zone,drone.enteredșidrone.ended, și zonele tale, de exemplu{"city": "chisinau", "radius_km": 30, "lead_min": 15}. - Scriptul verifică semnătura din
Satelit-Signature, răspunde 200 și ignoră o reîncercare cu acelașiid. - La
drone.zone,data.eta_minspune în câte minute ajunge drona; 0 înseamnă că e deja în zonă.data.zonespune care zonă. - Anunță canalul pe loc, cu locul raportat, direcția și viteza.
drone.endedspune când drona a dispărut.
Cât costă: Webhook-urile nu consumă credite. Zonele sunt pe planurile cu webhook-uri: 3 pe Creator, 20 pe Pro, 100 pe Business. Planul Start nu are webhook-uri.
Cum îl rulezi
export SATELIT_WEBHOOK_SECRET=whsec_...
export TELEGRAM_TOKEN=... TELEGRAM_CHAT=@canalul_tau
PORT=8080 node drone_alert.mjsCe scoate
DRONE ALERT: A drone may reach chisinau (30 km) in about 12 min, near Ocnița, heading S at 180 km/h.Scriptul complet
#!/usr/bin/env node
// Receives Satelit's drone watch (any plan with webhooks): drone.zone when a drone is in one of your zones or on its way
// there, drone.entered when one is inside Moldova, drone.ended when it is gone. Checks the signature, tells your channel once
// per drone and stage, and edits nothing else. Your zones are set in the dashboard on the address, for example
// [{"city": "chisinau", "radius_km": 30, "lead_min": 15}, {"lat": 47.76, "lon": 27.93, "radius_km": 20, "lead_min": 10}]
// SATELIT_WEBHOOK_SECRET=... PORT=8080 node drone_alert.mjs
// TELEGRAM_TOKEN=... TELEGRAM_CHAT=@your_channel (without them the alert is printed)
// Put it behind HTTPS and add the address with the events drone.zone, drone.entered and drone.ended.
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 { 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;
fetch(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ chat_id: TELEGRAM_CHAT, text, disable_web_page_preview: true }),
}).catch((e) => console.error(`telegram: ${e.message}`));
}
// the zone in words: a town and its radius, a point, or the whole country
const zoneName = (z) => (z.moldova ? "Moldova" : z.city ? `${z.city} (${z.radius_km} km)` : `${z.lat}, ${z.lon} (${z.radius_km} km)`);
const where = (d) => `near ${d.last_reported?.near ?? "an unknown place"}, heading ${d.heading ?? "?"} at ${d.speed_kmh ?? "?"} km/h`;
const count = (d) => (d.count > 1 ? `${d.count} drones` : "A drone");
function zone(d) {
const is = d.count > 1 ? "are" : "is";
const what = d.eta_min === 0 ? `${is} in ${zoneName(d.zone)}` : `may reach ${zoneName(d.zone)} in about ${d.eta_min} min`;
tell(`DRONE ALERT: ${count(d)} ${what}, ${where(d)}. Estimate, preliminary. ${d.url ?? ""}`.trim());
}
function entered(d) {
const town = d.moldova?.towns?.[0];
tell(`DRONE IN MOLDOVA: ${count(d)} ${where(d)}${town ? `, ${town.km} km from ${town.name}` : ""}. Follow the authorities' instructions. ${d.url ?? ""}`.trim());
}
function ended(d) {
tell(`Drone track ended (${d.status}): ${d.last_reported?.near ?? "last place unknown"}.`);
}
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 === "drone.zone") zone(event.data);
else if (event.type === "drone.entered") entered(event.data);
else if (event.type === "drone.ended") ended(event.data);
});
});
server.listen(Number(process.env.PORT ?? 8080), () => console.log(`listening on ${server.address().port}`));
Rutele folosite
Întrebări despre acest ghid
Cât de exactă e poziția dronei?
E o estimare din rapoarte publice, cu raza de incertitudine în position.accuracy_km. Traseul din ahead e cel probabil, nu sigur. Câmpul preliminary e mereu true.
Pot urmări mai multe orașe?
Da, fiecare adresă are până la 20 de zone: orașe cu rază, puncte cu rază sau {"moldova": true}. Fiecare dronă vine o singură dată pentru fiecare zonă.
E un sistem oficial de alertă?
Nu. Sunt estimările Satelit. Urmează mereu instrucțiunile autorităților; incidentele oficiale vin separat, ca drone.incident.