Skip to main content

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.1 by default. Pass --bind 0.0.0.0 only when the proxy runs on another host. Before v0.11.0 it bound every interface. --port 0 picks a free port; the startup line names the address bound.
  • Answers Graph's validation request (?validationToken=) and accepts notifications on /, /webhook and /lifecycle, so one tunnel serves both URLs.
  • With --client-state, or TEAMS_CLI_CLIENT_STATE (which stays out of process listings), notifications whose clientState is 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, plus kind (lifecycle when lifecycleEvent is present, otherwise change).

Responses, for a relay that forwards Graph's posts:

StatusMeaningRetry?
202Queued for stdout, or dropped for a wrong clientStateNo
400Not a notification collectionNo
408Body not received within 10 secondsYes
413Body over 1 MiBNo
503Output queue full (the stdout reader is behind), or shutting downYes, 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 create asks for one hour.
  • Renew with teams subscribe renew <subscription-id> --expiration <time> before expirationDateTime.

Lifecycle notifications arrive as kind: lifecycle lines:

lifecycleEventDo
reauthorizationRequiredteams subscribe renew
subscriptionRemovedCreate the subscription again
missedCatch up with teams message list

Deduplicate notifications by ID, and delete subscriptions you no longer need.