-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinteract.go
More file actions
86 lines (73 loc) · 2.5 KB
/
interact.go
File metadata and controls
86 lines (73 loc) · 2.5 KB
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
package voiceflow
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/xavidop/voiceflow-cli/internal/global"
"github.com/xavidop/voiceflow-cli/internal/types/tests"
"github.com/xavidop/voiceflow-cli/internal/types/voiceflow/interact"
"github.com/xavidop/voiceflow-cli/internal/utils"
)
func DialogManagerInteract(environmentName, userID string, interaction tests.Interaction, apiKeyOverride, subdomainOverride string) ([]interact.InteractionResponse, error) {
// Use the provided subdomain override, or fall back to global if not provided
subdomain := global.VoiceflowSubdomain
if subdomainOverride != "" {
subdomain = subdomainOverride
}
// Add the dot prefix if subdomain is not empty
if subdomain != "" {
subdomain = "." + subdomain
}
url := fmt.Sprintf("https://general-runtime%s.voiceflow.com/state/user/%s/interact?logs=off", subdomain, userID)
var interatctionRequest interact.InteratctionRequest
switch interaction.User.Type {
case "launch":
interatctionRequest = interact.InteratctionRequest{
Action: interact.Action{
Type: "launch",
},
}
case "text":
interatctionRequest = interact.InteratctionRequest{
Action: interact.Action{
Type: "text",
Payload: interaction.User.Text,
},
}
}
byts, err := json.Marshal(interatctionRequest)
if err != nil {
return []interact.InteractionResponse{}, fmt.Errorf("error marshalling request: %v", err)
}
payload := strings.NewReader(string(byts))
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return []interact.InteractionResponse{}, fmt.Errorf("error creating request: %v", err)
}
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
// Pass the optional token to the interaction
apiKey := global.VoiceflowAPIKey
if apiKeyOverride != "" {
apiKey = apiKeyOverride
}
req.Header.Add("Authorization", apiKey)
req.Header.Add("versionID", environmentName)
res, err := http.DefaultClient.Do(req)
if err != nil {
return []interact.InteractionResponse{}, fmt.Errorf("error calling API: %v", err)
}
defer utils.SafeClose(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return []interact.InteractionResponse{}, fmt.Errorf("error reading response: %v", err)
}
var interactions []interact.InteractionResponse
err = json.Unmarshal([]byte(string(body)), &interactions)
if err != nil {
return []interact.InteractionResponse{}, fmt.Errorf("error unmarshalling: %v. Response body: %s", err, string(body))
}
return interactions, nil
}