From 776028316da7013c1a2ce83edeb7c5c108fbc54b Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Tue, 24 Sep 2019 16:56:00 +1000 Subject: [PATCH] init project --- go.mod | 19 +++++++ go.sum | 77 ++++++++++++++++++++++++++ main.go | 31 +++++++++++ messages/entry.go | 81 ++++++++++++++++++++++++++++ sample-message.json | 129 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 337 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 messages/entry.go create mode 100644 sample-message.json diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..92a4025 --- /dev/null +++ b/go.mod @@ -0,0 +1,19 @@ +module github.com/wahyd4/push + +go 1.13 + +require ( + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.4.0 + github.com/golang/protobuf v1.3.2 // indirect + github.com/json-iterator/go v1.1.7 // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect + github.com/kr/pretty v0.1.0 // indirect + github.com/mattn/go-isatty v0.0.9 // indirect + github.com/sirupsen/logrus v1.4.2 + github.com/stretchr/testify v1.4.0 // indirect + github.com/ugorji/go v1.1.7 // indirect + golang.org/x/net v0.0.0-20190923162816-aa69164e4478 // indirect + golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7f363d5 --- /dev/null +++ b/go.sum @@ -0,0 +1,77 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ= +github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo= +github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 h1:rOhMmluY6kLMhdnrivzec6lLgaVbMHMn2ISQXJeJ5EM= +golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9fb9298 --- /dev/null +++ b/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "io/ioutil" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/wahyd4/push/messages" +) + +func main() { + r := gin.Default() + r.GET("/ping", func(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "pong", + }) + }) + + r.POST("/api/push", func(c *gin.Context) { + msg := messages.ActionMessage{} + bytes, err := ioutil.ReadAll(c.Request.Body) + if err != nil { + logrus.Errorf(err.Error()) + } + fmt.Println(string(bytes)) + _ = c.BindJSON(&msg) + logrus.Info(msg) + }) + r.Run() +} diff --git a/messages/entry.go b/messages/entry.go new file mode 100644 index 0000000..e741c8a --- /dev/null +++ b/messages/entry.go @@ -0,0 +1,81 @@ +package messages + +import "time" + +type ActionMessage struct { + ResponseID string `json:"responseId"` + QueryResult *QueryResult `json:"queryResult"` + OriginalDetectIntentRequest *OriginalDetectIntentRequest `json:"originalDetectIntentRequest"` + Session string `json:"session"` +} + +type QueryResult struct { + QueryText string `json:"queryText"` + Parameters struct { + Location string `json:"location"` + Userid string `json:"userid"` + } `json:"parameters"` + FulfillmentText string `json:"fulfillmentText"` + FulfillmentMessages []struct { + Text struct { + Text []string `json:"text"` + } `json:"text"` + } `json:"fulfillmentMessages"` + OutputContexts []struct { + Name string `json:"name"` + LifespanCount int `json:"lifespanCount"` + Parameters struct { + Location string `json:"location"` + LocationOriginal string `json:"location.original"` + Userid string `json:"userid"` + UseridOriginal string `json:"userid.original"` + } `json:"parameters"` + } `json:"outputContexts"` + Intent struct { + Name string `json:"name"` + DisplayName string `json:"displayName"` + } `json:"intent"` + IntentDetectionConfidence float64 `json:"intentDetectionConfidence"` + LanguageCode string `json:"languageCode"` +} + +type OriginalDetectIntentRequest struct { + Source string `json:"source"` + Version string `json:"version"` + Payload struct { + User struct { + Locale string `json:"locale"` + LastSeen time.Time `json:"lastSeen"` + UserVerificationStatus string `json:"userVerificationStatus"` + } `json:"user"` + Conversation struct { + ConversationID string `json:"conversationId"` + Type string `json:"type"` + ConversationToken string `json:"conversationToken"` + } `json:"conversation"` + Inputs []struct { + Intent string `json:"intent"` + RawInputs []struct { + InputType string `json:"inputType"` + Query string `json:"query"` + } `json:"rawInputs"` + Arguments []struct { + Name string `json:"name"` + RawText string `json:"rawText"` + TextValue string `json:"textValue"` + } `json:"arguments"` + } `json:"inputs"` + Surface struct { + Capabilities []struct { + Name string `json:"name"` + } `json:"capabilities"` + } `json:"surface"` + IsInSandbox bool `json:"isInSandbox"` + AvailableSurfaces []struct { + Capabilities []struct { + Name string `json:"name"` + } `json:"capabilities"` + } `json:"availableSurfaces"` + RequestType string `json:"requestType"` + } `json:"payload"` +} diff --git a/sample-message.json b/sample-message.json new file mode 100644 index 0000000..b7f49a8 --- /dev/null +++ b/sample-message.json @@ -0,0 +1,129 @@ +{ + "responseId": "5ee9846b-5fc6-43f0-9028-a2a49773134e-594de0a7", + "queryResult": { + "queryText": "air quality for beijing", + "parameters": { + "location": "Beijing", + "userid": "" + }, + "fulfillmentText": "What is the userid?", + "fulfillmentMessages": [ + { + "text": { + "text": [ + "What is the userid?" + ] + } + } + ], + "outputContexts": [ + { + "name": "projects/push-ab43b/agent/sessions/ABwppHGVhyNUp4gLGB1p2iC_dC0GFQX3UNH8FyHP5FkcFo-ehjFl-Co4t9Wue7VXmO2-JbLdSj1OzQ/contexts/ebd77175-1feb-4b7d-931a-70dd7889e09f_id_dialog_context", + "lifespanCount": 2, + "parameters": { + "location": "Beijing", + "location.original": "beijing", + "userid": "", + "userid.original": "" + } + }, + { + "name": "projects/push-ab43b/agent/sessions/ABwppHGVhyNUp4gLGB1p2iC_dC0GFQX3UNH8FyHP5FkcFo-ehjFl-Co4t9Wue7VXmO2-JbLdSj1OzQ/contexts/actions_intent_check_air_quality_dialog_context", + "lifespanCount": 2, + "parameters": { + "location": "Beijing", + "location.original": "beijing", + "userid": "", + "userid.original": "" + } + }, + { + "name": "projects/push-ab43b/agent/sessions/ABwppHGVhyNUp4gLGB1p2iC_dC0GFQX3UNH8FyHP5FkcFo-ehjFl-Co4t9Wue7VXmO2-JbLdSj1OzQ/contexts/actions_intent_check_air_quality_dialog_params_userid", + "lifespanCount": 1, + "parameters": { + "location": "Beijing", + "location.original": "beijing", + "userid": "", + "userid.original": "" + } + } + ], + "intent": { + "name": "projects/push-ab43b/agent/intents/ebd77175-1feb-4b7d-931a-70dd7889e09f", + "displayName": "actions.intent.CHECK_AIR_QUALITY" + }, + "intentDetectionConfidence": 0.7717451, + "languageCode": "en" + }, + "originalDetectIntentRequest": { + "source": "google", + "version": "2", + "payload": { + "user": { + "locale": "en-US", + "lastSeen": "2019-09-24T06:46:20Z", + "userVerificationStatus": "VERIFIED" + }, + "conversation": { + "conversationId": "ABwppHGVhyNUp4gLGB1p2iC_dC0GFQX3UNH8FyHP5FkcFo-ehjFl-Co4t9Wue7VXmO2-JbLdSj1OzQ", + "type": "ACTIVE", + "conversationToken": "[\"ebd77175-1feb-4b7d-931a-70dd7889e09f_id_dialog_context\",\"actions_intent_check_air_quality_dialog_context\",\"actions_intent_check_air_quality_dialog_params_userid\"]" + }, + "inputs": [ + { + "intent": "actions.intent.TEXT", + "rawInputs": [ + { + "inputType": "KEYBOARD", + "query": "air quality for beijing" + } + ], + "arguments": [ + { + "name": "text", + "rawText": "air quality for beijing", + "textValue": "air quality for beijing" + } + ] + } + ], + "surface": { + "capabilities": [ + { + "name": "actions.capability.AUDIO_OUTPUT" + }, + { + "name": "actions.capability.SCREEN_OUTPUT" + }, + { + "name": "actions.capability.WEB_BROWSER" + }, + { + "name": "actions.capability.ACCOUNT_LINKING" + }, + { + "name": "actions.capability.MEDIA_RESPONSE_AUDIO" + } + ] + }, + "isInSandbox": true, + "availableSurfaces": [ + { + "capabilities": [ + { + "name": "actions.capability.WEB_BROWSER" + }, + { + "name": "actions.capability.AUDIO_OUTPUT" + }, + { + "name": "actions.capability.SCREEN_OUTPUT" + } + ] + } + ], + "requestType": "SIMULATOR" + } + }, + "session": "projects/push-ab43b/agent/sessions/ABwppHGVhyNUp4gLGB1p2iC_dC0GFQX3UNH8FyHP5FkcFo-ehjFl-Co4t9Wue7VXmO2-JbLdSj1OzQ" +}