-
Notifications
You must be signed in to change notification settings - Fork 8
/
server_response.go
48 lines (44 loc) · 1.61 KB
/
server_response.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
package houndify
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"strings"
)
// ParseWrittenResponse will take final server response JSON (as a string)
// and parse out the human readable text to be displayed or spoken the end user.
// If the string is invalid JSON, the server had an error, or there was nothing
// to reply with, an error is returned.
func ParseWrittenResponse(serverResponseJSON string) (string, error) {
result := make(map[string]interface{})
err := json.Unmarshal([]byte(serverResponseJSON), &result)
if err != nil {
fmt.Println(err.Error())
return "", errors.New("failed to decode json")
}
if !strings.EqualFold(result["Status"].(string), "OK") {
return "", errors.New(result["ErrorMessage"].(string))
}
if result["NumToReturn"].(float64) < 1 {
return "", errors.New("no results to return")
}
return result["AllResults"].([]interface{})[0].(map[string]interface{})["WrittenResponseLong"].(string), nil
}
func parseConversationState(serverResponseJSON string) (interface{}, error) {
result := make(map[string]interface{})
err := json.Unmarshal([]byte(serverResponseJSON), &result)
if err != nil {
fmt.Println(err.Error())
return nil, errors.New("failed to decode json")
}
if !strings.EqualFold(result["Status"].(string), "OK") {
return nil, errors.New(result["ErrorMessage"].(string))
}
if result["NumToReturn"].(float64) < 1 {
return nil, errors.New("no results to return")
}
if len(result["AllResults"].([]interface{})) < 1 {
return nil, errors.New("empty server response")
}
return result["AllResults"].([]interface{})[0].(map[string]interface{})["ConversationState"], nil
}