Real-Time Events and Webhooks
Graph subscriptions need public HTTPS notification URLs. teams listen is a plain HTTP listener bound to 127.0.0.1; put a trusted tunnel or reverse proxy in front of it.
export TEAMS_CLI_CLIENT_STATE="$(openssl rand -hex 16)"
teams listen --port 8080 > notifications.ndjson
teams subscribe create --resource "/chats/$CHAT_ID/messages" --change-type created \
--webhook-url "$HTTPS_URL/webhook" --lifecycle-url "$HTTPS_URL/lifecycle" \
--expiration "$EXPIRES" --client-state "$TEAMS_CLI_CLIENT_STATE"
Start the listener and the tunnel before subscribe create: Graph validates both the webhook URL and the lifecycle URL when it creates the subscription.
The listener
- Binds
127.0.0.1by default. Pass--bind 0.0.0.0only when the proxy runs on another host. Before v0.11.0 it bound every interface.--port 0picks a free port; the startup line names the address bound. - Answers Graph's validation request (
?validationToken=) and accepts notifications on/,/webhookand/lifecycle, so one tunnel serves both URLs. - With
--client-state, orTEAMS_CLI_CLIENT_STATE(which stays out of process listings), notifications whoseclientStateis missing or different are dropped with a warning on stderr. Without it, anyone who can reach the port can inject lines, and the listener warns at startup. - Prints one JSON object per line: every field Graph sent except
clientState, pluskind(lifecyclewhenlifecycleEventis present, otherwisechange).
Responses, for a relay that forwards Graph's posts:
| Status | Meaning | Retry? |
|---|---|---|
| 202 | Queued for stdout, or dropped for a wrong clientState | No |
| 400 | Not a notification collection | No |
| 408 | Body not received within 10 seconds | Yes |
| 413 | Body over 1 MiB | No |
| 503 | Output queue full (the stdout reader is behind), or shutting down | Yes, with backoff |
At most 64 connections are served at once. On Ctrl+C or SIGTERM the listener stops admitting notifications and writes out everything it already answered 202; a second signal abandons the rest, and any acknowledged notification left unwritten is counted on stderr with exit code 1.
Subscription lifetimes
- Teams chat and channel message subscriptions last up to three days; presence up to one hour. Graph raises any expiration under 45 minutes to 45.
- A Teams subscription that lasts more than one hour needs
--lifecycle-url. Without--expiration,subscribe createasks for one hour. - Renew with
teams subscribe renew <subscription-id> --expiration <time>beforeexpirationDateTime.
Lifecycle notifications arrive as kind: lifecycle lines:
lifecycleEvent | Do |
|---|---|
reauthorizationRequired | teams subscribe renew |
subscriptionRemoved | Create the subscription again |
missed | Catch up with teams message list |
Deduplicate notifications by ID, and delete subscriptions you no longer need.