# Inbox — chat sessions

Whether you can still reply freely.

Providers only allow unprompted messages inside a window from the contact's last
inbound message — 24 hours for WhatsApp. Outside it, a free-form message is
refused and the conversation has to restart from an approved template.

A contact reachable on several channels has **one window per channel**, which is
why these are rows of their own rather than the single `last_seen` the chat
carries.

`inbox.sessions(...)` · see [connection.md](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/inbox/connection.md) first

| Action | Event | Scope |
|---|---|---|
| `sessions` | `chat_session.list` | `contacts:read` |

## The shape

```ts
interface ChatSession {
  id: string;
  channel: string;
  /** Unix seconds of the last inbound message on this channel. */
  last_seen: number;
}
```

## sessions

```ts
const sessions = await inbox.sessions(chatId);
```

`chat_session.list` · scope `contacts:read`

| Field | Type | Required | Notes |
|---|---|---|---|
| `id` | `string` | yes | Contact whose sessions to read. |
| `channel` | `string` | no | Narrow to one channel. Omitted, every channel the contact has used. |

Ordered by `last_seen` descending, so the channel the contact used most recently
comes first. A channel with no row has never had an inbound message — its window
was never open.

## Working out whether a window is open

```ts
const WINDOW = 24 * 60 * 60;  // seconds

const sessions = await inbox.sessions(chatId);
const now = Math.floor(Date.now() / 1000);

const open = sessions.filter((session) => now - session.last_seen < WINDOW);

if (!open.length) {
  // every window has lapsed: send a template, not free text
}
```

The same window drives the chat list's `session` filter, so
`chats.list({ filter: { session: 'active' } })` is the set of chats you can reply
to right now:

```ts
const { contacts } = await inbox.chats.list({
  filter: { session: 'active', assignment: 'me' },
});
```

Note that filter reads the chat's own `last_seen` — the most recent inbound
message across every channel — so it answers "is any window open". Read the
sessions themselves when you need to know *which* channel is still replyable.
