产品文档

长音频后台任务与 Webhook 签名校验

更新于 2026-12-03

针对超过 30 分钟的会议全程录音、大学公开课、庭审录像、播客节目与口述历史档案,推荐使用长音频后台任务接口(POST /v1/asr/jobs)。单个任务最高支持 2 小时时长、200 MB 文件体积,提交后立即返回 HTTP 202 Accepted 与 jobId,后台自动完成无损声学切分、跨分片时间轴拼合、全局说话人聚类与热词融合。

任务生命周期状态机

MERMAID
stateDiagram-v2
    [*] --> queued : POST /v1/asr/jobs (202)
    queued --> running : 后台工作协程领用
    queued --> cancelled : DELETE /v1/asr/jobs/{jobId}
    running --> succeeded : 转写完成并按秒结算
    running --> failed : 音频拉取或解码异常(不扣费)
    running --> cancelled : DELETE /v1/asr/jobs/{jobId}(不扣费)
    succeeded --> expired : 超过 72 小时保留期
  • 幂等导出:任务进入 succeeded 后 72 小时内,可随时调用 GET /v1/asr/jobs/{jobId}/result?format=... 导出 json、verbose_json、text、srt 或 vtt,多次下载不重复计费。
  • 主动清理:业务系统将转写结果落库后,可主动调用 DELETE /v1/asr/jobs/{jobId} 立即清除云端暂存的转写正文(响应返回 {"id":"job_...","status":"succeeded","deleted":true})。

Webhook 主动回调载荷与安全规范

在提交任务时传入公网 webhookUrl(支持 HTTP/HTTPS,禁止指向内网回环或云元数据地址),任务进入 succeeded 或 failed 终态后,平台会主动向该地址发起一次 POST 请求(Content-Type: application/json,连接与响应超时 10 秒):

回调请求头

请求头示例值说明
Content-Typeapplication/json; charset=utf-8UTF-8 编码 JSON 事件体
User-AgentHeimoriCloud-Webhook/1.0讯蒙智能云官方回调标识
X-Heimori-Eventasr.job.completed成功时为 asr.job.completed,失败时为 asr.job.failed
X-Heimori-Timestamp1764727398回调发出时刻的 Unix 秒级时间戳,用于防重放窗口校验(建议允许偏差 ≤ 300 秒)
X-Heimori-Signaturesha256=5c9a8d2e...仅提交任务时传入 webhookSecret 时附带:以 webhookSecret 为密钥对 <X-Heimori-Timestamp>.<rawBody> 字节流计算 HMAC-SHA256 十六进制摘要

回调 JSON 事件体示例

JSON
{
  "event": "asr.job.completed",
  "job": {
    "id": "job_6d2f91a8c30e4b179a02",
    "status": "succeeded",
    "progress": 100,
    "fileName": "annual-conference.mp3",
    "language": "mw",
    "durationSeconds": 3420,
    "errorCode": null,
    "errorMessage": null,
    "createdAt": "2026-12-03T02:00:00.000Z",
    "finishedAt": "2026-12-03T02:03:18.420Z",
    "expiresAt": "2026-12-06T02:03:18.420Z"
  }
}

Webhook HMAC-SHA256 签名校验示例

接收端须直接读取 HTTP 原始请求字节流(raw body) 与 X-Heimori-Timestamp 请求头,先校验时间戳偏差在 300 秒以内,再拼接 f"{timestamp}.".encode("utf-8") + raw_body 计算 HMAC-SHA256 并使用常量时间比较函数验签;验签通过后按 job.id 做幂等去重并调用 GET /v1/asr/jobs/{jobId}/result 拉取完整转写文本或字幕。

Python(FastAPI / Flask 通用验签)

Python
import hashlib
import hmac
import time

def verify_asr_webhook(
    secret: str,
    timestamp_header: str,
    signature_header: str,
    raw_body: bytes,
) -> bool:
    if not timestamp_header or not timestamp_header.isdigit():
        return False
    if abs(time.time() - int(timestamp_header)) > 300:
        return False
    if not signature_header or not signature_header.startswith("sha256="):
        return False
    received_hex = signature_header.split("=", 1)[1].strip()
    expected_hex = hmac.new(
        secret.encode("utf-8"),
        f"{timestamp_header}.".encode("utf-8") + raw_body,
        hashlib.sha256,
    ).hexdigest()
    return hmac.compare_digest(expected_hex, received_hex)

Node.js(Express / Fastify 通用验签)

JavaScript
import crypto from "node:crypto";

export function verifyAsrWebhook(secret, timestampHeader, signatureHeader, rawBodyBuffer) {
  const ts = Number(timestampHeader);
  if (!Number.isInteger(ts) || Math.abs(Math.floor(Date.now() / 1000) - ts) > 300) {
    return false;
  }
  if (typeof signatureHeader !== "string" || !signatureHeader.startsWith("sha256=")) {
    return false;
  }
  const received = Buffer.from(signatureHeader.slice("sha256=".length).trim(), "hex");
  const expected = crypto
    .createHmac("sha256", secret)
    .update(`${timestampHeader}.`)
    .update(rawBodyBuffer)
    .digest();
  if (received.length !== expected.length) return false;
  return crypto.timingSafeEqual(received, expected);
}