Webhooki
Zapisz jeden URL w panelu, a wywołamy go, gdy zmienią się wykupione dane. Powiadomienie nie zawiera danych: gdy przyjdzie, wywołaj API.
Konfiguracja
- W panelu, na stronie klucza: URL, zdarzenia i Zapisz. Sekret do podpisu (
whsec_…) pokazujemy tylko raz. - URL musi używać
https, portu 443 lub 8443 i publicznej nazwy hosta albo adresu. - Wyślij test dostarcza powiadomienie
webhook.test, aby sprawdzić Twój endpoint.
Zdarzenia
| Zdarzenie | Kiedy | Plan |
|---|---|---|
market.world.updated | Zmienił się Market świata z Twojego planu; jedno na świat. | market |
coins.updated | Zmienili się sprzedawcy coinsów albo oferty P2P. | Każdy plan |
npc_profit.updated | Zmienił się NPC Profit. | npc-profit |
webhook.test | Kliknięto Wyślij test w panelu. | Zawsze |
Sam wybierasz zdarzenia; dostarczamy tylko te z aktywnych modułów.
Żądanie
POST /tgm HTTP/1.1
Host: hooks.example.com
Content-Type: application/json
User-Agent: TGM-Webhooks/1
X-TGM-Signature: t=1763380800,v1=<64 hex>
{"id":"3f0c2d9e-8a51-4c47-9b3e-1f2a6d7c8e90","type":"market.world.updated","created_at":"2026-11-17T12:00:00.000Z","data":{"world":"Antica","data_version":"efd2cda81ebbb612","etag":"\"efd2cda81ebbb612\""}}idjest unikalne dla powiadomienia i nie zmienia się przy ponowieniach: użyj go, aby pomijać powtórki.datamówi, co się zmieniło:world(jeśli dotyczy),data_versioni nowyetag. Pobierz dane z API jak zwykle; kosztują jednostki jak zwykle.- Żądania przychodzą z naszej usługi dostarczania z
User-Agent: TGM-Webhooks/1. Nie ma stałej listy adresów: ufaj podpisowi, nigdy adresowi IP.
Sprawdzanie podpisu
Każde żądanie ma nagłówek X-TGM-Signature: t=<unix>,v1=<hex>. v1 to HMAC-SHA256, szesnastkowo, z "<t>.<body>" z Twoim sekretem jako kluczem.
- Użyj treści dokładnie w takiej postaci, w jakiej przyszła, przed odczytaniem JSON.
- Porównuj w stałym czasie.
- Odrzuć powiadomienie, którego
tróżni się od Twojego zegara o ponad 5 minut. - Każda próba jest podpisywana od nowa, z nowym
t.
#!/usr/bin/env bash
# Checks one Tibia Global Market webhook notice (needs openssl).
# TGM_WEBHOOK_SECRET=whsec_... bash webhook-verify.sh "<X-TGM-Signature>" < body.json
# The body must be the raw bytes you received, before any JSON parsing.
set -euo pipefail
header="${1:-}"
if [ -z "${TGM_WEBHOOK_SECRET:-}" ] || [ -z "$header" ]; then
echo "Set TGM_WEBHOOK_SECRET and pass the X-TGM-Signature header as the first argument." >&2
exit 2
fi
t=""
sigs=()
IFS=',' read -ra parts <<< "$header"
for p in "${parts[@]}"; do
p="${p// /}"
case "$p" in
t=*) t="${p#t=}" ;;
v1=*) sigs+=("${p#v1=}") ;;
esac
done
if ! [[ "$t" =~ ^[0-9]+$ ]] || [ "${#sigs[@]}" -eq 0 ]; then
echo "rejected: malformed signature header" >&2
exit 1
fi
# HMAC-SHA256 over "<t>.<raw body>", straight from stdin (no trailing newline added).
expected=$( { printf '%s.' "$t"; cat; } | openssl dgst -sha256 -hmac "$TGM_WEBHOOK_SECRET" | awk '{print $NF}')
# Constant-time comparison: compare HMACs of both values under a random key.
nonce=$(openssl rand -hex 16)
mac() { printf '%s' "$1" | openssl dgst -sha256 -hmac "$nonce" | awk '{print $NF}'; }
want=$(mac "$expected")
match=0
for s in "${sigs[@]}"; do
if [ "$(mac "$s")" = "$want" ]; then match=1; fi
done
if [ "$match" -ne 1 ]; then
echo "rejected: bad signature" >&2
exit 1
fi
now=$(date +%s)
age=$(( now > t ? now - t : t - now ))
if [ "$age" -gt $(( 5 * 60 )) ]; then
echo "rejected: timestamp too old" >&2
exit 1
fi
echo "ok"
// Checks one Tibia Global Market webhook notice.
// TGM_WEBHOOK_SECRET=whsec_... node webhook-verify.mjs "<X-TGM-Signature>" < body.json
// The body must be the raw bytes you received, before any JSON parsing.
import { createHmac, timingSafeEqual } from 'node:crypto';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const TOLERANCE_S = 5 * 60;
/** true when the header signs this exact body with this secret, at most 5 minutes ago. */
export function verify(secret, header, rawBody, nowS = Math.floor(Date.now() / 1000)) {
const parts = String(header).split(',').map((p) => p.trim().split('='));
const t = parts.find(([k]) => k === 't')?.[1];
const sigs = parts.filter(([k]) => k === 'v1').map(([, v]) => v);
if (!t || !/^\d+$/.test(t) || sigs.length === 0) return { ok: false, why: 'malformed signature header' };
const expected = createHmac('sha256', secret).update(`${t}.`).update(rawBody).digest();
const match = sigs.some((hex) => {
const got = Buffer.from(hex, 'hex');
return got.length === expected.length && timingSafeEqual(got, expected);
});
if (!match) return { ok: false, why: 'bad signature' };
if (Math.abs(nowS - Number(t)) > TOLERANCE_S) return { ok: false, why: 'timestamp too old' };
return { ok: true };
}
// Command line: the same check on stdin.
if (process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1])) {
const secret = process.env.TGM_WEBHOOK_SECRET;
const header = process.argv[2];
if (!secret || !header) {
console.error('Set TGM_WEBHOOK_SECRET and pass the X-TGM-Signature header as the first argument.');
process.exit(2);
}
const chunks = [];
for await (const c of process.stdin) chunks.push(c);
const r = verify(secret, header, Buffer.concat(chunks));
if (!r.ok) {
console.error(`rejected: ${r.why}`);
process.exit(1);
}
console.log('ok');
}
# Checks one Tibia Global Market webhook notice.
# TGM_WEBHOOK_SECRET=whsec_... python webhook_verify.py "<X-TGM-Signature>" < body.json
# The body must be the raw bytes you received, before any JSON parsing.
import hashlib
import hmac
import os
import sys
import time
TOLERANCE_S = 5 * 60
def verify(secret, header, raw_body, now_s=None):
"""(True, "") when the header signs this exact body with this secret, at most 5 minutes ago."""
now_s = int(time.time()) if now_s is None else now_s
parts = [p.strip().split("=", 1) for p in header.split(",")]
t = next((p[1] for p in parts if len(p) == 2 and p[0] == "t"), "")
sigs = [p[1] for p in parts if len(p) == 2 and p[0] == "v1"]
if not t.isdigit() or not sigs:
return False, "malformed signature header"
expected = hmac.new(secret.encode(), t.encode() + b"." + raw_body, hashlib.sha256).hexdigest()
if not any(hmac.compare_digest(expected.encode(), s.encode()) for s in sigs):
return False, "bad signature"
if abs(now_s - int(t)) > TOLERANCE_S:
return False, "timestamp too old"
return True, ""
if __name__ == "__main__":
secret = os.environ.get("TGM_WEBHOOK_SECRET", "")
if not secret or len(sys.argv) < 2:
print("Set TGM_WEBHOOK_SECRET and pass the X-TGM-Signature header as the first argument.", file=sys.stderr)
sys.exit(2)
ok, why = verify(secret, sys.argv[1], sys.stdin.buffer.read())
if not ok:
print("rejected: " + why, file=sys.stderr)
sys.exit(1)
print("ok")
Skrypty czytają sekret z TGM_WEBHOOK_SECRET, nagłówek z pierwszego argumentu, a treść ze standardowego wejścia, i wypisują ok albo powód.
Odpowiedź
Odpowiedz 2xx w ciągu 10 sekund, a pracę wykonaj potem. Przekierowań (3xx) nie śledzimy; liczą się jako błąd.
Ponowienia
Każda inna odpowiedź albo jej brak w 10 sekund to błąd. Ponawiamy to samo powiadomienie po 1 min, 5 min, 30 min, 2 h i 6 h, a potem rezygnujemy.
Limity
| Limit | Wartość |
|---|---|
| Adresy URL na konto | 1 |
market.world.updated na świat | 1 na 5 minut |
| Powiadomienia dziennie | 2000 |
Testy dziennie (webhook.test) | 5 |
Panel pokazuje 20 ostatnich dostarczeń wraz z ich stanem.