From 4e2656445cb3bb5847f1911642c627b47f7ad876 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 16 Feb 2020 13:38:20 -0500 Subject: [PATCH] Add SendMessage and SendMessageToChannel, and the necessary types to go with them --- chat.go | 40 ++++++++++++++++++++++++++++++++++++++++ types.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/chat.go b/chat.go index 76f7b06..088b040 100644 --- a/chat.go +++ b/chat.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "encoding/json" "errors" + "fmt" "os/exec" "strings" "time" @@ -167,6 +168,45 @@ func chatAPIOut(k *Keybase, c ChatAPI) (ChatAPI, error) { return r, nil } +// SendMessage sends a chat message +func (k *Keybase) SendMessage(options SendMessageOptions) (SendResponse, error) { + var r SendResponse + + arg := newSendMessageArg(options) + jsonBytes, _ := json.Marshal(arg) + + cmdOut, err := k.Exec("chat", "api", "-m", string(jsonBytes)) + if err != nil { + return r, err + } + + err = json.Unmarshal(cmdOut, &r) + if err != nil { + return r, err + } + + return r, nil +} + +// SendMessageToChannel sends a chat message to a channel +func (k *Keybase) SendMessageToChannel(channel chat1.ChatChannel, message string, a ...interface{}) (SendResponse, error) { + var r SendResponse + + opts := SendMessageOptions{ + Channel: channel, + Message: SendMessageBody{ + Body: fmt.Sprintf(message, a...), + }, + } + + r, err := k.SendMessage(opts) + if err != nil { + return r, err + } + + return r, nil +} + // Send sends a chat message func (c Chat) Send(message ...string) (ChatAPI, error) { m := ChatAPI{ diff --git a/types.go b/types.go index eeef0b0..3a8f792 100644 --- a/types.go +++ b/types.go @@ -21,6 +21,52 @@ type RunOptions struct { FilterChannels []chat1.ChatChannel // Only subscribe to messages from specified channels } +// Error holds an error message returned by the API +type Error struct { + Code int `json:"code"` + Message string `json:"message"` +} + +type SendMessageBody struct { + Body string +} + +// SendMessageOptions holds a set of options to be passed to SendMessage +type SendMessageOptions struct { + Channel chat1.ChatChannel `json:"channel,omitempty"` + ConversationID chat1.ConvIDStr `json:"conversation_id,omitempty"` + Message SendMessageBody `json:",omitempty"` + Filename string `json:"filename,omitempty"` + Title string `json:"title,omitempty"` + MessageID chat1.MessageID `json:"message_id,omitempty"` + ConfirmLumenSend bool `json:"confirm_lumen_send"` + ReplyTo *chat1.MessageID `json:"reply_to,omitempty"` +} + +type sendMessageParams struct { + Options SendMessageOptions +} + +type sendMessageArg struct { + Method string + Params sendMessageParams +} + +func newSendMessageArg(options SendMessageOptions) sendMessageArg { + return sendMessageArg{ + Method: "send", + Params: sendMessageParams{ + Options: options, + }, + } +} + +// SendResponse holds the data returned by the send method in the API +type SendResponse struct { + Result chat1.SendRes `json:"result"` + Error *Error `json:"error,omitempty"` +} + // ChatAPI holds information about a message received by the `keybase chat api-listen` command type ChatAPI struct { Type string `json:"type,omitempty"` @@ -595,11 +641,6 @@ type tParams struct { Options tOptions `json:"options"` } -type Error struct { - Code int `json:"code"` - Message string `json:"message"` -} - type tResult struct { ChatSent bool `json:"chatSent"` CreatorAdded bool `json:"creatorAdded"`