- หน้าแรกสำหรับนักพัฒนา
- ฝังเอดิเตอร์
- เหตุการณ์และ webhook
เหตุการณ์และ webhook
เหตุการณ์และ webhook
นอกจาก callback การบันทึกแล้ว แอปยังรับเหตุการณ์เกี่ยวกับเซสชัน ผู้ใช้ เอกสาร และความคิดเห็นของตนได้ โดยลงนามเหมือน callback การบันทึกทุกประการ (MRO-Signature) ส่งซ้ำตามช่วงเวลาหน่วงเดียวกัน และแสดงรายการพร้อมคำตอบของเซิร์ฟเวอร์คุณในคอนโซลนักพัฒนา (การส่งและเหตุการณ์) สมัครรับได้ในพาเนล Webhooks ของคอนโซล หรือด้วย API ของคอนโซล:
# สมัครรับเหตุการณ์ให้แอป (หรือในคอนโซลนักพัฒนา: Webhooks)
curl -X PATCH https://mroffice.ai/api/v1/embed/apps/$APP_ID \
-H "Content-Type: application/json" -H "X-CSRF-Token: …" --cookie "…" \
-d '{ "webhook_url": "https://your-app.example/mroffice/events",
"webhook_events": ["document.opened", "user.joined", "comment.created", "comment.mention", "document.closed"] }'
# "webhook_events": ["*"] สมัครรับทุกเหตุการณ์ รวมถึงเหตุการณ์ใหม่ ส่วน null รับเฉพาะของ callback การบันทึก
# ส่งตัวอย่างที่ลงนามแล้วของเหตุการณ์ใดก็ได้ เพื่อดูว่า endpoint ของคุณตอบอย่างไร
curl -X POST https://mroffice.ai/api/v1/embed/apps/$APP_ID/callbacks/test -d '{ "event": "comment.mention" }' …| เหตุการณ์ | เมื่อใด |
|---|---|
document.saved, document.save_failed | การบันทึกเขียนเวอร์ชันใหม่ / ล้มเหลว (callback การบันทึก: ส่งไปยัง URL ของ callback เสมอ ไม่ว่าจะสมัครรับหรือไม่) |
session.started, session.ended | เซสชันเริ่มต้น และเซสชันสิ้นสุด (data.reason: closed, idle, expired หรือ revoked) |
user.joined, user.left | เซสชันแรกของผู้ใช้บนเอกสารเริ่มต้น และเซสชันสุดท้ายของผู้ใช้สิ้นสุด |
document.opened, document.closed | เซสชันแรกบนเอกสารที่ยังไม่มีเซสชัน และเซสชันสุดท้ายสิ้นสุด |
document.locked, document.unlocked | มีผู้ที่เขียนเอกสารได้ (ทุกโหมดยกเว้น view) อยู่ในเอกสาร / ไม่อยู่แล้ว — เป็นล็อกแบบ WOPI: อย่าเปลี่ยนแปลงสำเนาของคุณในระหว่างนั้น |
comment.created, comment.updated, comment.resolved, comment.deleted | ตรวจพบเมื่อเอกสารถูกบันทึก (การบันทึกด้วยตนเอง อัตโนมัติ หรือเมื่อปิด) โดยเปรียบเทียบความคิดเห็นในไฟล์ใหม่กับไฟล์ก่อนหน้า: ความคิดเห็นและการตอบกลับของ Word ความคิดเห็นแบบเธรดและแบบเดิมของ Excel ความคิดเห็นของ PowerPoint และโน้ตของ PDF |
revision.accepted, revision.rejected | การเปลี่ยนแปลงที่ติดตาม (Word) หายไปตั้งแต่การบันทึกครั้งก่อน: ถือว่ายอมรับเมื่อผลของการเปลี่ยนแปลงยังคงอยู่ มิฉะนั้นถือว่าปฏิเสธ |
job.succeeded, job.failed | งานของเซิร์ฟเวอร์ (การแปลง การเติมเทมเพลต การเปรียบเทียบ) สิ้นสุด โดย data คืองานพร้อม download_url ใหม่ และยังส่งสำหรับคำขอเดียวที่มี "webhook": true ด้วย |
comment.mention | ทันทีเมื่อมีการโพสต์ความคิดเห็นที่กล่าวถึงผู้ใช้ (ดูผู้ใช้ สถานะการอยู่ในเอกสาร และการกล่าวถึง) |
form.submitted | ผู้ที่กรอกแบบฟอร์ม PDF ส่งแบบฟอร์มแล้ว (เสร็จสิ้นและส่ง): data.role และ data.values |
POST https://your-app.example/mroffice/events
Content-Type: application/json
User-Agent: MROffice-Embed/1
MRO-Signature: t=1795100212,v1=5f1c…e9a2
MRO-Event-Id: 2b7d… (also Idempotency-Key: the same on every retry)
MRO-Event-Type: comment.mention
{
"id": "2b7d…",
"type": "comment.mention",
"created": "2026-11-20T10:00:00+00:00",
"app_id": "7f1d2c3b-…",
"document": { "id": "…", "key": "contract-42", "name": "contract.docx", "format": "docx", "version": 3 },
"session": { "id": "…", "mode": "comment", "effective_mode": "comment" },
"user": { "id": "user-7", "name": "Ada Lovelace", "email": "[email protected]" },
"data": {
"comment": { "thread_id": "w:1A2B3C4D", "id": "w:1A2B3C4D:m1", "text": "@Grace Hopper please check this clause." },
"mentioned": [{ "id": "user-9", "name": "Grace Hopper", "email": "[email protected]" }]
}
}- document คือ { id, key, name, format, version } ส่วน session คือ { id, mode, effective_mode } และ user คือผู้ใช้ของคุณ { id, name, email } (ผู้ที่ทำ) data ของ comment.* คือ { comment: { id, author, author_id, text, resolved, parent_id, date, anchor? }, save_id, version } และ data ของ revision.* คือ { revision: { type, author, author_id, date, text }, save_id, version }
- เหตุการณ์ส่งไปยัง URL ของ webhook ของแอป (หรือ URL ของ callback หากไม่มี) ส่วนเหตุการณ์สองรายการของ callback การบันทึกเองจะส่งไปยัง URL ของ callback เสมอ ให้ตอบด้วย 2xx ใดก็ได้ภายใน 20 วินาที คำตอบอื่นจะถูกส่งซ้ำหลัง 10 วินาที 30 วินาที 2 นาที 10 นาที 30 นาที 1 ชั่วโมง 3 ชั่วโมง และ 6 ชั่วโมง (410 Gone จะหยุด) โดย MRO-Event-Id และ Idempotency-Key คงเดิมในทุกครั้งที่ส่งซ้ำ: ให้จัดการแต่ละเหตุการณ์เพียงครั้งเดียว เหตุการณ์อาจมาถึงไม่เรียงลำดับ: ให้เรียงตาม created
- POST /api/v1/embed/apps/{id}/callbacks/test พร้อม { "event": "<type>" } ส่งตัวอย่างที่ลงนามแล้วของเหตุการณ์นั้นทันที ("test": true) ส่วน GET /api/v1/embed/apps/{id}/webhooks แสดงการสมัครรับ ประเภทเหตุการณ์ทั้งหมด และจำนวนที่ส่งไปแล้วของแต่ละประเภท
import express from "express";
import { verifyWebhook } from "mroffice-embed/server"; // หรือ verifyMROfficeSignature ด้านบน
app.post("/mroffice/events", express.raw({ type: "application/json" }), async (req, res) => {
let event;
try {
event = await verifyWebhook(req.body, req.get("MRO-Signature"), process.env.MROFFICE_WEBHOOK_SECRET);
} catch {
return res.status(401).end(); // ไม่ได้ลงนามโดย MR Office หรือเก่าเกินไป
}
if (await alreadyHandled(event.id)) return res.json({ ok: true }); // การส่งซ้ำใช้ id เดิม
switch (event.type) {
case "comment.mention":
for (const person of event.data.mentioned) await notify(person.id, event.document, event.data.comment.text);
break;
case "document.locked": await files.markEditing(event.document.key, true); break;
case "document.unlocked": await files.markEditing(event.document.key, false); break;
case "comment.created": await activity.add(event.document.key, event.data.comment); break;
}
res.json({ ok: true }); // 2xx ใดก็ได้ภายใน 20 วินาที
});import hashlib, hmac, time
def verify_mroffice_signature(raw_body: bytes, header: str, webhook_secret: str, tolerance: int = 300) -> bool:
pairs = [item.split("=", 1) for item in header.split(",") if "=" in item]
timestamps = [value for key, value in pairs if key.strip() == "t"]
signatures = [value for key, value in pairs if key.strip() == "v1"]
if not timestamps or not timestamps[0].isdigit() or abs(time.time() - int(timestamps[0])) > tolerance:
return False
expected = hmac.new(webhook_secret.encode(), f"{timestamps[0]}.".encode() + raw_body, hashlib.sha256).hexdigest()
return any(hmac.compare_digest(expected, candidate.strip()) for candidate in signatures)# FastAPI; Flask และ Django อ่าน body ดิบด้วยวิธีเดียวกัน
from fastapi import FastAPI, Request, Response
app = FastAPI()
@app.post("/mroffice/events")
async def mroffice_events(request: Request) -> Response:
raw = await request.body() # ไบต์ตามที่ส่งมา ก่อนแยกวิเคราะห์ JSON ใด ๆ
if not verify_mroffice_signature(raw, request.headers.get("MRO-Signature", ""), WEBHOOK_SECRET):
return Response(status_code=401)
event = json.loads(raw)
if already_handled(event["id"]): # การส่งซ้ำใช้ id เดิม
return Response(status_code=200)
if event["type"] == "comment.mention":
for person in event["data"]["mentioned"]:
notify(person["id"], event["document"]["key"], event["data"]["comment"]["text"])
return Response(status_code=200)