Browse Source

Add ChatList()

main
Sam 6 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 {
type keybase interface { type keybase interface {
ChatSend(user, message string) (chatOutResult, error) ChatSend(user, message string) (chatOutResult, error)
ChatSendTeam(team, channel, message string) (chatOutResult, error) ChatSendTeam(team, channel, message string) (chatOutResult, error)
ChatList() (chatOutResult, error)
LoggedIn() bool LoggedIn() bool
Username() string Username() string
Version() string Version() string

47
api/chatOut.go

@ -5,10 +5,9 @@ import (
"os/exec" "os/exec"
) )
// ---- Struct for sending to API // ---- Struct for sending to API
type chatOut struct { type chatOut struct {
Method string `json:"method"` Method string `json:"method"`
Params chatOutParams `json:"params"` Params chatOutParams `json:"params"`
} }
type chatOutChannel struct { type chatOutChannel struct {
@ -26,6 +25,7 @@ type chatOutOptions struct {
type chatOutParams struct { type chatOutParams struct {
Options chatOutOptions `json:"options"` Options chatOutOptions `json:"options"`
} }
// ---- // ----
// ---- Struct for data received after sending to API // ---- Struct for data received after sending to API
@ -33,22 +33,40 @@ type chatOutResult struct {
Result chatOutResultResult `json:"result"` Result chatOutResultResult `json:"result"`
} }
type chatOutResultRatelimits struct { type chatOutResultRatelimits struct {
Tank string `json:"tank"` Tank string `json:"tank,omitempty"`
Capacity int `json:"capacity"` Capacity int `json:"capacity,omitempty"`
Reset int `json:"reset"` Reset int `json:"reset,omitempty"`
Gas int `json:"gas"` 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 { type chatOutResultResult struct {
Message string `json:"message"` Message string `json:"message,omitempty"`
ID int `json:"id"` ID int `json:"id,omitempty"`
Ratelimits []chatOutResultRatelimits `json:"ratelimits"` 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. // chatAPIOut() sends JSON requests to the chat API and returns its response.
func chatAPIOut(keybasePath string, c chatOut) (chatOutResult, error) { func chatAPIOut(keybasePath string, c chatOut) (chatOutResult, error) {
jsonBytes, _ := json.Marshal(c) jsonBytes, _ := json.Marshal(c)
cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes)) cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes))
cmdOut, err := cmd.Output() cmdOut, err := cmd.Output()
if err != nil { if err != nil {
@ -82,3 +100,12 @@ func (k Keybase) ChatSendTeam(team, channel, message string) (chatOutResult, err
return chatAPIOut(k.path, m) 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() {
// Send current client version to self if client is logged in. // Send current client version to self if client is logged in.
if loggedin { 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) fmt.Println(c.Result.Message)
} else { } else {
fmt.Println("Not logged in") fmt.Println("Not logged in")

Loading…
Cancel
Save