-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
66 lines (60 loc) · 1.52 KB
/
google.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
package main
import (
"encoding/json"
"net/http"
"os"
"github.com/saphoooo/AlexaSkills/google"
)
// actionsNewTextToSpeechResponse marshals a text in Google ActionsResponse format
func actionsNewTextToSpeechResponse(text string) ([]byte, error) {
item := &google.Items{
SimpleResponse: &google.SimpleResponse{
TextToSpeech: text,
DisplayText: text,
},
}
items := []*google.Items{item}
action := &google.ActionsResponse{
GPayload: &google.GPayload{
Google: &google.Google{
RichResponse: &google.RichResponse{
Items: items,
},
},
},
}
resp, err := json.Marshal(action)
if err != nil {
return nil, err
}
return resp, nil
}
type googleCookingResponse struct{}
// sendCookingResponse returns the search results to Actions on Google API
func (g googleCookingResponse) sendCookingResponse(cr *cookingResponse) error {
text, err := resultsToText(getCookingInstructionIntent(cr.Params))
if err != nil {
return err
}
resp, err := actionsNewTextToSpeechResponse(text)
if err != nil {
return err
}
err = keepCookingParams(cr.Pool, cr.Key, cr.Params)
if err != nil {
return err
}
jsonReply(cr.ResponseWriter, resp)
return nil
}
// NewActionsVerifier ...
func NewActionsVerifier(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
secret := r.Header.Get("superSecret")
if secret != os.Getenv("ZGUINGOUS_KITCHEN") {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
})
}