Combine chatIn.go and chatOut.go into just chat.go
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -99,6 +100,99 @@ func heartbeat(c chan<- ChatAPI, freq time.Duration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// chatAPIOut sends JSON requests to the chat API and returns its response.
|
||||||
|
func chatAPIOut(keybasePath string, c ChatAPI) (ChatAPI, error) {
|
||||||
|
jsonBytes, _ := json.Marshal(c)
|
||||||
|
|
||||||
|
cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes))
|
||||||
|
cmdOut, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return ChatAPI{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var r ChatAPI
|
||||||
|
if err := json.Unmarshal(cmdOut, &r); err != nil {
|
||||||
|
return ChatAPI{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send sends a chat message
|
||||||
|
func (c Chat) Send(message ...string) (ChatAPI, error) {
|
||||||
|
m := ChatAPI{
|
||||||
|
Params: ¶ms{},
|
||||||
|
}
|
||||||
|
m.Method = "send"
|
||||||
|
m.Params.Options.Channel = c.Channel
|
||||||
|
m.Params.Options.Message.Body = strings.Join(message, " ")
|
||||||
|
|
||||||
|
r, err := chatAPIOut(c.keybase.Path, m)
|
||||||
|
if err != nil {
|
||||||
|
return ChatAPI{}, err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Edit edits a previously sent chat message
|
||||||
|
func (c Chat) Edit(messageId int, message ...string) (ChatAPI, error) {
|
||||||
|
m := ChatAPI{
|
||||||
|
Params: ¶ms{},
|
||||||
|
}
|
||||||
|
m.Method = "edit"
|
||||||
|
m.Params.Options.Channel = c.Channel
|
||||||
|
m.Params.Options.Message.Body = strings.Join(message, " ")
|
||||||
|
m.Params.Options.MessageID = messageId
|
||||||
|
|
||||||
|
r, err := chatAPIOut(c.keybase.Path, m)
|
||||||
|
if err != nil {
|
||||||
|
return ChatAPI{}, err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// React sends a reaction to a message.
|
||||||
|
func (c Chat) React(messageId int, reaction string) (ChatAPI, error) {
|
||||||
|
m := ChatAPI{
|
||||||
|
Params: ¶ms{},
|
||||||
|
}
|
||||||
|
m.Method = "reaction"
|
||||||
|
m.Params.Options.Channel = c.Channel
|
||||||
|
m.Params.Options.Message.Body = reaction
|
||||||
|
m.Params.Options.MessageID = messageId
|
||||||
|
|
||||||
|
r, err := chatAPIOut(c.keybase.Path, m)
|
||||||
|
if err != nil {
|
||||||
|
return ChatAPI{}, err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete deletes a chat message
|
||||||
|
func (c Chat) Delete(messageId int) (ChatAPI, error) {
|
||||||
|
m := ChatAPI{
|
||||||
|
Params: ¶ms{},
|
||||||
|
}
|
||||||
|
m.Method = "delete"
|
||||||
|
m.Params.Options.Channel = c.Channel
|
||||||
|
m.Params.Options.MessageID = messageId
|
||||||
|
|
||||||
|
r, err := chatAPIOut(c.keybase.Path, m)
|
||||||
|
if err != nil {
|
||||||
|
return ChatAPI{}, err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChatList returns a list of all conversations.
|
||||||
|
func (k *Keybase) ChatList() (ChatAPI, error) {
|
||||||
|
m := ChatAPI{}
|
||||||
|
m.Method = "list"
|
||||||
|
|
||||||
|
r, err := chatAPIOut(k.Path, m)
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
|
||||||
// Read fetches chat messages from a conversation. By default, 10 messages will
|
// Read fetches chat messages from a conversation. By default, 10 messages will
|
||||||
// be fetched at a time. However, if count is passed, then that is the number of
|
// be fetched at a time. However, if count is passed, then that is the number of
|
||||||
// messages that will be fetched.
|
// messages that will be fetched.
|
||||||
100
chatOut.go
100
chatOut.go
@ -1,100 +0,0 @@
|
|||||||
package keybase
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// chatAPIOut sends JSON requests to the chat API and returns its response.
|
|
||||||
func chatAPIOut(keybasePath string, c ChatAPI) (ChatAPI, error) {
|
|
||||||
jsonBytes, _ := json.Marshal(c)
|
|
||||||
|
|
||||||
cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes))
|
|
||||||
cmdOut, err := cmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
return ChatAPI{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var r ChatAPI
|
|
||||||
if err := json.Unmarshal(cmdOut, &r); err != nil {
|
|
||||||
return ChatAPI{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send sends a chat message
|
|
||||||
func (c Chat) Send(message ...string) (ChatAPI, error) {
|
|
||||||
m := ChatAPI{
|
|
||||||
Params: ¶ms{},
|
|
||||||
}
|
|
||||||
m.Method = "send"
|
|
||||||
m.Params.Options.Channel = c.Channel
|
|
||||||
m.Params.Options.Message.Body = strings.Join(message, " ")
|
|
||||||
|
|
||||||
r, err := chatAPIOut(c.keybase.Path, m)
|
|
||||||
if err != nil {
|
|
||||||
return ChatAPI{}, err
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Edit edits a previously sent chat message
|
|
||||||
func (c Chat) Edit(messageId int, message ...string) (ChatAPI, error) {
|
|
||||||
m := ChatAPI{
|
|
||||||
Params: ¶ms{},
|
|
||||||
}
|
|
||||||
m.Method = "edit"
|
|
||||||
m.Params.Options.Channel = c.Channel
|
|
||||||
m.Params.Options.Message.Body = strings.Join(message, " ")
|
|
||||||
m.Params.Options.MessageID = messageId
|
|
||||||
|
|
||||||
r, err := chatAPIOut(c.keybase.Path, m)
|
|
||||||
if err != nil {
|
|
||||||
return ChatAPI{}, err
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// React sends a reaction to a message.
|
|
||||||
func (c Chat) React(messageId int, reaction string) (ChatAPI, error) {
|
|
||||||
m := ChatAPI{
|
|
||||||
Params: ¶ms{},
|
|
||||||
}
|
|
||||||
m.Method = "reaction"
|
|
||||||
m.Params.Options.Channel = c.Channel
|
|
||||||
m.Params.Options.Message.Body = reaction
|
|
||||||
m.Params.Options.MessageID = messageId
|
|
||||||
|
|
||||||
r, err := chatAPIOut(c.keybase.Path, m)
|
|
||||||
if err != nil {
|
|
||||||
return ChatAPI{}, err
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete deletes a chat message
|
|
||||||
func (c Chat) Delete(messageId int) (ChatAPI, error) {
|
|
||||||
m := ChatAPI{
|
|
||||||
Params: ¶ms{},
|
|
||||||
}
|
|
||||||
m.Method = "delete"
|
|
||||||
m.Params.Options.Channel = c.Channel
|
|
||||||
m.Params.Options.MessageID = messageId
|
|
||||||
|
|
||||||
r, err := chatAPIOut(c.keybase.Path, m)
|
|
||||||
if err != nil {
|
|
||||||
return ChatAPI{}, err
|
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ChatList returns a list of all conversations.
|
|
||||||
func (k *Keybase) ChatList() (ChatAPI, error) {
|
|
||||||
m := ChatAPI{}
|
|
||||||
m.Method = "list"
|
|
||||||
|
|
||||||
r, err := chatAPIOut(k.Path, m)
|
|
||||||
return r, err
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user