-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack9gag.go
172 lines (155 loc) · 3.16 KB
/
slack9gag.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Response struct {
Text string `json:"text"`
Attachments []Attachments `json:"attachments"`
}
type Attachments struct {
Title string `json:"title"`
Title_link string `json:"title_link"`
Image_url string `json:"image_url"`
}
type jsonData struct {
Status int64
Message string
Data []Data
}
type Data struct {
Id string
Caption string
Images struct {
Small string
Cover string
Normal string
Large string
}
Media interface{}
Link string
Votes struct {
Count int64
}
Comments struct {
Count int64
}
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("listening...")
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
url := "http://infinigag.k3min.eu"
// Read the Request Parameter "command"
command := r.FormValue("command")
if command == "/9gag" {
// Read the Request Parameter "text"
text := r.FormValue("text")
respText := "9gag\n"
s := strings.Split(text, " ")
var section string
var subsection string
if len(s) == 2 {
section = s[0]
subsection = s[1]
respText += "Section: " + section + ", "
respText += "Sub-section: " + subsection
} else if len(s) == 1 {
section = s[0]
respText += " Section: " + section
}
switch section {
case "":
case "cute":
url += "/cute"
case "comic":
url += "/comic"
case "cosplay":
url += "/cosplay"
case "design":
url += "/design"
case "food":
url += "/food"
case "funny":
url += "/funny"
case "geeky":
url += "/geeky"
case "gif":
url += "/gif"
case "girl":
url += "/girl"
case "meme":
url += "/meme"
case "nsfw":
url += "/nsfw"
case "timely":
url += "/timely"
case "wtf":
url += "/wtf"
default:
fmt.Fprint(w, "I do not understand your command.")
return
}
switch subsection {
case "":
case "fresh":
url += "/fresh"
case "hot":
url += "/hot"
default:
fmt.Fprint(w, "I do not understand your command.")
return
}
fmt.Println(url)
r, err := http.Get(url)
if err != nil {
fmt.Println("Error requesting data")
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println("Error while opening file", err)
return
}
x := new(jsonData)
err = json.Unmarshal(body, &x)
if err != nil {
fmt.Println("Error while parsing file", err)
return
}
jsonResp(w, x, respText)
} else {
fmt.Fprint(w, "I do not understand your command.")
}
}
func jsonResp(w http.ResponseWriter, x *jsonData, respText string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
attachments := make([]Attachments, len(x.Data))
for i := 0; i < len(x.Data); i++ {
attachments[i] = Attachments{
Title: x.Data[i].Caption,
Title_link: x.Data[i].Link,
Image_url: x.Data[i].Images.Small,
}
}
resp := Response{
Text: respText,
Attachments: attachments,
}
r, err := json.Marshal(resp)
if err != nil {
fmt.Println("Couldn't marshal hook response:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(r)
}