-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
422 lines (350 loc) · 10.3 KB
/
server.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
"github.com/omidnikta/logrus"
"google.golang.org/api/googleapi/transport"
youtube "google.golang.org/api/youtube/v3"
)
// BodyMsg message messenger
type BodyMsg struct {
Object string `json:"object"`
Entry []messaging `json:"entry"`
}
type messaging struct {
ID string `json:"id"`
Messaging []messagingEl `json:"messaging"`
Time int `json:"time"`
}
type messagingEl struct {
Message *message `json:"message,omitempty"`
PostBack *message `json:"postback,omitempty"`
Recipient recipient `json:"recipient"`
Sender sender `json:"sender"`
Timestamp int `json:"timestamp"`
}
type message struct {
Mid string `json:"mid,omitempty"`
Seq int `json:"seq,omitempty"`
Text string `json:"text,omitempty"`
Payload string `json:"payload,omitempty"`
}
type serverMessage struct {
Text string `json:"text,omitempty"`
Attachment *attachment `json:"attachment,omitempty"`
}
type recipient struct {
ID string `json:"id"`
}
type sender struct {
ID string `json:"id"`
}
type response struct {
Recipient recipient `json:"recipient"`
Message serverMessage `json:"message"`
}
type privacyData struct {
Domain string
Business string
City string
Country string
}
type attachment struct {
AttachmentType string `json:"type"`
Payload payload `json:"payload"`
}
type payload struct {
TemplateType string `json:"template_type"`
Elements []element `json:"elements"`
}
type element struct {
Title string `json:"title"`
ImageURL string `json:"image_url"`
DefaultAction defaultAction `json:"default_action"`
Buttons []button `json:"buttons"`
}
type defaultAction struct {
DefaultActionType string `json:"type"`
URL string `json:"url,omitempty"`
WebViewHeightRatio string `json:"webview_height_ratio,omitempty"`
MessengerExtensions bool `json:"messenger_extensions,omitempty"`
}
type button struct {
ButtonType string `json:"type"`
URL string `json:"url,omitempty"`
Title string `json:"title"`
MessengerExtensions bool `json:"messenger_extensions,omitempty"`
WebViewHeightRatio string `json:"webview_height_ratio,omitempty"`
Payload string `json:"payload,omitempty"`
}
func hello(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "Greetings from mi AI %s!", r.URL.Path[1:])
}
func verifyHook(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
verifyToken := os.Getenv("VERIFY_TOKEN")
mode := r.URL.Query().Get("hub.mode")
token := r.URL.Query().Get("hub.verify_token")
challenge := r.URL.Query().Get("hub.challenge")
if mode != "" && token != "" {
if mode == "subscribe" && token == verifyToken {
fmt.Println("WEBHOOK_VERIFIED")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", challenge)
} else {
http.Error(w, "Challenge failure", http.StatusBadRequest)
}
}
}
func apiHook(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
body := BodyMsg{}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
logrus.Errorf("Failed to decode JSON: %v.", err)
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
return
}
if body.Object == "page" {
for _, entry := range body.Entry {
webhookEvent := entry.Messaging[0]
if webhookEvent.Message != nil {
handleMessage(webhookEvent.Sender.ID, webhookEvent.Message)
}
if webhookEvent.PostBack != nil {
handlePostback(webhookEvent.Sender.ID, webhookEvent.PostBack)
}
}
} else {
http.Error(w, "Message error", http.StatusBadRequest)
}
}
func privacyHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
privacyData := privacyData{
os.Getenv("DOMAIN"),
os.Getenv("BUSINESS"),
os.Getenv("CITY"),
os.Getenv("COUNTRY"),
}
renderTemplate(w, "template/privacy_policy", privacyData)
}
func renderTemplate(w http.ResponseWriter, tmpl string, data privacyData) {
t, _ := template.ParseFiles(tmpl + ".html")
t.Execute(w, data)
}
// Handles messages events
func handleMessage(senderPsid string, receivedMessage *message) {
res := response{
Recipient: recipient{
ID: senderPsid,
},
}
if receivedMessage.Text != "" {
searchResults := youtubeSearchAPI(receivedMessage.Text)
if len(searchResults) > 0 {
var (
elements []element
buttons []button
)
for _, item := range searchResults {
elements = append(elements, element{
Title: item.Snippet.Title,
ImageURL: item.Snippet.Thumbnails.High.Url,
DefaultAction: defaultAction{
DefaultActionType: "web_url",
URL: "https://www.youtube.com/watch?v=" + item.Id.VideoId,
WebViewHeightRatio: "tall",
MessengerExtensions: true,
},
Buttons: append(buttons, button{
ButtonType: "postback",
Title: "Get MP3",
Payload: "GET_MP3:" + item.Id.VideoId + ":" + item.Snippet.Title,
}),
})
}
res.Message = serverMessage{
Attachment: &attachment{
AttachmentType: "template",
Payload: payload{
TemplateType: "generic",
Elements: elements,
},
},
}
} else {
res.Message = serverMessage{
Text: "Aucun résultat n'a été trouvé pour " + receivedMessage.Text,
}
}
callSendAPI(senderPsid, res)
}
}
// Handles messaging_postbacks events
func handlePostback(senderPsid string, receivedPostback *message) {
payload := strings.Split(receivedPostback.Payload, ":")
switch payload[0] {
case "GET_MP3":
fileName, err := downloadMP3(payload[1], payload[2])
if err != nil {
log.Fatalf("Error downloading MP3: %v", err)
return
}
uploadMP3(senderPsid, fileName)
}
}
// Creates a new file upload http request with optional extra params
// https://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/
func newFileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
func uploadMP3(senderPsid, fileName string) {
extraParams := map[string]string{
"recipient": "{'id': " + senderPsid + "}",
"message": "{'attachment':{'type':'file', 'payload':{'is_reusable':true}}}",
}
url := "https://graph.facebook.com/v2.6/me/messages?access_token=" + os.Getenv("PAGE_ACCESS_TOKEN")
request, err := newFileUploadRequest(url, extraParams, "filedata", fileName)
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
res, err := client.Do(request)
if err != nil {
log.Fatal(err)
} else {
body := &bytes.Buffer{}
_, err := body.ReadFrom(res.Body)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
}
}
func downloadMP3(videoID, videoName string) (string, error) {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting working dir: %v", err)
}
// Do not download and return mp3Path if the file is already present
mp3Path := dir + "/tmp/" + videoName + ".mp3"
if _, err := os.Stat(mp3Path); !os.IsNotExist(err) {
return mp3Path, nil
}
fileName := "'" + dir + "/tmp/%(title)s.%(ext)s" + "'"
videoURL := "https://www.youtube.com/watch?v=" + videoID
command := "youtube-dl --extract-audio --audio-format mp3 -o " + fileName + " " + videoURL
cmd := exec.Command("bash", "-c", command)
err = cmd.Run()
return mp3Path, err
}
// Sends response messages via the Send API
func callSendAPI(senderPsid string, res response) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(res)
if err != nil {
logrus.Errorf("Failed to encode JSON: %v.", err)
return
}
url := "https://graph.facebook.com/v2.6/me/messages"
req, err := http.NewRequest("POST", url, body)
req.Header.Add("Authorization", "Bearer "+os.Getenv("PAGE_ACCESS_TOKEN"))
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Status:", resp.Status)
fmt.Println("response Body:", string(respBody))
}
func youtubeSearchAPI(video string) []*youtube.SearchResult {
client := &http.Client{
Transport: &transport.APIKey{Key: os.Getenv("YOUTUBE_DATA_API_KEY")},
}
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating new YouTube client: %v", err)
}
// Make the API call to YouTube.
call := service.Search.List("id,snippet").
Q(video).
MaxResults(5)
response, err := call.Do()
if err != nil {
log.Fatalf("Error making search API call: %v", err)
}
return response.Items
}
// Sends response messages via the Send API
func facebookSendAPI(senderPsid string, res response) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(res)
if err != nil {
logrus.Errorf("Failed to encode JSON: %v.", err)
return
}
url := "https://graph.facebook.com/v2.6/me/messages"
req, err := http.NewRequest("POST", url, body)
req.Header.Add("Authorization", "Bearer "+os.Getenv("PAGE_ACCESS_TOKEN"))
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Status:", resp.Status)
fmt.Println("response Body:", string(respBody))
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
certPath := os.Getenv("CERT_PATH")
certKeyPath := os.Getenv("CERT_KEY_PATH")
router := httprouter.New()
router.GET("/", hello)
router.GET("/privacy", privacyHandler)
router.GET("/webhook", verifyHook)
router.POST("/webhook", apiHook)
error := http.ListenAndServeTLS(":443", certPath, certKeyPath, router)
if error != nil {
log.Fatal(http.ListenAndServe(":8080", router))
}
}