HTTP API: a chat's recent messages, and sending a message (closes #5)
check / check (push) Successful in 1m5s
check / check (push) Successful in 1m5s
`GET /api/v1/chats/{id}/messages?count=N` returns a chat's recent messages, oldest first (`count` 1 to 100, default 20), and `POST` to the same path sends a text message and returns it once the chat client has taken it. Both answer `404` for any id the chats list does not show, looked up in the same contact list. The message record is defined once, for the webhooks to reuse.
Disclosures: items are read five at a time, because one item can be many times its text and 100 at once could pass the 16 MiB read limit; text too long gets `413`, a contact who deleted the chat `409` (unverified for one still connecting); a query the server cannot read gets its own `400`.
Model: opus-5-5
This commit was merged in pull request #15.
This commit is contained in:
+14
-1
@@ -1,5 +1,5 @@
|
||||
// Package api is the bot's HTTP API, through which another program
|
||||
// reads the bot's chats.
|
||||
// reads the bot's chats and sends messages in them.
|
||||
//
|
||||
// Every request must carry the credential, as "Authorization: Bearer
|
||||
// {credential}"; with no credential configured, every request is
|
||||
@@ -47,6 +47,17 @@ const (
|
||||
type ChatClient interface {
|
||||
// Contacts returns the contacts of the user userID.
|
||||
Contacts(ctx context.Context, userID int64) ([]simplex.Contact, error)
|
||||
|
||||
// ChatItems returns the last count items of the chat with a
|
||||
// contact, oldest first.
|
||||
ChatItems(
|
||||
ctx context.Context, contactID int64, count int,
|
||||
) ([]simplex.ChatItem, error)
|
||||
|
||||
// SendMessage sends a text message to a contact and returns it.
|
||||
SendMessage(
|
||||
ctx context.Context, contactID int64, text string,
|
||||
) (simplex.ChatItem, error)
|
||||
}
|
||||
|
||||
// Params configures New.
|
||||
@@ -86,6 +97,8 @@ func New(p Params) *http.Server {
|
||||
})
|
||||
router.Route("/api/v1", func(r chi.Router) {
|
||||
r.Get("/chats", h.handleChats())
|
||||
r.Get("/chats/{id}/messages", h.handleMessages())
|
||||
r.Post("/chats/{id}/messages", h.handleSend())
|
||||
})
|
||||
|
||||
return &http.Server{
|
||||
|
||||
Reference in New Issue
Block a user