Files
slack2asana/slack2asana.go

79 lines
1.3 KiB
Go
Raw Normal View History

2022-10-20 22:03:07 -07:00
package main
import (
2022-10-23 06:10:46 +00:00
"encoding/json"
"errors"
"fmt"
2022-10-20 22:03:07 -07:00
"net/http"
2022-10-23 06:10:46 +00:00
"os"
2022-10-20 22:03:07 -07:00
)
type StarsResponse struct {
2022-10-23 06:10:46 +00:00
Ok bool `json:"ok"`
Error string `json:"error"`
Items []*Item `json:"items"`
2022-10-20 22:03:07 -07:00
}
type Item struct {
2022-10-23 06:10:46 +00:00
Type string `json:"type"`
Channel string `json:"channel"`
2022-10-20 22:03:07 -07:00
Message *Message `json:"message"`
}
2022-10-23 06:10:46 +00:00
type Message struct {
ClientMessageId string `json:"client_msg_id"`
Text string `json:"text"`
User string `json:"user"`
Ts string `json:"ts"`
Permalink string `json:"permalink"`
2022-10-20 22:03:07 -07:00
}
2022-10-23 06:10:46 +00:00
func main() {
c := &http.Client{}
2022-10-20 22:03:07 -07:00
2022-10-23 06:10:46 +00:00
stars, err := getStars(c)
if err != nil {
panic(err)
}
for _, item := range stars {
if item.Type != "message" {
continue
}
fmt.Printf("%#v\n", item.Message)
}
}
func getStars(c *http.Client) ([]*Item, error) {
req, err := http.NewRequest("GET", "https://slack.com/api/stars.list", 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)
stars := &StarsResponse{}
err = dec.Decode(stars)
if err != nil {
return nil, err
}
if !stars.Ok {
return nil, errors.New(stars.Error)
}
return stars.Items, nil
}
func addAuth(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SLACK_TOKEN")))
}