- 開発者ホーム
- エディターの埋め込み
- イベントと Webhook
イベントと Webhook
イベントと Webhook
保存コールバックのほかに、アプリはセッション、ユーザー、文書、コメントに関するイベントを受け取れます。署名は保存コールバックとまったく同じ(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 は保存コールバックのイベントだけに登録します。
# 任意のイベントの署名付きサンプルを送り、エンドポイントの応答を確認します
curl -X POST https://mroffice.ai/api/v1/embed/apps/$APP_ID/callbacks/test -d '{ "event": "comment.mention" }' …| イベント | タイミング |
|---|---|
document.saved, document.save_failed | 保存で新しいバージョンが書き込まれた / 保存に失敗した(保存コールバック: 購読の有無にかかわらず、常にコールバック URL に送信)。 |
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 } です。comment.* の data: { comment: { id, author, author_id, text, resolved, parent_id, date, anchor? }, save_id, version }。revision.* の data: { revision: { type, author, author_id, date, text }, save_id, version }。
- イベントはアプリの Webhook URL(ない場合はコールバック URL)に送られます。保存コールバック自身の 2 つのイベントは常にコールバック URL に送られます。20 秒以内に任意の 2xx を返してください。それ以外の応答は、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 }); // 20 秒以内に任意の 2xx
});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 でも同じ方法で生のボディを読みます。
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)