-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (68 loc) · 1.61 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
)
func isValidInput(text string) bool {
if len(text) >= 2 && (text == "NSFW" || text[:1] == "@" || text[:1] == "#") {
return true
}
return false
}
func getInstaImg(text string) (string, error) {
var imgUrl string
var err error
if text == "NSFW" {
imgUrl, err = GetRandomFromProfile()
} else if text[:1] == "@" {
imgUrl, err = GetFromProfile(text[1:])
} else if text[:1] == "#" {
imgUrl, err = GetFromTag(text[1:])
}
return imgUrl, err
}
func handler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Fprint(w, "`Wrong input`")
return
}
//strip string
limitedChannels := strings.Replace(os.Getenv("LIMITED_CHANNELS"), " ", "", -1)
if limitedChannels != "" {
limitedChannelsArr := strings.Split(limitedChannels, ",")
if !contains(limitedChannelsArr, r.FormValue("channel_name")) {
fmt.Fprint(w, "`No permission`")
return
}
}
w.Header().Set("Content-Type", "application/json")
text := r.FormValue("text")
if !isValidInput(text) {
fmt.Fprint(w, "`Invalid input`")
return
}
imgUrl, err := getInstaImg(text)
if err != nil {
fmt.Fprint(w, "`No img found`")
return
}
fmt.Fprintf(w, `{"response_type": "in_channel", "attachments": [{"fields": [{"title": "%s"}],"image_url": "%s"}]}`, text, imgUrl)
}
func main() {
addr := ":" + os.Getenv("PORT")
http.HandleFunc("/", handler)
log.Printf("Listening on %s\n", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
func contains(arr []string, str string) bool {
for _, value := range arr {
if value == str {
return true
}
}
return false
}