Add SendMessage and SendMessageToChannel, and the necessary types to go with them
This commit is contained in:
40
chat.go
40
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{
|
||||
|
||||
51
types.go
51
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"`
|
||||
|
||||
Reference in New Issue
Block a user