Sfoglia il codice sorgente

decodeing http message to xml wrapped into function

master
Patrick Peng Sun 8 anni fa
parent
commit
b0553ccff8
3 ha cambiato i file con 11 aggiunte e 9 eliminazioni
  1. +6
    -0
      inMsg.go
  2. +1
    -2
      outMsg_test.go
  3. +4
    -7
      server.go

+ 6
- 0
inMsg.go Vedi File

xml.Unmarshal([]byte(s), &r) xml.Unmarshal([]byte(s), &r)
return r.Event return r.Event
} }

func decryptToXML(raw string) string {
e := ReadEncryptedMsg(raw)
sd := Decode(e.Encrypt)
return sd
}

+ 1
- 2
outMsg_test.go Vedi File

func TestBuildTxtMsg(t *testing.T) { func TestBuildTxtMsg(t *testing.T) {
SetupConfig() SetupConfig()
s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE") s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE")
e := ReadEncryptedMsg(s)
sd := Decode(e.Encrypt)
sd := decryptToXML(s)
h := ReadCommonHeader(sd) h := ReadCommonHeader(sd)
m := ReadTextMsg(sd) m := ReadTextMsg(sd)
AssertEqual(t, m.Content, "你好", "Message content => wrong") AssertEqual(t, m.Content, "你好", "Message content => wrong")

+ 4
- 7
server.go Vedi File



switch r.Method { switch r.Method {
case "POST": case "POST":
answerWechatPostEcho(w, r)
//answerWechatPost(w, r)
//answerWechatPostEcho(w, r)
answerWechatPost(w, r)
case "GET": case "GET":
answerInitialAuth(w, r) answerInitialAuth(w, r)
default: default:
// //
func answerWechatPost(w http.ResponseWriter, r *http.Request) { func answerWechatPost(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body) 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) h := ReadCommonHeader(d)
reply, _ := BuildTextMsg(h.MsgType, h.FromUserName) reply, _ := BuildTextMsg(h.MsgType, h.FromUserName)
if h.MsgType == "voice" { if h.MsgType == "voice" {

Loading…
Annulla
Salva