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:
+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