-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.go
57 lines (46 loc) · 1.03 KB
/
discord.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
package hap
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
)
//go:generate go run examples/gen.go
// discord webhook is a fallback because the
// (faunadb) storage bucket will be next
func Webhook(status string) string {
// TODO instead of env var, retrieve webhook from session db table
wh := os.Getenv("DISCORD_WEBHOOK")
av := os.Getenv("GITHUB_AVATAR")
if wh == "DISABLE_WEBHOOK" {
return ""
}
m := map[string]string{
"username": "Webhook-activitypub-demo",
"avatar_url": av,
"content": status,
}
buf, err := json.Marshal(m)
if err != nil {
log.Print("Marshal failed on webhook")
log.Print(err)
return ""
}
resp, err := http.Post(wh, "application/json", bytes.NewReader(buf))
if err != nil {
log.Print("Post failed for webhook")
log.Print(err)
return ""
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print("Response webhook failed")
log.Print(err)
return ""
}
log.Printf("Webhook status - %s", data)
return string(data)
}