Parcourir la source

EventMsg designed as an entire package

master
Patrick Peng Sun il y a 8 ans
Parent
révision
5209362587
3 fichiers modifiés avec 44 ajouts et 0 suppressions
  1. +27
    -0
      inMsg.go
  2. +13
    -0
      inMsg_test.go
  3. +4
    -0
      server.go

+ 27
- 0
inMsg.go Voir le fichier

@@ -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
}

+ 13
- 0
inMsg_test.go Voir le fichier

@@ -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 := `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>
<FromUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></FromUserName>
<CreateTime>1493903506</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[MEMBER_SEARCH]]></EventKey>
</xml>`
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&timestamp=1493303819&nonce=1367712073&openid=oUN420bxqFqlx0ZQHciUOesZO3PE&encrypt_type=aes&msg_signature=46331890d4c95a017a014a009ccc7f37228665db HTTP/1.1

+ 4
- 0
server.go Voir le fichier

@@ -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

Chargement…
Annuler
Enregistrer