-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
74 lines (63 loc) · 1.63 KB
/
actions.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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
log "github.com/sirupsen/logrus"
)
type Action func(interface{})
func sendDataHelper(service string) Action {
return func(d interface{}) {
data, err := json.Marshal(d)
if err != nil {
log.Fatal(err)
}
log.Info("Sending the following to " + service)
log.Info(string(data))
url := os.Getenv("NLP_API") + "/send_message/" + string(data)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error(err.Error())
}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Error(err.Error())
}
defer res.Body.Close()
}
}
// Send payload to recipe-walkthrough endpoint
func changeStep(payload interface{}) {
url := os.Getenv("RECIPE_WALKTHROUGH_API") + "/increment-step"
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(payload.(string))))
if err != nil {
log.Error("Could not execute action changeStep")
log.Error(err.Error())
}
req.Header.Add("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Error("Could not execute action changeStep")
log.Error(err.Error())
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error("Error reading response")
log.Error(err.Error())
} else {
log.Info("Action response: " + string(body))
}
}
// Action used for testing purposes that just logs the payload
func mockAction(i interface{}) {
log.Info(i.(string))
}
// Actions for the trigger-queue to execute
var Actions = map[string]Action{
"sendToNLP": sendDataHelper("NLP"),
"changeStep": changeStep,
"mockAction": mockAction,
}