forked from jsgoecke/go-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
117 lines (103 loc) · 3.02 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
// Copyright (c) 2014 Jason Goecke
// messages.go
package wit
import (
"encoding/json"
"net/url"
)
// Represents a Wit message (https://wit.ai/docs/api#toc_3)
type Message struct {
MsgId string `json:"msg_id"`
MsgBody string `json:"msg_body"`
Outcome Outcome `json:"outcome"`
}
// Represents the outcome portion of a Wit message
type Outcome struct {
Intent string `json:"intent"`
Entities MessageEntity `json:"entities"`
Confidence float32 `json:"confidence"`
}
// Represents the entity portion of a Wit message
type MessageEntity struct {
Metric Metric `json:"metric"`
Datetime []Datetime `json:"datetime"`
}
// Represents the metric portion of a Wit message
type Metric struct {
Value string `json:"value"`
Body string `json:"value"`
}
// Represents the datetime portion of a Wit message
type Datetime struct {
Value DatetimeValue `json:"value"`
Body string `json:"body"`
}
// Represents the datetime value portion of a Wit message
type DatetimeValue struct {
From string `json:"from"`
To string `json:"to"`
}
// Represents a request to process a message
type MessageRequest struct {
File, Query, MsgId, ContentType string
FileContents []byte
// Are context and Meta necessary anymore?
// Context Context
// Meta map[string]interface{}
}
// Represents the context portion of the message request
type Context struct {
ReferenceTime string `json:"reference_time"`
Timezone string `json:"timezone"`
}
// Lists an already existing message (https://wit.ai/docs/api#toc_11)
//
// result, err := client.Messages("ba0fcf60-44d3-4499-877e-c8d65c239730")
func (client *WitClient) 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
}
// Requests processing of a text message (https://wit.ai/docs/api#toc_3)
//
// result, err := client.Message(request)
func (client *WitClient) Message(request *MessageRequest) (*Message, error) {
result, err := get(client.ApiBase + "/message?q=" + url.QueryEscape(request.Query))
if err != nil {
return nil, err
}
message, _ := parseMessage(result)
return message, nil
}
// 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 *WitClient) 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
}