From 52093625879e008e1c727fcdbfa134ea002ad351 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Fri, 5 May 2017 01:40:04 +1000 Subject: [PATCH] EventMsg designed as an entire package --- inMsg.go | 27 +++++++++++++++++++++++++++ inMsg_test.go | 13 +++++++++++++ server.go | 4 ++++ 3 files changed, 44 insertions(+) diff --git a/inMsg.go b/inMsg.go index 98ab297..1a19459 100644 --- a/inMsg.go +++ b/inMsg.go @@ -70,6 +70,16 @@ type LinkMsg struct { MsgId int64 } +//EventMsg all type of event +type EventMsg struct { + Event string //subscribe, unsubscribe, SCAN, LOCATION, CLICK, VIEW + EventKey string //for CLICK, VIEW, SCAN, subscribe + Ticket string //for SCAN, subscribe + Latitude float64 //for LOCATION + Longitude float64 //for LOCATION + Precision float64 //for LOCATION +} + //ReadCommonHeader parse xml of common field of wechat post message func ReadCommonHeader(s string) CommonHeader { var r = CommonHeader{} @@ -132,3 +142,20 @@ func ReadEncryptedMsg(s string) EncryptedMsg { xml.Unmarshal([]byte(s), &r) return r } + +//ReadEventMsg get event msg details +func ReadEventMsg(s string) EventMsg { + var r = EventMsg{} + xml.Unmarshal([]byte(s), &r) + return r +} + +//ReadEventType tells event type in string +// subscribe +// unsubscribe +// scan +func ReadEventType(s string) string { + var r = EventMsg{} + xml.Unmarshal([]byte(s), &r) + return r.Event +} diff --git a/inMsg_test.go b/inMsg_test.go index 5d89013..cad8e02 100644 --- a/inMsg_test.go +++ b/inMsg_test.go @@ -177,6 +177,19 @@ func TestReadEncryptMsg(t *testing.T) { AssertEqual(t, m.Encrypt, "Dv3epMMhmmGU1o6lg71IfbpRrOYX1S8oZX3nwW0uBAHHMKx62T4KniS4efuf8fNHWf6gsF/YGaDraF6HhGOdKp8vbzluiIEsCnIveKN1pO+IUDOBBxzPAzQSFSYJ3OwVXWmBdBcC1S5guQrOxLysH+6UIWSor9cEef+94UAKTNw/MLB0zPfqK5TVoN1A0yobmP9OU8wtFJP0L1aKySPFGGbqBMfJkStRTrYLjIQfZ7pAIisB/g3c87w26r7LUz9hVh4ey3/T6cjQ8vKvgNKL3j8y4IwUdmnmTPrrdOsyA1pz69977xKHFtIptZYHKGD9dTW6PyPcKKTP6iOod6Agb8TI+is80auqHkjvUyvT/xPG8fxak/wI9BKzKndAnwxlcDG/8WElkHVl0TwxpsCb48ZxLEf4GFKaYaliC9xBVweKLNnqdbBmzwfe7GBNWC61h7KQYqwtZqMkZs3BBsStcQ==", "ToUserName failed") } +func TestReadEventMsg(t *testing.T) { + msg := ` + +1493903506 + + + +` + m := ReadEventMsg(msg) + AssertEqual(t, m.Event, "CLICK", "Expect Click event") + AssertEqual(t, m.EventKey, "MEMBER_SEARCH", "Expect MEMBER_SEARCH") +} + func TestAcceptLocationInfo(t *testing.T) { // POST /api?signature=1a9991fc0d1916495e26e07dca7d2c305c529b0d×tamp=1493303819&nonce=1367712073&openid=oUN420bxqFqlx0ZQHciUOesZO3PE&encrypt_type=aes&msg_signature=46331890d4c95a017a014a009ccc7f37228665db HTTP/1.1 diff --git a/server.go b/server.go index 7c75bc4..73f0916 100644 --- a/server.go +++ b/server.go @@ -65,6 +65,10 @@ func answerWechatPost(w http.ResponseWriter, r *http.Request) { a := ReadVoiceMsg(d) reply, _ = BuildTextMsg(a.Recognition, h.FromUserName) } + if h.MsgType == "event" { + a := ReadEventMsg(d) + reply, _ = BuildTextMsg(a.Event+"/"+a.EventKey, h.FromUserName) + } w.Header().Set("Content-Type", "text/xml; charset=utf-8") fmt.Fprint(w, reply) return