forked from rogertalk/go-avs
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathavs.go
111 lines (94 loc) · 3.72 KB
/
avs.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
/*
Package avs makes requests to Amazon's AVS API using HTTP/2 and also supports
creating a downchannel which receives directives from AVS based on the user's
speech and/or actions in the companion app.
To send a Recognize event to AVS, you can use the PostRecognize function:
audio, _ := os.Open("./request.wav")
response, err := avs.PostRecognize(ACCESS_TOKEN, "abc123",
"abc123dialog", audio)
For simple events you can use the PostEvent function:
event := NewVolumeChanged("abc123", 100, false)
response, err := avs.PostEvent(ACCESS_TOKEN, event)
You can also make requests to AVS with the Client.Do method:
request := avs.NewRequest(ACCESS_TOKEN)
request.Event = avs.NewRecognize("abc123", "abc123dialog")
request.Audio, _ = os.Open("./request.wav")
response, err := avs.DefaultClient.Do(request)
A Response will contain a list of directives from AVS. The list contains untyped
Message instances which hold the raw response data and headers, but it can be
typed by calling the Typed method of Message:
for _, directive := range response.Directives {
switch d := directive.Typed().(type) {
case *avs.Speak:
cid := d.ContentId()
ioutil.WriteFile("./speak.mp3", response.Content[cid], 0666)
default:
fmt.Println("No code to handle directive:", d)
}
}
To create a downchannel, a long-lived request for AVS to deliver directives,
use the CreateDownchannel method of the Client type:
directives, _ := avs.CreateDownchannel(ACCESS_TOKEN)
for directive := range directives {
switch d := directive.Typed().(type) {
case *avs.DeleteAlert:
fmt.Println("Delete alert:", d.Payload.Token)
case *avs.SetAlert:
fmt.Println("Set an alert for:", d.Payload.ScheduledTime)
default:
fmt.Println("No code to handle directive:", d)
}
}
*/
package avs
import (
"io"
)
// The different endpoints that are supported by the AVS API.
const (
// You can find the latest versioning information on the AVS API overview page:
// https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/content/avs-api-overview
Version = "/v20160207"
DirectivesPath = Version + "/directives"
EventsPath = Version + "/events"
PingPath = "/ping"
)
// DefaultClient is the default Client.
var DefaultClient = &Client{
// EndpointURL is the base endpoint URL for the AVS API.
EndpointURL: "https://avs-alexa-na.amazon.com",
}
// CreateDownchannel establishes a persistent connection with AVS and returns a
// read-only channel through which AVS will deliver directives.
//
// CreateDownchannel is a wrapper around DefaultClient.CreateDownchannel.
func CreateDownchannel(accessToken string) (<-chan *Message, error) {
return DefaultClient.CreateDownchannel(accessToken)
}
// PostEvent will post an event to AVS.
//
// PostEvent is a wrapper around DefaultClient.Do.
func PostEvent(accessToken string, event TypedMessage) (*Response, error) {
request := NewRequest(accessToken)
request.Event = event
return DefaultClient.Do(request)
}
// PostRecognize will post a Recognize event to AVS.
//
// PostRecognize is a wrapper around DefaultClient.Do.
func PostRecognize(accessToken, messageId, dialogRequestId string, audio io.Reader) (*Response, error) {
request := NewRequest(accessToken)
request.Event = NewRecognize(messageId, dialogRequestId)
request.Audio = audio
return DefaultClient.Do(request)
}
// PostSynchronizeState will post a SynchronizeState event with the provided
// context to AVS.
//
// PostSynchronizeState is a wrapper around DefaultClient.Do.
func PostSynchronizeState(accessToken, messageId string, context []TypedMessage) (*Response, error) {
request := NewRequest(accessToken)
request.Event = NewSynchronizeState(messageId)
request.Context = context
return DefaultClient.Do(request)
}