/
webhook.go
71 lines (62 loc) · 1.47 KB
/
webhook.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
package server
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
var (
urlReplacer = strings.NewReplacer("http://", "", "https://", "")
gameIdToName = map[string]string{
"2kki": "Yume 2kki",
"amillusion": "Amillusion",
"braingirl": "Braingirl",
"deepdreams": "Deep Dreams",
"flow": ".flow",
"genie": "Dream Genie",
"mikan": "Mikan Muzou",
"muma": "Muma|Rope",
"nostalgic": "nostAlgic",
"oversomnia": "Oversomnia",
"prayers": "Answered Prayers",
"sheawaits": "She Awaits",
"someday": "Someday",
"tsushin": "Yume Tsushin",
"ultraviolet": "Ultra Violet",
"unevendream": "Uneven Dream",
"yume": "Yume Nikki",
}
)
type WebhookRequest struct {
Username string `json:"username"`
AvatarUrl string `json:"avatar_url"`
Content string `json:"content"`
AllowedMentions struct {
Parse []string `json:"parse"`
} `json:"allowed_mentions"`
}
func sendWebhookMessage(url, name, badge, message string, sanitize bool) error {
var avatarUrl string
if badge != "" {
avatarUrl = fmt.Sprintf("https://ynoproject.net/%s/images/badge/%s.png", config.gameName, badge)
}
content := message
if sanitize {
content = urlReplacer.Replace(message)
}
body, err := json.Marshal(WebhookRequest{
Username: name,
AvatarUrl: avatarUrl,
Content: content,
})
if err != nil {
return err
}
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}