-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (124 loc) · 2.98 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
)
var (
port = os.Getenv("PORT")
discordWebhookURL = os.Getenv("DISCORD_WEBHOOK_URL")
msgPrefix = os.Getenv("MESSAGE_PREFIX")
channelID = os.Getenv("SLACK_CHANNEL_ID")
)
var urlRe = regexp.MustCompile(`[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)`)
type slackTyped struct {
Type string
}
type slackEventTyped struct {
Event struct {
Type string
}
}
type slackChallengeEvent struct {
Challenge string `json:"challenge"`
}
type slackMessageEvent struct {
Event struct {
Text string
Channel string
}
}
type discordWebhookRequest struct {
Username string `json:"username"`
Content string `json:"content"`
}
func main() {
if port == "" {
port = "8080"
}
if discordWebhookURL == "" {
fmt.Fprintln(os.Stderr, "No Discord webhook token is provided. Set DISCORD_WEBHOOK_TOKEN")
os.Exit(1)
}
http.HandleFunc("/event", func(w http.ResponseWriter, r *http.Request) {
var req slackTyped
body, err := ioutil.ReadAll(r.Body)
fmt.Println(string(body))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if err := json.Unmarshal(body, &req); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
switch req.Type {
case "url_verification":
var challenge slackChallengeEvent
if err := json.Unmarshal(body, &challenge); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if err := json.NewEncoder(w).Encode(challenge); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "event_callback":
var ev slackEventTyped
if err := json.Unmarshal(body, &ev); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
switch ev.Event.Type {
case "message":
var msg slackMessageEvent
if err := json.Unmarshal(body, &msg); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if msg.Event.Channel != channelID {
return
}
for _, url := range urlRe.FindAllString(msg.Event.Text, -1) {
wreq := &discordWebhookRequest{
Username: "Music Commander",
Content: fmt.Sprintf("%s%s", msgPrefix, url),
}
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(wreq); err != nil {
fmt.Fprintln(os.Stderr, err)
}
w.Close()
}()
resp, err := http.Post(discordWebhookURL, "application/json", r)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
switch resp.StatusCode {
case http.StatusOK:
fmt.Printf("%s; Response: ", resp.Status)
io.Copy(os.Stdout, resp.Body)
fmt.Println()
default:
fmt.Fprintf(os.Stderr, "%s; Response: ", resp.Status)
io.Copy(os.Stderr, resp.Body)
fmt.Fprintln(os.Stderr)
}
}
default:
fmt.Printf("Ignored: %s\n", string(body))
}
default:
fmt.Printf("Ignored: %s\n", string(body))
}
})
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}