-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (82 loc) · 1.73 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
func weebhook(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("secret") != os.Getenv("FORTUNE_COOKIE") {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Page not found"))
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
var a action
err = json.Unmarshal(data, &a)
if err != nil {
panic(err)
}
switch a.QueryResult.Action {
case "input.welcome":
log.Println("input.welcome triggered")
rand.Seed(time.Now().UTC().UnixNano())
if rand.Float32() > 0.5 {
err := sendMessage(w, NewWelcomeMessage())
if err != nil {
panic(err)
}
} else {
err := sendMessage(w, NewEvent("custom_welcome_event"))
if err != nil {
panic(err)
}
}
case "input.feeling":
log.Println("input.feeling triggered")
err := sendQuoteWithFeeling(w, a.QueryResult.Parameters.Feeling)
if err != nil {
panic(err)
}
case "input.fortune":
log.Println("input.fortune triggered")
err := sendQuote(w)
if err != nil {
panic(err)
}
case "input.authors":
log.Println("input.authors triggered")
err := sendMessage(w, NewAuthorList())
if err != nil {
panic(err)
}
case "input.author.quote":
log.Println("input.authors.quote triggered")
a, err := getAuthor(a.QueryResult.OutputContexts)
if err != nil {
panic(err)
}
err = sendAuthorQuote(w, a)
if err != nil {
panic(err)
}
default:
log.Println("no input action triggered")
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/webhook", weebhook).Methods("POST")
log.Println("Start listening on :9090...")
err := http.ListenAndServe(":9090", r)
if err != nil {
panic(err)
}
}