Browse Source

Add ChatList()

main
Sam 5 years ago
parent
commit
db7996185e
  1. 1
      api/api.go
  2. 47
      api/chatOut.go
  3. 11
      main.go

1
api/api.go

@ -12,6 +12,7 @@ type Keybase struct { @@ -12,6 +12,7 @@ type Keybase struct {
type keybase interface {
ChatSend(user, message string) (chatOutResult, error)
ChatSendTeam(team, channel, message string) (chatOutResult, error)
ChatList() (chatOutResult, error)
LoggedIn() bool
Username() string
Version() string

47
api/chatOut.go

@ -5,10 +5,9 @@ import ( @@ -5,10 +5,9 @@ import (
"os/exec"
)
// ---- Struct for sending to API
type chatOut struct {
Method string `json:"method"`
Method string `json:"method"`
Params chatOutParams `json:"params"`
}
type chatOutChannel struct {
@ -26,6 +25,7 @@ type chatOutOptions struct { @@ -26,6 +25,7 @@ type chatOutOptions struct {
type chatOutParams struct {
Options chatOutOptions `json:"options"`
}
// ----
// ---- Struct for data received after sending to API
@ -33,22 +33,40 @@ type chatOutResult struct { @@ -33,22 +33,40 @@ type chatOutResult struct {
Result chatOutResultResult `json:"result"`
}
type chatOutResultRatelimits struct {
Tank string `json:"tank"`
Capacity int `json:"capacity"`
Reset int `json:"reset"`
Gas int `json:"gas"`
Tank string `json:"tank,omitempty"`
Capacity int `json:"capacity,omitempty"`
Reset int `json:"reset,omitempty"`
Gas int `json:"gas,omitempty"`
}
type chatOutResultChannel struct {
Name string `json:"name"`
Public bool `json:"public"`
MembersType string `json:"members_type"`
TopicType string `json:"topic_type,omitempty"`
TopicName string `json:"topic_name,omitempty"`
}
type chatOutResultConversations struct {
ID string `json:"id"`
Channel chatOutResultChannel `json:"channel"`
Unread bool `json:"unread"`
ActiveAt int `json:"active_at"`
ActiveAtMs int64 `json:"active_at_ms"`
MemberStatus string `json:"member_status"`
}
type chatOutResultResult struct {
Message string `json:"message"`
ID int `json:"id"`
Ratelimits []chatOutResultRatelimits `json:"ratelimits"`
Message string `json:"message,omitempty"`
ID int `json:"id,omitempty"`
Ratelimits []chatOutResultRatelimits `json:"ratelimits,omitempty"`
Conversations []chatOutResultConversations `json:"conversations,omitempty"`
Offline bool `json:"offline,omitempty"`
}
// ----
// chatAPIOut() sends JSON requests to the chat API and returns its response.
func chatAPIOut(keybasePath string, c chatOut) (chatOutResult, error) {
jsonBytes, _ := json.Marshal(c)
cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes))
cmdOut, err := cmd.Output()
if err != nil {
@ -82,3 +100,12 @@ func (k Keybase) ChatSendTeam(team, channel, message string) (chatOutResult, err @@ -82,3 +100,12 @@ func (k Keybase) ChatSendTeam(team, channel, message string) (chatOutResult, err
return chatAPIOut(k.path, m)
}
// ChatList() returns a list of all conversations.
func (k Keybase) ChatList() ([]chatOutResultConversations, error) {
m := chatOut{}
m.Method = "list"
r, err := chatAPIOut(k.path, m)
return r.Result.Conversations, err
}

11
main.go

@ -17,7 +17,16 @@ func main() { @@ -17,7 +17,16 @@ func main() {
// Send current client version to self if client is logged in.
if loggedin {
c, _ := k.ChatSend(username, version)
chatList, _ := k.ChatList()
allChats := ""
for _, chat := range chatList {
if chat.Channel.MembersType == "team" {
allChats += fmt.Sprintf("%s#%s\n", chat.Channel.Name, chat.Channel.TopicName)
} else {
allChats += fmt.Sprintf("%s\n", chat.Channel.Name)
}
}
c, _ := k.ChatSend(username, fmt.Sprintf("Version: %s\nConversations:\n```%s```\n", version, allChats))
fmt.Println(c.Result.Message)
} else {
fmt.Println("Not logged in")

Loading…
Cancel
Save