Fetch team info

This commit is contained in:
Ian Gulliver
2022-10-28 19:03:04 +00:00
parent f758bf295a
commit 9e61cf416c
2 changed files with 56 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ import (
type SlackClient struct { type SlackClient struct {
cli *http.Client cli *http.Client
token string token string
team *Team
} }
type removeStarRequest struct { type removeStarRequest struct {
@@ -45,6 +46,12 @@ type simpleResponse struct {
Error string `json:"error"` Error string `json:"error"`
} }
type teamResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
Team *Team `json:"team"`
}
type Purpose struct { type Purpose struct {
Value string `json:"value"` Value string `json:"value"`
} }
@@ -73,16 +80,56 @@ type Message struct {
Permalink string `json:"permalink"` Permalink string `json:"permalink"`
} }
type Team struct {
Domain string `json:"domain"`
}
type User struct { type User struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }
func NewSlackClient() *SlackClient { func NewSlackClient() (*SlackClient, error) {
return &SlackClient{ sc := &SlackClient{
cli: &http.Client{}, cli: &http.Client{},
token: os.Getenv("SLACK_TOKEN"), token: os.Getenv("SLACK_TOKEN"),
} }
team, err := sc.GetTeam()
if err != nil {
return nil, err
}
sc.team = team
return sc, nil
}
func (sc *SlackClient) GetTeam() (*Team, error) {
req, err := http.NewRequest("GET", "https://slack.com/api/team.info", nil)
if err != nil {
return nil, err
}
sc.addAuth(req)
resp, err := sc.cli.Do(req)
if err != nil {
return nil, err
}
dec := json.NewDecoder(resp.Body)
team := &teamResponse{}
err = dec.Decode(team)
if err != nil {
return nil, err
}
if !team.Ok {
return nil, errors.New(team.Error)
}
return team.Team, nil
} }
func (sc *SlackClient) GetStars() ([]*Item, error) { func (sc *SlackClient) GetStars() ([]*Item, error) {
@@ -264,7 +311,7 @@ func (sc *SlackClient) GetNotes(item *Item, user *User, channel *Channel) (strin
switch { switch {
case channel.IsIm: case channel.IsIm:
return fmt.Sprintf( return fmt.Sprintf(
"<body>%s</body>", `<body>%s</body>`,
sc.escape(title), sc.escape(title),
), nil ), nil
case channel.IsMpIm: case channel.IsMpIm:

View File

@@ -10,9 +10,13 @@ func main() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
ac := NewAsanaClient() ac := NewAsanaClient()
sc := NewSlackClient()
err := Poll(ac, sc) sc, err := NewSlackClient()
if err != nil {
log.Fatalf("%s", err)
}
err = Poll(ac, sc)
if err != nil { if err != nil {
log.Printf("%s", err) log.Printf("%s", err)
} }