# Chat

Canonical page: https://docs.ovok.com/python-sdk/namespaces/chat

`ovok.chat` holds threads between a patient and their care team. A Patient, Practitioner or RelatedPerson session sees the threads about patients it may see, and chat must be on for the project.

## Open a thread and send a message [#open-a-thread-and-send-a-message]

```python
thread = ovok.chat.threads.open({"patient": "Patient/123", "participants": ["Practitioner/456"]})

message = ovok.chat.messages.send(thread["id"], {"text": "How are you feeling today?"})
```

- `open` answers the open thread with the same people when one exists.
- `send` adds a random `clientId` when you send none. A retry with the same `clientId` answers the message already sent, so the SDK retries a send safely.
- `replyTo` names the message you answer.

## Send a file [#send-a-file]

```python
attachment = ovok.chat.attachments.upload(
    file=Path("scan.pdf").read_bytes(), file_name="scan.pdf", content_type="application/pdf"
)
ovok.chat.messages.send(thread["id"], {"text": "My scan", "attachments": [attachment["id"]]})
```

- `upload` sends the file straight to the file store, without your bearer token. It accepts PDF, PNG, JPEG and WebP files.
- `attachments.link` and `messages.attachment_link` answer a `url` that downloads the file without a bearer, until `expiresAt`.

## Read threads and messages [#read-threads-and-messages]

```python
for thread in ovok.chat.threads.list({"patient": "Patient/123"}).auto_paging_iter():
    print(thread["title"], thread["unreadCount"])

ovok.chat.read(thread["id"])
```

- `threads.list` takes `patient`, `updatedSince` and `limit`. `messages.list` takes `since` and `limit` and lists the newest messages first.
- `read` marks the thread read for the signed-in person, now or at `readAt`.
- The TypeScript SDK's live `listen` is not in the Python SDK, because it needs a Socket.IO client. Poll with `updatedSince` and `since` instead.

## See also [#see-also]

- [Push](/python-sdk/namespaces/push)
- [`ChatResource` reference](/python-sdk/reference/resources/chat#chatresource)
