Postia API
ZH
控制台

Webhook

有事件发生时,Postia 会调用你的 HTTPS 端点。可在控制台或通过 API 添加端点;所需 scope 为 webhooks:manage。

事件

事件触发时机
post.published帖子已发布到一个渠道。每个渠道一条事件。
post.failed帖子无法发送到某个渠道。
channel.disconnected某个渠道失去授权,需要重新连接。
ping你请求了一次测试投递。

使用 API 密钥创建的端点会收到其所有者的帖子和渠道事件。由 OAuth 应用创建的端点会收到已授权该应用的用户的事件,范围限于用户授予的 scope 所对应的事件。

{
  "id": "evt_0a1b2c3d4e5f60718293a4b5",
  "type": "post.published",
  "created_at": "2026-12-24T09:00:03Z",
  "data": {
    "post_id": "0191c1a4-8e2b-7c3d-9a10-5b7c2f1d3e44",
    "channel_id": "0191c1a4-1111-7c3d-9a10-5b7c2f1d3e44",
    "status": "published",
    "url": "https://t.me/example/42"
  }
}

投递

验证签名

Postia-Signature 的格式为 t=<unix seconds>,v1=<hex>。v1 的值是 <t>.<raw body> 的 HMAC-SHA256,密钥为端点的 whsec_... secret。请使用原始字节,以恒定时间比较,并拒绝与本机时钟相差超过 5 分钟的时间戳。该 secret 仅在创建端点或轮换 secret 时显示一次。

import { createHmac, timingSafeEqual } from "node:crypto";

// rawBody is the Buffer you received, before any JSON parsing.
export function verify(rawBody, header, secret) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  if (Math.abs(Date.now() / 1000 - Number(parts.t)) > 300) return false;
  const expected = createHmac("sha256", secret)
    .update(`${parts.t}.`).update(rawBody).digest("hex");
  const a = Buffer.from(expected), b = Buffer.from(parts.v1 || "");
  return a.length === b.length && timingSafeEqual(a, b);
}
import hashlib, hmac, time

def verify(raw_body: bytes, header: str, secret: str) -> bool:
    parts = dict(p.split("=", 1) for p in header.split(","))
    if abs(time.time() - int(parts["t"])) > 300:
        return False
    signed = parts["t"].encode() + b"." + raw_body
    expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, parts.get("v1", ""))