Browse Source

Combine chatIn.go and chatOut.go into just chat.go

main
Sam 5 years ago
parent
commit
370fce9406
  1. 94
      chat.go
  2. 100
      chatOut.go

94
chatIn.go → chat.go

@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"os/exec"
"strings"
"time"
)
@ -99,6 +100,99 @@ func heartbeat(c chan<- ChatAPI, freq time.Duration) { @@ -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: &params{},
}
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: &params{},
}
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: &params{},
}
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: &params{},
}
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
// be fetched at a time. However, if count is passed, then that is the number of
// messages that will be fetched.

100
chatOut.go

@ -1,100 +0,0 @@ @@ -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: &params{},
}
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: &params{},
}
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: &params{},
}
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: &params{},
}
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
}
Loading…
Cancel
Save