Referenz

Beispiele

Fertige Bausteine für Python, Node.js, einen Webhook-Empfänger und die Shell.

Als Markdown öffnen

Fertige Bausteine zum Kopieren. Alle lesen den Schlüssel aus der Umgebungsvariable SAMY_KEY.

Python

Python
import os, requests

SAMY_KEY = os.environ["SAMY_KEY"]

def samy(text, title=None, url=None, buttons=None, priority="normal"):
    res = requests.post(
        "https://api-production-e537a.up.railway.app/v1/push",
        headers={"Authorization": f"Bearer {SAMY_KEY}"},
        json={"title": title, "text": text, "url": url,
              "buttons": buttons or [], "priority": priority},
        timeout=10,
    )
    res.raise_for_status()
    return res.json()["message_id"]

samy("2× Kaffeebecher, 89,00 €", title="Neue Bestellung #1042",
     buttons=[{"id": "label", "label": "Versandlabel drucken"}])

Node.js

Node.js
export async function samy(message) {
  const res = await fetch("https://api-production-e537a.up.railway.app/v1/push", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.SAMY_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(message),
  });
  if (!res.ok) throw new Error(`Samy antwortet mit ${res.status}: ${await res.text()}`);
  return (await res.json()).message_id;
}

await samy({ title: "Deploy fehlgeschlagen", text: "Build 318 ist abgebrochen.", priority: "high" });

Webhook-Empfänger (Python, Flask)

Reagiert auf einen Aktions-Knopf und prüft die Signatur.

Python
import hashlib, hmac, os
from flask import Flask, request

app = Flask(__name__)
SECRET = hashlib.sha256(os.environ["SAMY_KEY"].encode()).hexdigest().encode()

@app.post("/samy")
def samy_webhook():
    expected = "sha256=" + hmac.new(SECRET, request.get_data(), hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("X-Samy-Signature", "")):
        return "", 401
    event = request.get_json()
    if event["button_id"] == "label":
        print("Versandlabel drucken für Nachricht", event["message_id"])
    return "", 204

Shell

Shell
curl https://api-production-e537a.up.railway.app/v1/push \
  -H "Authorization: Bearer $SAMY_KEY" \
  -H "Content-Type: application/json" \
  -d '{"title":"Backup","text":"Backup von heute Nacht ist fertig.","priority":"silent"}'