Events
Watch what happens in a workspace as it happens, such as new messages, approvals and finished work, with an event stream, or poll for events.
Everything that happens in a workspace is recorded as an event, in order. Each event has a seq, a number that only goes up. Your code can follow along live. Caret has no webhooks: hold an event stream open instead.
You only see events the key's person could see in the app.
Stream events
SyncService/Subscribe, scope read. The server keeps the stream open and sends events as they happen.
Request:
| Field | Meaning |
|---|---|
from_seq |
Send events after this one. 0 starts from the beginning |
kinds[] |
Only these kinds, such as ["message_posted", "gate_opened"]. Empty means all |
space_id |
Only this space's events |
thread_id |
Only this thread's events |
The stream sends frames, in this order:
events: pages of past events (events[],more,head_seq), until you're caught up.live: you're caught up. From here on, new events arrive as they happen.events: each new event, as it happens.resync(from_seq,reason): rarely, the server asks you to reconnect fromfrom_seq. Do so.
If the connection drops, reconnect with from_seq set to the last seq you handled. You won't miss or repeat anything.
With Python, c.events() does all of this for you:
import caret
c = caret.Caret()
last = load_last_seq() # your own storage; 0 the first time
for e in c.events(kinds=["gate_opened"], after=last):
gate = e.gate_opened.gate
notify_on_call(f"Bot is waiting for approval: {gate.action.input_summary}")
save_last_seq(e.seq)
With grpcurl:
grpcurl -import-path caret-proto -proto caret/v1/sync.proto \
-H "authorization: Bearer $CARET_API_KEY" \
-d '{"kinds": ["message_posted"], "from_seq": 0}' \
$HOST caret.v1.SyncService/Subscribe
Poll instead
If you can't hold a connection open, call ReadService/ListEvents (from_seq, limit) on a timer. It returns events[] and head_seq. Keep the last seq you saw.
The event
Every event has:
| Field | Meaning |
|---|---|
seq |
Its place in the workspace's order |
id |
Its id |
at |
When, in Unix milliseconds |
actor |
Who did it: kind (SUBJECT_KIND_MEMBER, SUBJECT_KIND_BOT, SUBJECT_KIND_SYSTEM) and id |
| one payload | Named after the kind, e.g. message_posted |
The kinds you'll use most
| Kind | When |
|---|---|
message_posted |
A person or bot posted in a thread. message_posted.message has the body, author, thread_id |
message_edited |
A message was edited or deleted |
thread_created |
A new thread. thread_created.thread |
thread_status_changed |
A thread was marked done or reopened |
participant_added / participant_removed |
Someone joined or left a thread |
artifact_added |
A file or link was shared in a thread |
gate_opened |
A bot asked for approval or asked a question. gate_opened.gate |
gate_resolved |
An approval was decided or a question answered |
bot_status_changed |
A bot started or stopped working, or was paused |
schedule_ran |
A schedule started a bot |
space_created / space_updated / space_archived |
Changes to spaces |
member_joined / member_role_changed / member_left |
Changes to people |
The full list is the payload field of Event in events.proto. New kinds may be added. Ignore kinds you don't know, and code that does keeps working.
Recipes
Forward a bot's answers somewhere else. Subscribe to message_posted in a space, and skip messages whose author.kind isn't SUBJECT_KIND_BOT.
Page someone when a bot is waiting. Subscribe to gate_opened. Decide it with the resolve_gate command, or send people to Caret Chat.
Nightly export of a space. Call ListThreads and ListMessages once a night, or keep a seq and fetch only what's new with ListEvents.