Remove stars

This commit is contained in:
Ian Gulliver
2022-10-27 22:51:06 +00:00
parent 7a44aa2f5f
commit 2143ef38fa

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -9,6 +10,11 @@ import (
"os" "os"
) )
type RemoveStarRequest struct {
Channel string `json:"channel"`
Timestamp string `json:"timestamp"`
}
type ChannelResponse struct { type ChannelResponse struct {
Ok bool `json:"ok"` Ok bool `json:"ok"`
Error string `json:"error"` Error string `json:"error"`
@@ -54,6 +60,11 @@ type User struct {
Name string `json:"name"` Name string `json:"name"`
} }
type SimpleResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
func main() { func main() {
c := &http.Client{} c := &http.Client{}
@@ -83,6 +94,11 @@ func main() {
} }
fmt.Printf("%s\n", title) 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 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) { func addAuth(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SLACK_TOKEN"))) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SLACK_TOKEN")))
} }