Working fetch call
This commit is contained in:
@@ -1,26 +1,78 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StarsResponse struct {
|
type StarsResponse struct {
|
||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
Items []Item `json:"items"`
|
Error string `json:"error"`
|
||||||
|
Items []*Item `json:"items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Channel string `json:"channel",omitempty`
|
Channel string `json:"channel"`
|
||||||
Message *Message `json:"message"`
|
Message *Message `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message Struct {
|
type Message struct {
|
||||||
ClientMessageId string `json:"client_message_id"`
|
ClientMessageId string `json:"client_msg_id"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
Ts string `json:"ts"`
|
Ts string `json:"ts"`
|
||||||
Permalink string `json:"permalink"`
|
Permalink string `json:"permalink"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
c := &http.Client{}
|
||||||
|
|
||||||
|
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")))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user