-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (129 loc) · 3.26 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
)
const (
facebookEndPoint = "https://graph.facebook.com/v2.6/me/messages"
)
// Main entry point.
func main() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/webhook", webhookHandler)
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
log.Println("listen on port " + port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
panic(err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Server is running")
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
verifyTokenAction(w, r)
} else if r.Method == "POST" {
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(bodyBytes))
messageStr, senderId := GetMessageFromRequest(bodyBytes)
answer, _ := createBotAnswer(messageStr, senderId)
if answer.senderId != "" {
sendMessage(answer)
}
} else {
log.Println("Request was neither GET nor POST.")
}
}
func verifyTokenAction(w http.ResponseWriter, r *http.Request) {
verifyToken := os.Getenv("VERIFYTOKEN")
if r.URL.Query().Get("hub.verify_token") == verifyToken {
log.Println("verify token success.")
fmt.Fprintf(w, r.URL.Query().Get("hub.challenge"))
} else {
log.Println("Error: verify token failed.")
fmt.Fprint(w, "Error, wrong validation token")
}
}
func createBotAnswer(message string, senderId string) (Answer, TextInfo) {
textInfo := TextInfo{}
errText := []string{"Das habe ich leider nicht verstanden."}
if senderId == "" {
return Answer{"", 0, errText}, textInfo
}
if message == "" {
return Answer{senderId, 0, errText}, textInfo
}
textInfo.text = message
stopNames, stopIds, lines, isDelay := ProcessText(message)
textInfo.stopNames = stopNames
textInfo.stopIds = stopIds
textInfo.lines = lines
textInfo.delay = isDelay
textInfo.category = Categorize(textInfo)
answer := ProcessAnswer(textInfo)
answer.senderId = senderId
return answer, textInfo
}
// Sends message to FB endpoint.
func sendMessage(answer Answer) {
for _, answerText := range answer.text {
reqBody := Body{
Recipient: Recipient{
Id: answer.senderId,
},
Message: Message{
Text: answerText,
},
}
bodyBytes, _ := json.Marshal(reqBody)
req, err := http.NewRequest("POST", facebookEndPoint, bytes.NewBuffer(bodyBytes))
if err != nil {
log.Print(err)
}
accessToken := os.Getenv("ACCESSTOKEN")
values := url.Values{}
values.Add("access_token", accessToken)
req.URL.RawQuery = values.Encode()
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{Timeout: time.Duration(30 * time.Second)}
res, err := client.Do(req)
if err != nil {
log.Println(err)
}
var result map[string]interface{}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
if err := json.Unmarshal(resBody, &result); err != nil {
log.Println(err)
}
log.Println(result)
res.Body.Close()
}
}
type Body struct {
Recipient Recipient `json:"recipient"`
Message Message `json:"message"`
}
type Recipient struct {
Id string `json:"id"`
}
type Message struct {
Text string `json:"text"`
}