Route Approvals to Slack
Post every waiting tool call to a Slack channel with a link to decide it, and mark the message when the call is decided. The router sends signed exec.* webhooks for your project; a small handler you run turns them into Slack messages. Decisions happen in Erebine, not in Slack.
Prerequisites
- erectl signed in to the project, to create its webhook
subscription with
erectl webhooks --create. - A Slack app with a bot token that has the
chat:writescope, invited to the channel the messages go to. - A public HTTPS endpoint for the handler.
Environment
The sample handler reads three variables:
xoxb-...).
C0123456789.
Step 1: Subscribe to Approval Events
Point a project webhook at the handler and subscribe to the request and its outcomes:
exec.approval_requested: a call is waiting; post a message.exec.approved,exec.rejected,exec.cancelled: the call was decided or cancelled; update the message.exec.approval_timed_out: nobody decided in time; update the message.
erectl webhooks --create \
--url https://slack-bridge.example.com/erebine \
--events exec.approval_requested,exec.approved,exec.rejected,exec.cancelled,exec.approval_timed_out \
--secret "$(openssl rand -hex 32)"
Keep the secret: the handler verifies every delivery with it. Event payloads are listed on Webhook Events.
Step 2: Verify the Signature
Each delivery carries X-Webhook-Timestamp and
X-Webhook-Signature. The signature is
sha256= followed by the hex HMAC-SHA256 of
<timestamp>.<raw body> under the
subscription's secret (see
Signature Verification).
Verify the raw bytes before parsing them, and refuse a timestamp
more than five minutes old.
import hashlib
import hmac
import os
import time
SECRET = os.environ["WEBHOOK_SECRET"].encode()
def verified(body: bytes, timestamp: str, signature: str) -> bool:
if not timestamp.isdigit() or abs(time.time() - int(timestamp)) > 300:
return False
expected = hmac.new(SECRET, timestamp.encode() + b"." + body,
hashlib.sha256).hexdigest()
return hmac.compare_digest("sha256=" + expected, signature)
Step 3: Post to Slack
On exec.approval_requested, post the tool, its risk
and its workspace, with the url from the payload: it
opens the call on the Operations page
(/ops?invocation=<id>), where an owner or
operator decides it. Keep the message's ts under the
invocation id so Step 4 can update it.
import requests
from flask import Flask, abort, request
app = Flask(__name__)
SLACK = "https://slack.com/api/"
HEADERS = {"Authorization": "Bearer " + os.environ["SLACK_BOT_TOKEN"]}
CHANNEL = os.environ["SLACK_CHANNEL"]
posted = {} # invocation id -> Slack message ts; use a real store in production
@app.post("/erebine")
def receive():
if not verified(request.get_data(),
request.headers.get("X-Webhook-Timestamp", ""),
request.headers.get("X-Webhook-Signature", "")):
abort(401)
event = request.get_json()
data = event["data"]
key = data["invocation_id"].lower()
if event["type"] == "exec.approval_requested":
text = (f"`{data['tool_name']}` ({data['risk']}) is waiting in "
f"workspace {data.get('workspace_id', '-')}. "
f"<{data.get('url', '')}|Decide in Erebine>")
reply = requests.post(SLACK + "chat.postMessage", headers=HEADERS,
json={"channel": CHANNEL, "text": text}).json()
if reply.get("ok"):
posted[key] = reply["ts"]
else:
settle(key, event["type"])
return "", 204
The payload names the workspace by id. Map ids to names in the handler if the channel should read names.
Step 4: Settle the Message
On exec.approved, exec.rejected,
exec.cancelled or
exec.approval_timed_out, replace the message with the
outcome so nobody follows a stale link.
OUTCOMES = {
"exec.approved": "Approved",
"exec.rejected": "Rejected",
"exec.cancelled": "Cancelled",
"exec.approval_timed_out": "Timed out",
}
def settle(key: str, event_type: str) -> None:
ts = posted.pop(key, None)
outcome = OUTCOMES.get(event_type)
if ts is None or outcome is None:
return
requests.post(SLACK + "chat.update", headers=HEADERS,
json={"channel": CHANNEL, "ts": ts,
"text": f"{outcome}: tool call {key}"})
Deliveries are at least once and not ordered. Dedupe on the
envelope id, and treat an outcome for a message you
never posted as a no-op.
Troubleshooting
- Every delivery gets
401 - The handler's
WEBHOOK_SECRETis not the subscription's secret, or the body was re-encoded before the HMAC. Sign the raw bytes as received. - Nothing arrives
- The subscription lists other events, or is inactive. Show it with
erectl webhooks <id>and send a test witherectl webhooks <id> --test; list attempts witherectl webhooks <id> --deliveries. - Messages post but never update
- The subscription lacks
exec.approved,exec.rejected,exec.cancelledorexec.approval_timed_out, or the handler lost the storedtson restart.