# ChatResource

Canonical page: https://docs.ovok.com/java-sdk/reference/chat-resource

```java
public final class ChatResource
```

Person-to-person chat between patients and their care team under `/v1/chat`. Get it from [`Ovok.chat()`](/java-sdk/reference/ovok#chat).

## Methods [#methods]

### threads [#threads]

```java
public ChatResource.Threads threads()
```

Returns the signed-in person's threads.

**Returns:** the thread routes

### messages [#messages]

```java
public ChatResource.Messages messages()
```

Returns the messages in a thread.

**Returns:** the message routes

### attachments [#attachments]

```java
public ChatResource.Attachments attachments()
```

Returns the files to send in threads.

**Returns:** the attachment routes

### read [#read]

```java
public void read(
        String threadId,
        Object request,
        RequestOptions options) throws IOException, InterruptedException
```

Marks a thread read by the signed-in person, now or at `readAt` (`POST /v1/chat/threads/{threadId}/read`).

**Parameters:**

- `threadId`: the thread id
- `request`: the optional `readAt` time; `null` marks it read now
- `options`: per-request options, such as the timeout

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

## ChatResource.Threads [#chatresourcethreads]

```java
public static final class Threads
```

The signed-in person's chat threads.

### Threads.open [#threadsopen]

```java
public JsonNode open(
        Object request,
        RequestOptions options) throws IOException, InterruptedException
```

Opens a thread about a patient, or answers the one these participants already have; every participant must be allowed to read the patient (`POST /v1/chat/threads`).

**Parameters:**

- `request`: the `patient`, `participants` and optional `title`
- `options`: per-request options, such as the timeout

**Returns:** the thread

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

### Threads.list [#threadslist]

```java
public Page<JsonNode> list(
        Map<String, ?> params,
        RequestOptions options) throws IOException, InterruptedException
```

Lists the signed-in person's threads, newest message first (`GET /v1/chat/threads`).

**Parameters:**

- `params`: optional `patient`, `updatedSince` and `limit`
- `options`: per-request options, such as the timeout

**Returns:** the first page; iterating it loads the next pages

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

### Threads.retrieve [#threadsretrieve]

```java
public JsonNode retrieve(
        String threadId,
        RequestOptions options) throws IOException, InterruptedException
```

Reads one thread (`GET /v1/chat/threads/{threadId}`).

**Parameters:**

- `threadId`: the thread id
- `options`: per-request options, such as the timeout

**Returns:** the thread

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

## ChatResource.Messages [#chatresourcemessages]

```java
public static final class Messages
```

Messages in a chat thread.

### Messages.list [#messageslist]

```java
public Page<JsonNode> list(
        String threadId,
        Map<String, ?> params,
        RequestOptions options) throws IOException, InterruptedException
```

Lists a thread's messages, newest first; later pages go further back (`GET /v1/chat/threads/{threadId}/messages`).

**Parameters:**

- `threadId`: the thread id
- `params`: optional `since` and `limit`
- `options`: per-request options, such as the timeout

**Returns:** the first page; iterating it loads the next pages

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

### Messages.send [#messagessend]

```java
public JsonNode send(
        String threadId,
        Map<String, ?> message,
        RequestOptions options) throws IOException, InterruptedException
```

Sends a message, with any of the signed-in person's own uploads (`POST /v1/chat/threads/{threadId}/messages`). It carries a client id the server remembers, so a retried send is stored once.

**Parameters:**

- `threadId`: the thread id
- `message`: the `text`, optional `attachments` (upload ids), `replyTo` and `clientId`; a missing `clientId` is generated
- `options`: per-request options, such as the timeout

**Returns:** the message

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

### Messages.attachmentLink [#messagesattachmentlink]

```java
public JsonNode attachmentLink(
        String threadId,
        String messageId,
        String attachmentId,
        RequestOptions options) throws IOException, InterruptedException
```

Returns a link that downloads a file sent with a message, for the next hour (`GET /v1/chat/threads/{threadId}/messages/{messageId}/attachments/{attachmentId}`).

**Parameters:**

- `threadId`: the thread id
- `messageId`: the message id
- `attachmentId`: the attachment id
- `options`: per-request options, such as the timeout

**Returns:** the `url` and `expiresAt`

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response

## ChatResource.Attachments [#chatresourceattachments]

```java
public static final class Attachments
```

Files the signed-in person uploads to send in their chat threads.

### Attachments.upload [#attachmentsupload]

```java
public JsonNode upload(
        byte[] file,
        String fileName,
        String contentType,
        RequestOptions options) throws IOException, InterruptedException
```

Uploads a PDF, PNG, JPEG or WebP of up to 10 MB, kept private (`POST /v1/chat/attachments`), then stores the file where the server says. Send it with `messages().send(threadId, Map.of("text", text, "attachments", List.of(attachmentId)), options)`, in as many threads as you like.

**Parameters:**

- `file`: the file's bytes
- `fileName`: the file name
- `contentType`: `application/pdf`, `image/png`, `image/jpeg` or `image/webp`
- `options`: per-request options, such as the timeout

**Returns:** the attachment's `id`, `title` and `contentType`

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response
- [`OvokServiceException`](/java-sdk/reference/ovok-service-exception): if the store refuses the file, for example because it is larger than 10 MB

### Attachments.link [#attachmentslink]

```java
public JsonNode link(
        String attachmentId,
        RequestOptions options) throws IOException, InterruptedException
```

Returns a link that downloads one of the signed-in person's own uploads for the next hour (`GET /v1/chat/attachments/{attachmentId}`).

**Parameters:**

- `attachmentId`: the attachment id
- `options`: per-request options, such as the timeout

**Returns:** the `url` and `expiresAt`

**Throws:**

- `IOException`: if the request cannot be sent or the response cannot be read
- `InterruptedException`: if the thread is interrupted while waiting for the response
