-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
131 lines (116 loc) · 3.08 KB
/
app.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
const (
FACEBOOK_API = "https://graph.facebook.com/v2.6/me/messages?access_token=%s"
IMAGE = "http://37.media.tumblr.com/e705e901302b5925ffb2bcf3cacb5bcd/tumblr_n6vxziSQD11slv6upo3_500.gif"
)
type Callback struct {
Object string `json:"object,omitempty"`
Entry []struct {
ID string `json:"id,omitempty"`
Time int `json:"time,omitempty"`
Messaging []Messaging `json:"messaging,omitempty"`
} `json:"entry,omitempty"`
}
type Messaging struct {
Sender User `json:"sender,omitempty"`
Recipient User `json:"recipient,omitempty"`
Timestamp int `json:"timestamp,omitempty"`
Message Message `json:"message,omitempty"`
}
type User struct {
ID string `json:"id,omitempty"`
}
type Message struct {
MID string `json:"mid,omitempty"`
Text string `json:"text,omitempty"`
QuickReply *struct {
Payload string `json:"payload,omitempty"`
} `json:"quick_reply,omitempty"`
Attachments *[]Attachment `json:"attachments,omitempty"`
Attachment *Attachment `json:"attachment,omitempty"`
}
type Attachment struct {
Type string `json:"type,omitempty"`
Payload Payload `json:"payload,omitempty"`
}
type Response struct {
Recipient User `json:"recipient,omitempty"`
Message Message `json:"message,omitempty"`
}
type Payload struct {
URL string `json:"url,omitempty"`
}
func VerificationEndpoint(w http.ResponseWriter, r *http.Request) {
challenge := r.URL.Query().Get("hub.challenge")
mode := r.URL.Query().Get("hub.mode")
token := r.URL.Query().Get("hub.verify_token")
if mode != "" && token == os.Getenv("VERIFY_TOKEN") {
w.WriteHeader(200)
w.Write([]byte(challenge))
} else {
w.WriteHeader(404)
w.Write([]byte("Error, wrong validation token"))
}
}
func ProcessMessage(event Messaging) {
client := &http.Client{}
response := Response{
Recipient: User{
ID: event.Sender.ID,
},
Message: Message{
Attachment: &Attachment{
Type: "image",
Payload: Payload{
URL: IMAGE,
},
},
},
}
body := new(bytes.Buffer)
json.NewEncoder(body).Encode(&response)
url := fmt.Sprintf(FACEBOOK_API, os.Getenv("PAGE_ACCESS_TOKEN"))
req, err := http.NewRequest("POST", url, body)
req.Header.Add("Content-Type", "application/json")
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
}
func MessagesEndpoint(w http.ResponseWriter, r *http.Request) {
var callback Callback
json.NewDecoder(r.Body).Decode(&callback)
if callback.Object == "page" {
for _, entry := range callback.Entry {
for _, event := range entry.Messaging {
ProcessMessage(event)
}
}
w.WriteHeader(200)
w.Write([]byte("Got your message"))
} else {
w.WriteHeader(404)
w.Write([]byte("Message not supported"))
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/webhook", VerificationEndpoint).Methods("GET")
r.HandleFunc("/webhook", MessagesEndpoint).Methods("POST")
if err := http.ListenAndServe("0.0.0.0:8080", r); err != nil {
log.Fatal(err)
}
}