From 2143ef38fa37812933969e5ef1bad58a9a3852b6 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 27 Oct 2022 22:51:06 +0000 Subject: [PATCH] Remove stars --- slack2asana.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/slack2asana.go b/slack2asana.go index 7a684d4..237b54d 100644 --- a/slack2asana.go +++ b/slack2asana.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "errors" "fmt" @@ -9,6 +10,11 @@ import ( "os" ) +type RemoveStarRequest struct { + Channel string `json:"channel"` + Timestamp string `json:"timestamp"` +} + type ChannelResponse struct { Ok bool `json:"ok"` Error string `json:"error"` @@ -54,6 +60,11 @@ type User struct { Name string `json:"name"` } +type SimpleResponse struct { + Ok bool `json:"ok"` + Error string `json:"error"` +} + func main() { c := &http.Client{} @@ -83,6 +94,11 @@ func main() { } fmt.Printf("%s\n", title) + + err = removeStar(c, item) + if err != nil { + panic(err) + } } } @@ -197,6 +213,45 @@ func getChannel(c *http.Client, id string) (*Channel, error) { return channel.Channel, nil } +func removeStar(c *http.Client, item *Item) error { + body := &RemoveStarRequest{ + Channel: item.Channel, + Timestamp: item.Message.Ts, + } + + js, err := json.Marshal(body) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", "https://slack.com/api/stars.remove", bytes.NewReader(js)) + if err != nil { + return err + } + + addAuth(req) + req.Header.Add("Content-Type", "application/json") + + resp, err := c.Do(req) + if err != nil { + return err + } + + dec := json.NewDecoder(resp.Body) + sr := &SimpleResponse{} + + err = dec.Decode(sr) + if err != nil { + return err + } + + if !sr.Ok { + return errors.New(sr.Error) + } + + return nil +} + func addAuth(req *http.Request) { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SLACK_TOKEN"))) }