-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
362 lines (328 loc) · 9.5 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"sync"
"time"
// "github.com/fika-io/alexa/migration"
"github.com/fika-io/go-avs"
"golang.org/x/net/context"
"google.golang.org/cloud/datastore"
)
const (
ConfigPath = "./config.json"
)
var (
// TODO: Replace cache with an LRU once we start hitting tens of thousands.
cache = make(map[int64]*ServiceAuth)
cacheMutex sync.Mutex
config Config
ctx = context.Background()
store *datastore.Client
)
func main() {
// Load configuration from file.
data, err := ioutil.ReadFile(ConfigPath)
if err != nil {
log.Fatalf("! Failed to load config (ioutil.ReadFile: %v)", err)
}
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatalf("! Failed to load config (json.Unmarshal: %v)", err)
}
// Set up the Datastore client.
store, err = datastore.NewClient(ctx, config.ProjectId)
if err != nil {
log.Fatalf("! Failed to create Datastore client (datastore.NewClient: %v)", err)
}
// migration.Migrate(store, func(aad migration.AlexaAuthData) (*datastore.Key, interface{}) {
// key := Key(ctx, aad.AccountId)
// ia := &ServiceAuth{
// AccountId: aad.AccountId,
// AccessToken: aad.AccessToken,
// ExpiresIn: aad.ExpiresIn,
// LastRefresh: aad.LastRefresh,
// RefreshToken: aad.RefreshToken,
// Service: datastore.NewKey(ctx, "Service", config.ServiceId, 0, nil),
// TokenType: aad.TokenType,
// }
// return key, ia
// })
// Set up server for handling incoming requests.
http.HandleFunc("/request", requestHandler)
log.Printf(". Starting server on %s...", config.ListenAddr)
if err := http.ListenAndServe(config.ListenAddr, nil); err != nil {
log.Fatalf("! Failed to serve (http.ListenAndServe: %v)", err)
}
}
type Config struct {
ListenAddr string
ProjectId string
ServiceId string
AmazonClientId string
AmazonClientSecret string
}
type ServiceAuth struct {
AccountId int64 `datastore:"-" json:"-"`
AccessToken string `datastore:"access_token,noindex" json:"access_token"`
ExpiresIn int `datastore:"expires_in,noindex" json:"expires_in"`
LastRefresh time.Time `datastore:"last_refresh" json:"-"`
RefreshToken string `datastore:"refresh_token,noindex" json:"refresh_token"`
Service *datastore.Key `datastore:"service" json:"-"`
TokenType string `datastore:"token_type,noindex" json:"token_type"`
}
func (ia *ServiceAuth) Expired() bool {
return time.Now().After(ia.LastRefresh.Add(time.Duration(ia.ExpiresIn) * time.Second))
}
func (ia *ServiceAuth) Refresh() error {
resp, err := http.PostForm("https://api.amazon.com/auth/o2/token", url.Values{
"client_id": {config.AmazonClientId},
"client_secret": {config.AmazonClientSecret},
"grant_type": {"refresh_token"},
"refresh_token": {ia.RefreshToken},
})
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(data, ia)
}
func Key(ctx context.Context, accountId int64) *datastore.Key {
return datastore.NewKey(
ctx, "ServiceAuth", fmt.Sprintf("service_%s", config.ServiceId), 0,
datastore.NewKey(ctx, "Account", "", accountId, nil))
}
func GetAuth(ctx context.Context, accountId int64) (auth *ServiceAuth, err error) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
auth = cache[accountId]
if auth == nil || auth.Expired() {
auth, err = GetAuthFromDatastore(ctx, accountId)
if err == nil {
cache[accountId] = auth
}
}
return
}
func GetAuthFromDatastore(ctx context.Context, accountId int64) (*ServiceAuth, error) {
key := Key(ctx, accountId)
var auth ServiceAuth
if err := store.Get(ctx, key, &auth); err != nil {
return nil, err
}
auth.AccountId = accountId
if auth.Expired() {
err := auth.Refresh()
if err != nil {
return nil, err
}
auth.LastRefresh = time.Now()
_, err = store.Put(ctx, key, &auth)
if err != nil {
return nil, err
}
}
return &auth, nil
}
var (
downchannels = make(map[int64]<-chan *avs.Message)
downchannelsMutex sync.Mutex
)
func downchannelThread(auth ServiceAuth, directives <-chan *avs.Message) {
log.Printf(". Started downchannel for %d", auth.AccountId)
for directive := range directives {
handleDirective(auth, directive, nil)
}
log.Printf("! Downchannel for %d closed", auth.AccountId)
downchannelsMutex.Lock()
defer downchannelsMutex.Unlock()
if downchannels[auth.AccountId] == directives {
delete(downchannels, auth.AccountId)
}
}
func ensureDownchannel(auth ServiceAuth) {
downchannelsMutex.Lock()
defer downchannelsMutex.Unlock()
if _, ok := downchannels[auth.AccountId]; ok {
// There's already an active downchannel.
return
}
ch, err := avs.DefaultClient.CreateDownchannel(auth.AccessToken)
if err != nil {
log.Printf("! Failed to create downstream for %d: %v", auth.AccountId, err)
return
}
downchannels[auth.AccountId] = ch
go downchannelThread(auth, ch)
}
type Authorizer interface {
AccessToken() string
}
type Device struct {
client *avs.Client
auth Authorizer
AlertsState *avs.AlertsState
PlaybackState *avs.PlaybackState
SpeechState *avs.SpeechState
VolumeState *avs.VolumeState
directives chan avs.TypedMessage
mx sync.Mutex
}
func (d *Device) Context() []avs.TypedMessage {
return []avs.TypedMessage{
d.AlertsState,
d.PlaybackState,
d.SpeechState,
d.VolumeState,
}
}
func (d *Device) Running() bool {
d.mx.Lock()
defer d.mx.Unlock()
return d.directives != nil
}
func (d *Device) Start() (<-chan avs.TypedMessage, error) {
d.mx.Lock()
defer d.mx.Unlock()
if d.directives != nil {
return nil, fmt.Errorf("device already started")
}
ch, err := d.client.CreateDownchannel(d.auth.AccessToken())
if err != nil {
return nil, err
}
d.directives = make(chan avs.TypedMessage)
go d.background(ch)
return d.directives, nil
}
func (d *Device) Stop() error {
d.mx.Lock()
defer d.mx.Unlock()
if d.directives == nil {
return fmt.Errorf("device already stopped")
}
close(d.directives)
d.directives = nil
return nil
}
func (d *Device) Synchronize() error {
return d.postEvent(avs.NewSynchronizeState(avs.RandomUUIDString()))
}
func (d *Device) background(ch <-chan *avs.Message) {
for msg := range ch {
if ok := d.handleDirective(msg.Typed()); !ok {
break
}
}
d.Stop()
}
func (d *Device) handleDirective(directive avs.TypedMessage) bool {
d.mx.Lock()
defer d.mx.Unlock()
if d.directives == nil {
return false
}
d.directives <- directive
return true
}
func (d *Device) postEvent(event avs.TypedMessage) error {
req := avs.NewRequest(d.auth.AccessToken())
req.Context = d.Context()
resp, err := d.client.Do(req)
if err != nil {
return err
}
for _, msg := range resp.Directives {
if ok := d.handleDirective(msg.Typed()); !ok {
return fmt.Errorf("device was stopped")
}
}
return nil
}
func NewDevice(client *avs.Client, auth Authorizer) *Device {
return &Device{
client: client, auth: auth,
AlertsState: avs.NewAlertsState([]avs.Alert{}, []avs.Alert{}),
PlaybackState: avs.NewPlaybackState("", 0, avs.PlayerActivityIdle),
SpeechState: avs.NewSpeechState("", 0, avs.PlayerActivityIdle),
VolumeState: avs.NewVolumeState(100, false),
}
}
func handleDirective(auth ServiceAuth, directive *avs.Message, resp *avs.Response) {
log.Printf("< Directive for %d: %s", auth.AccountId, directive)
log.Println(string(directive.GetMessage().Payload))
switch d := directive.Typed().(type) {
case *avs.ExpectSpeech:
log.Printf(". Alexa wants you to speak within %s!", d.Timeout())
case *avs.Play:
if cid := d.Payload.AudioItem.Stream.ContentId(); cid != "" {
save(resp, cid)
} else {
log.Printf(". Remote stream: %s", d.Payload.AudioItem.Stream.URL)
}
token := avs.RandomUUIDString()
a, b := avs.PostEvent(auth.AccessToken, avs.NewPlaybackStarted(avs.RandomUUIDString(), token, 0))
fmt.Println(a, b)
a, b = avs.PostEvent(auth.AccessToken, avs.NewPlaybackNearlyFinished(avs.RandomUUIDString(), token, 1*time.Second))
fmt.Println(a, b)
case *avs.Speak:
save(resp, d.ContentId())
case *avs.Message:
log.Printf("! No typed message for %s\n%v\n%s", d, d.Header, string(d.Payload))
default:
log.Printf(". Unhandled directive %s", d)
}
}
func requestHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("! Failed to read body: %v", err)
return
}
_ = body
audio, err := os.Open(r.URL.Query().Get("file"))
if err != nil {
log.Printf("! Failed to open audio file: %v", err)
return
}
// Get the access token for Amazon.
beforeAuth := time.Now()
auth, err := GetAuth(ctx, 5676073085829120)
if err != nil {
log.Printf("! Failed to get Alexa auth info: %v", err)
return
}
// Set up a downchannel.
go ensureDownchannel(*auth)
// Post the request to AVS.
beforeRequest := time.Now()
response, err := avs.PostRecognize(auth.AccessToken, avs.RandomUUIDString(), avs.RandomUUIDString(), audio)
if err != nil {
log.Printf("! Failed to call AVS: %v", err)
return
}
if response == nil {
log.Println(". Alexa had nothing to say.")
return
}
for _, directive := range response.Directives {
handleDirective(*auth, directive, response)
}
log.Printf(". [Fetch auth: %s] [AVS call: %s]", beforeRequest.Sub(beforeAuth), time.Since(beforeRequest))
}
var savedFiles = 0
func save(resp *avs.Response, cid string) {
savedFiles++
filename := fmt.Sprintf("./response%d.mp3", savedFiles)
ioutil.WriteFile(filename, resp.Content[cid], 0666)
log.Printf(". Saved Alexa’s response to %s", filename)
}