-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
85 lines (79 loc) · 2.45 KB
/
handler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"strconv"
"time"
"github.com/nlopes/slack"
"github.com/nlopes/slack/slackevents"
)
type EventHandler struct {
sc *slack.Client
kc *KibelaClient
}
func NewEventHandler(slackToken, kibelaTeam, kibelaToken string) *EventHandler {
return &EventHandler{
sc: slack.New(slackToken),
kc: NewKibelaClient(kibelaTeam, kibelaToken),
}
}
func (h *EventHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.String()
eventsAPIEvent, e := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionNoVerifyToken())
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
}
switch eventsAPIEvent.Type {
case slackevents.URLVerification:
var r *slackevents.ChallengeResponse
err := json.Unmarshal([]byte(body), &r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(r.Challenge))
case slackevents.CallbackEvent:
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.LinkSharedEvent:
unfurls := map[string]slack.Attachment{}
for _, url := range ev.Links {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := h.kc.NoteFromPath(ctx, url.URL)
if err != nil {
log.Printf("[ERROR] can not retrieve the note (%s): %s", url, err)
return
}
t, err := time.Parse(KibelaTimeFormat, res.Note.PublishedAt)
if err != nil {
log.Printf("[ERROR] can not parse publishedAt (%s): %s", res.Note.PublishedAt, err)
return
}
unfurls[url.URL] = slack.Attachment{
AuthorIcon: res.Note.Author.AvatarImage.URL,
AuthorLink: res.Note.Author.URL,
AuthorName: res.Note.Author.Account,
Title: res.Note.Title,
TitleLink: res.Note.URL,
Text: res.Note.Summary,
Footer: "Kibela",
FooterIcon: "https://cdn.kibe.la/assets/shortcut_icon-99b5d6891a0a53624ab74ef26a28079e37c4f953af6ea62396f060d3916df061.png",
Ts: json.Number(strconv.FormatInt(t.Unix(), 10)),
}
}
options := []slack.MsgOption{}
_, _, _, err := h.sc.UnfurlMessage(ev.Channel, ev.MessageTimeStamp.String(), unfurls, options...)
if err != nil {
log.Printf("[ERROR] can not send unfurl message: %s", err)
return
}
log.Printf("[INFO] send unfurl message for %s", ev.Links)
}
}
}