-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmessages.go
138 lines (125 loc) · 4.13 KB
/
messages.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
// Copyright (c) 2014 Jason Goecke
// messages.go
package wit
import (
"encoding/json"
"net/url"
"strconv"
)
// Message represents a Wit message (https://wit.ai/docs/api#toc_3)
type Message struct {
MsgID string `json:"msg_id"`
Text string `json:"_text"`
Outcomes []Outcome `json:"outcomes"`
}
// Outcome represents the outcome portion of a Wit message
type Outcome struct {
Text string `json:"_text"`
Intent string `json:"intent"`
IntentId string `json:"intent_id"`
Entities map[string][]MessageEntity `json:"entities"`
Confidence float32 `json:"confidence"`
}
// MessageEntity represents the entity portion of a Wit message
type MessageEntity struct {
Metadata *string `json:"metadata,omitempty"`
Value *interface{} `json:"value,omitempty"`
Grain *string `json:"grain,omitempty"`
Type *string `json:"type,omitempty"`
Unit *string `json:"unit,omitempty"`
Body *string `json:"body,omitempty"`
Entity *string `json:"entity,omitempty"`
Start *int64 `json:"start,omitempty"`
End *int64 `json:"end,omitempty"`
Values *[]interface{} `json:"values,omitempty"`
From *DatetimeIntervalEnd `json:"from,omitempty"`
To *DatetimeIntervalEnd `json:"to,omitempty"`
}
// DatetimeValue represents the datetime value portion of a Wit message
type DatetimeValue struct {
From DatetimeIntervalEnd `json:"from"`
To DatetimeIntervalEnd `json:"to"`
}
type DatetimeIntervalEnd struct {
Value string `json:"value"`
Grain string `json:"grain"`
}
// MessageRequest represents a request to process a message
type MessageRequest struct {
File string `json:"file,omitempty"`
Query string `json:"query"`
MsgID string `json:"msg_id,omitempty"`
Context string `json:"context, omitempty"`
ContentType string `json:"contentType, omitempty"`
N int `json:"n,omitempty"`
FileContents []byte `json:"-"`
// Are context and Meta necessary anymore?
// Context Context
// Meta map[string]interface{}
}
// Context represents the context portion of the message request
type Context struct {
ReferenceTime string `json:"reference_time"`
Timezone string `json:"timezone"`
}
// Messages lists an already existing message (https://wit.ai/docs/api#toc_11)
//
// result, err := client.Messages("ba0fcf60-44d3-4499-877e-c8d65c239730")
func (client *Client) Messages(id string) (*Message, error) {
result, err := get(client.APIBase + "/messages/" + id)
if err != nil {
return nil, err
}
message, err := parseMessage(result)
return message, nil
}
// Message requests processing of a text message (https://wit.ai/docs/api#toc_3)
//
// result, err := client.Message(request)
func (client *Client) Message(request *MessageRequest) (*Message, error) {
query := url.QueryEscape(request.Query)
if request.Context != "" {
query += "&context=" + request.Context
}
if request.MsgID != "" {
query += "&msg_id" + request.MsgID
}
if request.N != 0 {
query += "&n=" + strconv.Itoa(request.N)
}
result, err := get(client.APIBase + "/message?q=" + query)
if err != nil {
return nil, err
}
message, _ := parseMessage(result)
return message, nil
}
// AudioMessage requests processing of an audio message (https://wit.ai/docs/api#toc_8)
//
// request := &MessageRequest{}
// request.File = "./audio_sample/helloWorld.wav"
// request.FileContents = data
// request.ContentType = "audio/wav;rate=8000"
// message, err := client.AudioMessage(request)
func (client *Client) AudioMessage(request *MessageRequest) (*Message, error) {
result, err := postFile(client.APIBase+"/speech", request)
if err != nil {
return nil, err
}
message, err := parseMessage(result)
if err != nil {
return nil, err
}
return message, nil
}
// Parses the JSON into a Message
//
// message, err := parseMessage([]byte(data))
func parseMessage(data []byte) (*Message, error) {
message := &Message{}
err := json.Unmarshal(data, message)
if err != nil {
return nil, err
}
return message, nil
}