-
Notifications
You must be signed in to change notification settings - Fork 2
/
machaao.go
106 lines (80 loc) · 2.51 KB
/
machaao.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
package machaao
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
)
// Get MachaaoAPIToken from https://portal.messengerx.io
var MachaaoAPIToken string = os.Getenv("MachaaoAPIToken")
// Get WitAPIToken from https://wit.ai
var WitAPIToken string = os.Getenv("WitAPIToken")
// for dev, use https://ganglia-dev.machaao.com
var MachaaoBaseURL string = os.Getenv("MachaaoBaseURL")
// This function handles messages
// Input parameters (http.ResponseWriter, *http.Request)
type MessageHandler func(http.ResponseWriter, *http.Request)
// Starts server at given PORT. WebHook is machaao_hook
// input message handler type function(http.ResponseWriter, *http.Request)
func Server(handler MessageHandler) {
port := GetPort()
if MachaaoAPIToken == "" {
log.Fatalln("[ERROR] Machaao API Token not initialised.")
}
if MachaaoBaseURL == "" {
log.Fatalln("[ERROR] Machaao Base URL not initialised.")
}
if WitAPIToken == "" {
log.Println("[WARNING] Wit API Token not initialised.")
}
//API handler function
http.HandleFunc("/machaao_hook", handler)
//Go http server
log.Println("[-] Listening on...", port)
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatal(err)
}
}
// Sends post request to MessengerX.io API
func SendPostReq(apiURL string, body interface{}) (*http.Response, error) {
var url string = MachaaoBaseURL + "/" + apiURL
//Body converted to json bytes from interface.
jsonBody, _ := json.Marshal(body)
//Post request sent to MessengerX.io API
req, err1 := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
//Sets required headers for MessengerX.io API
req.Header.Set("Content-Type", "application/json")
req.Header.Set("api_token", MachaaoAPIToken)
if err1 != nil {
log.Println(err1)
return nil, err1
}
client := &http.Client{}
resp, err2 := client.Do(req)
return resp, err2
}
// Sends get request to MessengerX.io API
func SendGetReq(apiURL string) (*http.Response, error) {
var url string = MachaaoBaseURL + "/" + apiURL
req, err1 := http.NewRequest("GET", url, nil)
if err1 != nil {
log.Println(err1)
return nil, err1
}
//Sets required headers for MessengerX.io API
req.Header.Set("Content-Type", "application/json")
req.Header.Set("api_token", MachaaoAPIToken)
client := &http.Client{}
resp, err2 := client.Do(req)
return resp, err2
}
// Set PORT as env var or leave it to use 4747
func GetPort() string {
port := os.Getenv("PORT")
if port == "" {
port = "4747"
log.Println("[-] No PORT environment variable detected. Setting to ", port)
}
return ":" + port
}