From 133d958029cd7e2c9656987a2d9f446606b0e2e1 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 23 Oct 2022 06:27:15 +0000 Subject: [PATCH] Get to a permission error while looking up IMs --- slack2asana.go | 117 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/slack2asana.go b/slack2asana.go index 2d23042..e2cd50a 100644 --- a/slack2asana.go +++ b/slack2asana.go @@ -5,15 +5,36 @@ import ( "errors" "fmt" "net/http" + "net/url" "os" ) +type ChannelResponse struct { + Ok bool `json:"ok"` + Error string `json:"error"` + Channel *Channel `json:"channel"` +} + type StarsResponse struct { Ok bool `json:"ok"` Error string `json:"error"` Items []*Item `json:"items"` } +type UserResponse struct { + Ok bool `json:"ok"` + Error string `json:"error"` + User *User `json:"user"` +} + +type Channel struct { + Id string `json:"id"` + Name string `json:"name"` + IsChannel bool `json:"is_channel"` + IsGroup bool `json:"is_group"` + IsIm bool `json:"is_im"` +} + type Item struct { Type string `json:"type"` Channel string `json:"channel"` @@ -28,6 +49,11 @@ type Message struct { Permalink string `json:"permalink"` } +type User struct { + Id string `json:"id"` + Name string `json:"name"` +} + func main() { c := &http.Client{} @@ -41,7 +67,22 @@ func main() { continue } - fmt.Printf("%#v\n", item.Message) + fmt.Printf("%#v\n", item) + fmt.Printf("\t%#v\n", item.Message) + + user, err := getUser(c, item.Message.User) + if err != nil { + panic(err) + } + + fmt.Printf("\t%#v\n", user) + + channel, err := getChannel(c, item.Channel) + if err != nil { + panic(err) + } + + fmt.Printf("\t%#v\n", channel) } } @@ -73,6 +114,80 @@ func getStars(c *http.Client) ([]*Item, error) { return stars.Items, nil } +func getUser(c *http.Client, id string) (*User, error) { + u, err := url.Parse("https://slack.com/api/users.info") + if err != nil { + return nil, err + } + + v := &url.Values{} + v.Add("user", id) + u.RawQuery = v.Encode() + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return nil, err + } + + addAuth(req) + + resp, err := c.Do(req) + if err != nil { + return nil, err + } + + dec := json.NewDecoder(resp.Body) + user := &UserResponse{} + + err = dec.Decode(user) + if err != nil { + return nil, err + } + + if !user.Ok { + return nil, errors.New(user.Error) + } + + return user.User, nil +} + +func getChannel(c *http.Client, id string) (*Channel, error) { + u, err := url.Parse("https://slack.com/api/conversations.info") + if err != nil { + return nil, err + } + + v := &url.Values{} + v.Add("channel", id) + u.RawQuery = v.Encode() + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return nil, err + } + + addAuth(req) + + resp, err := c.Do(req) + if err != nil { + return nil, err + } + + dec := json.NewDecoder(resp.Body) + channel := &ChannelResponse{} + + err = dec.Decode(channel) + if err != nil { + return nil, err + } + + if !channel.Ok { + return nil, errors.New(channel.Error) + } + + return channel.Channel, nil +} + func addAuth(req *http.Request) { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SLACK_TOKEN"))) }