ailistener

git clone git://archive.git.mtrnord.blog/OpenHomeAuto/ailistener.git
Log | Files | Refs | LICENSE

commit ab790e7371825c1c14a863adeb7480ab9a188aca
parent 17b675152b1d4d8f5a97da6bd83044c39353cb8f
Author: MTRNord <mtrnord1@gmail.com>
Date:   Thu, 28 Jun 2018 21:58:21 +0200

Check if SignIn is required

Diffstat:
M.idea/usage.statistics.xml | 8++++----
Mpkg/webhook/webhook.go | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 80 insertions(+), 8 deletions(-)

diff --git a/.idea/usage.statistics.xml b/.idea/usage.statistics.xml @@ -38,14 +38,14 @@ <session id="958628364"> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="go" value="631" /> - <entry key="txt" value="337" /> + <entry key="go" value="755" /> + <entry key="txt" value="354" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> - <entry key="Go" value="631" /> - <entry key="PLAIN_TEXT" value="337" /> + <entry key="Go" value="755" /> + <entry key="PLAIN_TEXT" value="354" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.open"> diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go @@ -71,6 +71,48 @@ func Start() { } } +type OriginalResp struct { + Source string `json:"source"` + Version string `json:"version"` + Payload struct { + IsInSandbox bool `json:"isInSandbox"` + Surface struct { + Capabilities []struct { + Name string `json:"name"` + } `json:"capabilities"` + } `json:"surface"` + RequestType string `json:"requestType"` + Inputs []struct { + RawInputs []struct { + Query string `json:"query"` + InputType string `json:"inputType"` + } `json:"rawInputs"` + Arguments []struct { + RawText string `json:"rawText"` + TextValue string `json:"textValue"` + Name string `json:"name"` + } `json:"arguments"` + Intent string `json:"intent"` + } `json:"inputs"` + User struct { + LastSeen time.Time `json:"lastSeen"` + Locale string `json:"locale"` + UserID string `json:"userId"` + AccessToken string `json:"accessToken"` + } `json:"user"` + Conversation struct { + ConversationID string `json:"conversationId"` + Type string `json:"type"` + ConversationToken string `json:"conversationToken"` + } `json:"conversation"` + AvailableSurfaces []struct { + Capabilities []struct { + Name string `json:"name"` + } `json:"capabilities"` + } `json:"availableSurfaces"` + } `json:"payload"` +} + func MessagesEndPoint(rw http.ResponseWriter, req *http.Request) { var err error var dfr *df.Request @@ -100,18 +142,46 @@ func MessagesEndPoint(rw http.ResponseWriter, req *http.Request) { case "play_music": log.Println(dfr.QueryResult) - resp := dflow.DoSignIn() + var oresp *OriginalResp + if err = json.Unmarshal(dfr.OriginalDetectIntentRequest, &oresp); err != nil { + rw.WriteHeader(http.StatusBadRequest) + return + } + + if oresp.Payload.User.AccessToken == "" { + resp := dflow.DoSignIn() + // Do things with the context you just retrieved + dff := &df.Fulfillment{ + FollowupEventInput: resp, + } + + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(http.StatusOK) + json.NewEncoder(rw).Encode(dff) + + return + } + // Do things with the context you just retrieved dff := &df.Fulfillment{ - FollowupEventInput: resp, + FulfillmentMessages: df.Messages{ + df.ForGoogle(df.SingleSimpleResponse("Starting Music", "Starting Music")), + {RichMessage: df.Text{Text: []string{"Starting Music"}}}, + }, } rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(http.StatusOK) json.NewEncoder(rw).Encode(dff) + return case "auth": - OrespJ, _ := dfr.OriginalDetectIntentRequest.MarshalJSON() - log.Println("OrespJ: ", string(OrespJ)) + var oresp *OriginalResp + if err = json.Unmarshal(dfr.OriginalDetectIntentRequest, &oresp); err != nil { + rw.WriteHeader(http.StatusBadRequest) + return + } + + log.Printf("OrespJ: \n %+v\n", oresp) respPJ, _ := dfr.QueryResult.Parameters.MarshalJSON() log.Println("respPJ: ", string(respPJ)) for _, v := range dfr.QueryResult.OutputContexts { @@ -143,6 +213,7 @@ func MessagesEndPoint(rw http.ResponseWriter, req *http.Request) { rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(http.StatusOK) json.NewEncoder(rw).Encode(dff) + return default: log.Println(dfr.QueryResult) @@ -155,6 +226,7 @@ func MessagesEndPoint(rw http.ResponseWriter, req *http.Request) { rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(http.StatusOK) json.NewEncoder(rw).Encode(dff) + return } }