HTTP API: a chat's recent messages, and sending a message (closes #5)
check / check (push) Successful in 1m21s
check / check (push) Successful in 1m21s
GET /api/v1/chats/{id}/messages returns the messages among a chat's
last count items (default 20, at most 100), oldest first. POST sends a
text, waits for the chat client's answer and returns 201 with the
message as sent. A chat the bot does not have is 404, a contact who
deleted the chat is 409, a text too long for one message is 413, and
any other failure is 500 with a chosen sentence.
The chat client spells out a message's formatting in its answer, up to
26 times the text's length, so 100 items in one answer can pass the
16 MiB read limit and end the connection. Items are read five at a
time.
Model: opus-5-5
This commit is contained in:
@@ -89,7 +89,7 @@ variables that are absent.
|
||||
## API
|
||||
|
||||
An HTTP API beside the chat client lets another program read the bot's
|
||||
chats. It speaks JSON on `PORT`.
|
||||
chats and send messages in them. It speaks JSON on `PORT`.
|
||||
|
||||
**Authentication.** Every request carries the credential from
|
||||
`API_TOKEN_FILE`:
|
||||
@@ -133,6 +133,88 @@ curl -H "Authorization: Bearer $TOKEN" http://127.0.0.1:8080/api/v1/chats
|
||||
with the bot. The chat stays in the list, but nothing more reaches
|
||||
them.
|
||||
|
||||
### `GET /api/v1/chats/{id}/messages`
|
||||
|
||||
The latest messages in the chat `id`, oldest first. `count`, a whole
|
||||
number from 1 to 100 and 20 if absent, is how many of the chat's latest
|
||||
items to read. The chat client also records events in a chat, such as
|
||||
the contact connecting, and only messages are returned, so fewer than
|
||||
`count` can come back.
|
||||
|
||||
```sh
|
||||
curl -H "Authorization: Bearer $TOKEN" \
|
||||
'http://127.0.0.1:8080/api/v1/chats/3/messages?count=5'
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"id": 9,
|
||||
"direction": "received",
|
||||
"type": "text",
|
||||
"text": "2 + 2",
|
||||
"time": "2026-09-29T03:14:34Z"
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"direction": "sent",
|
||||
"type": "text",
|
||||
"text": "4",
|
||||
"time": "2026-09-29T03:14:35.101223457Z"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
A message has:
|
||||
|
||||
- `id` — its number, unique across all the bot's chats.
|
||||
- `direction` — `received` from the contact, or `sent` by the bot.
|
||||
- `type` — `text`, or another kind of SimpleX message, such as `image`,
|
||||
`file` or `voice`.
|
||||
- `text` — the text; for a message that is not `text`, its caption,
|
||||
which can be empty.
|
||||
- `time` — in RFC 3339, in UTC: for a received message, when it reached
|
||||
the SimpleX relay, to the second; for a sent one, when the bot sent
|
||||
it.
|
||||
|
||||
A `count` other than a whole number from 1 to 100 gets `400`, and an
|
||||
`id` that is not one of the bot's chats gets `404`.
|
||||
|
||||
### `POST /api/v1/chats/{id}/messages`
|
||||
|
||||
Sends the `text` in the body to the chat `id`, and answers `201` with
|
||||
the message as sent. The answer comes once the chat client has taken the
|
||||
message, before it reaches the contact.
|
||||
|
||||
```sh
|
||||
curl -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"text":"hello"}' http://127.0.0.1:8080/api/v1/chats/3/messages
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"message": {
|
||||
"id": 12,
|
||||
"direction": "sent",
|
||||
"type": "text",
|
||||
"text": "hello",
|
||||
"time": "2026-09-29T03:14:43.519552587Z"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Nothing is sent, and the answer is:
|
||||
|
||||
- `400` if the body is not JSON of that shape, or `text` is empty;
|
||||
- `404` if `id` is not one of the bot's chats;
|
||||
- `409` if the contact cannot receive messages: they have deleted their
|
||||
chat with the bot (`contact_deleted` is `true`), or have not finished
|
||||
connecting;
|
||||
- `413` if the body is over 64 KiB, or the text is too long for one
|
||||
SimpleX message, which holds about 15,000 bytes.
|
||||
|
||||
## Entrypoints
|
||||
|
||||
This repo adheres to the
|
||||
|
||||
Reference in New Issue
Block a user