-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
62 lines (52 loc) · 1.36 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package summaraizer
import (
"encoding/json"
"io"
"net/http"
)
// Slack is a source that fetches comments from a Slack thread.
type Slack struct {
Token string // The OAuth token.
Channel string // The channel ID.
TS string // The timestamp of the thread.
}
// Fetch fetches comments from a Slack thread.
func (s *Slack) Fetch(writer io.Writer) error {
return fetchAndEncode(writer, func() (Comments, error) {
url := "https://slack.com/api/conversations.replies?channel=" + s.Channel + "&ts=" + s.TS
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
request.Header.Set("Authorization", "Bearer "+s.Token)
request.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response slackThreadResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
var comments Comments
for _, message := range response.Messages {
comments = append(comments, Comment{
Author: message.User,
Body: message.Text,
})
}
return comments, nil
})
}
type slackThreadResponse struct {
Messages []struct {
User string `json:"user"`
Text string `json:"text"`
} `json:"messages"`
}