-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
264 lines (238 loc) · 7.34 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
//"runtime"
//"regexp"
"strings"
)
var accessToken = os.Getenv("ACCESS_TOKEN")
var verifyToken = os.Getenv("VERIFY_TOKEN")
var port = os.Getenv("PORT")
const FacebookEndPoint = "https://graph.facebook.com/v2.6/me/messages"
type ReceivedMessage struct {
Object string `json:"object"`
Entry []Entry `json:"entry"`
}
type Entry struct {
ID int64 `json:"id"`
Time int64 `json:"time"`
Messaging []Messaging `json:"messaging"`
}
type Messaging struct {
Sender Sender `json:"sender"`
Recipient Recipient `json:"recipient"`
Timestamp int64 `json:"timestamp"`
Message Message `json:"message"`
}
type Sender struct {
ID int64 `json:"id"`
}
type Recipient struct {
ID int64 `json:"id"`
}
type Message struct {
MID string `json:"mid"`
Seq int64 `json:"seq"`
Text string `json:"text"`
}
type Payload struct {
TemplateType string `json:"template_type"`
Text string `json:"text"`
Buttons Buttons `json:"buttons"`
}
type Buttons struct {
Type string `json:"type"`
Url string `json:"url"`
Title string `json:"title"`
}
type Attachment struct {
Type string `json:"type"`
Payload Payload `json:"payload"`
}
type ButtonMessageBody struct {
Attachment Attachment `json:"attachment"`
}
type ButtonMessage struct {
Recipient Recipient `json:"recipient"`
ButtonMessageBody ButtonMessageBody `json:"message"`
}
type SendMessage struct {
Recipient Recipient `json:"recipient"`
Message struct {
Text string `json:"text"`
} `json:"message"`
}
func main() {
http.HandleFunc("/", TopPageHandler)
http.HandleFunc("/webhook", webhookHandler)
address := fmt.Sprintf(":%s", port)
http.ListenAndServe(address, nil)
}
func TopPageHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is go-bot application's top page.")
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
verifyTokenAction(w, r)
}
if r.Method == "POST" {
webhookPostAction(w, r)
}
}
func verifyTokenAction(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("hub.verify_token") == verifyToken {
log.Print("verify token success.")
fmt.Fprintf(w, r.URL.Query().Get("hub.challenge"))
} else {
log.Print("Error: verify token failed.")
fmt.Fprintf(w, "Error, wrong validation token")
}
}
func webhookPostAction(w http.ResponseWriter, r *http.Request) {
var receivedMessage ReceivedMessage
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print(err)
}
if err = json.Unmarshal(body, &receivedMessage); err != nil {
log.Print(err)
}
messagingEvents := receivedMessage.Entry[0].Messaging
for _, event := range messagingEvents {
senderID := event.Sender.ID
if &event.Message != nil && event.Message.Text != "" {
// TODO: Fix sendButtonMessage function
//if messageForButton(event.Message.Text) {
// message := getReplyMessage(event.Message.Text)
// sendButtonMessage(senderID, message)
//} else {
// message := getReplyMessage(event.Message.Text)
// sendTextMessage(senderID, message)
//}
message := getReplyMessage(event.Message.Text)
sendTextMessage(senderID, message)
}
}
fmt.Fprintf(w, "Success")
}
func messageForButton(text string) bool {
return strings.Contains(text, "予約") || strings.Contains(text, "RESERVATION") || strings.Contains(text, "RESERVE") || strings.Contains(text, "BOOKING")
}
// TODO: Reply message is just sample and made by easy logic, need to enhance the logic.
func getReplyMessage(receivedMessage string) string {
var message string
receivedMessage = strings.ToUpper(receivedMessage)
log.Print(" Received message: " + receivedMessage)
if strings.Contains(receivedMessage, "予約") {
message = "予約ありがとうございます。ご予約日を選んでください。"
} else if strings.Contains(receivedMessage, "場所") {
message = "BTSアソーク駅の近くです。"
} else if strings.Contains(receivedMessage, "電話") {
message = "02-123-4567"
} else if strings.Contains(receivedMessage, "RESERVATION") {
message = "Thank you for reservation! Choose the date."
} else if strings.Contains(receivedMessage, "RESERVE") {
message = "Thank you for reservation! Choose the date."
} else if strings.Contains(receivedMessage, "BOOKING") {
message = "Thank you for reservation! Choose the date."
} else if strings.Contains(receivedMessage, "LOCATION") {
message = "Near BTS Asok station."
} else if strings.Contains(receivedMessage, "WHERE") {
message = "Near BTS Asok station."
} else if strings.Contains(receivedMessage, "TEL") {
message = "02-123-4567"
} else if receivedMessage == "HI" {
message = "Hi! This is Golang Restaurant."
} else if receivedMessage == "こんにちは" {
message = "お問合せありがとうございます。Golangレストランです。"
} else {
message = "ちょっと意味が分かりません"
}
return message
}
// TODO: Fix Buttons json's format
func sendButtonMessage(senderID int64, text string) {
recipient := new(Recipient)
recipient.ID = senderID
buttonMessage := new(ButtonMessage)
buttonMessage.Recipient = *recipient
buttonMessage.ButtonMessageBody.Attachment.Type = "template"
buttonMessage.ButtonMessageBody.Attachment.Payload.TemplateType = "button"
buttonMessage.ButtonMessageBody.Attachment.Payload.Text = text
buttonMessage.ButtonMessageBody.Attachment.Payload.Buttons.Type = "web_url"
buttonMessage.ButtonMessageBody.Attachment.Payload.Buttons.Url = "https://still-bayou-19762.herokuapp.com/"
buttonMessage.ButtonMessageBody.Attachment.Payload.Buttons.Title = "May 13"
log.Print(buttonMessage)
buttonMessageBody, err := json.Marshal(buttonMessage)
log.Print(buttonMessageBody)
if err != nil {
log.Print(err)
}
req, err := http.NewRequest("POST", FacebookEndPoint, bytes.NewBuffer(buttonMessageBody))
if err != nil {
log.Print(err)
}
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.Print(err)
}
defer res.Body.Close()
var result map[string]interface{}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Print(err)
}
if err := json.Unmarshal(body, &result); err != nil {
log.Print(err)
}
log.Print(result)
}
func sendTextMessage(senderID int64, text string) {
recipient := new(Recipient)
recipient.ID = senderID
send_message := new(SendMessage)
send_message.Recipient = *recipient
send_message.Message.Text = text
send_message_body, err := json.Marshal(send_message)
if err != nil {
log.Print(err)
}
req, err := http.NewRequest("POST", FacebookEndPoint, bytes.NewBuffer(send_message_body))
if err != nil {
log.Print(err)
}
fmt.Println("%T", req)
fmt.Println("%T", err)
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.Print(err)
}
defer res.Body.Close()
var result map[string]interface{}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Print(err)
}
if err := json.Unmarshal(body, &result); err != nil {
log.Print(err)
}
log.Print(result)
}