From b0553ccff8c81d79c00d6a4eb13615dd468f4057 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sun, 7 May 2017 12:22:07 +1000 Subject: [PATCH] decodeing http message to xml wrapped into function --- inMsg.go | 6 ++++++ outMsg_test.go | 3 +-- server.go | 11 ++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/inMsg.go b/inMsg.go index 1a19459..dcdfa3a 100644 --- a/inMsg.go +++ b/inMsg.go @@ -159,3 +159,9 @@ func ReadEventType(s string) string { xml.Unmarshal([]byte(s), &r) return r.Event } + +func decryptToXML(raw string) string { + e := ReadEncryptedMsg(raw) + sd := Decode(e.Encrypt) + return sd +} diff --git a/outMsg_test.go b/outMsg_test.go index fc8228b..9cd4f07 100644 --- a/outMsg_test.go +++ b/outMsg_test.go @@ -5,8 +5,7 @@ import "testing" func TestBuildTxtMsg(t *testing.T) { SetupConfig() s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE") - e := ReadEncryptedMsg(s) - sd := Decode(e.Encrypt) + sd := decryptToXML(s) h := ReadCommonHeader(sd) m := ReadTextMsg(sd) AssertEqual(t, m.Content, "你好", "Message content => wrong") diff --git a/server.go b/server.go index 3dca6d8..e687cd8 100644 --- a/server.go +++ b/server.go @@ -26,8 +26,8 @@ func apiV1Main(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": - answerWechatPostEcho(w, r) - //answerWechatPost(w, r) + //answerWechatPostEcho(w, r) + answerWechatPost(w, r) case "GET": answerInitialAuth(w, r) default: @@ -55,11 +55,8 @@ func answerInitialAuth(w http.ResponseWriter, r *http.Request) { // func answerWechatPost(w http.ResponseWriter, r *http.Request) { body, _ := ioutil.ReadAll(r.Body) - //fmt.Printf("get body: %s", string(body)) - s := ReadEncryptedMsg(string(body)) - //fmt.Printf("to decrypt %s", s.Encrypt) - d := Decode(s.Encrypt) - fmt.Printf("decrypt as: %s", d) + d := decryptToXML(string(body)) + fmt.Printf("decrypt as: %s\n", d) h := ReadCommonHeader(d) reply, _ := BuildTextMsg(h.MsgType, h.FromUserName) if h.MsgType == "voice" {