This is not all code Oh, just a demo can verify and accept the post passed the message and can return the message, the middle of the reply to the logic of the need to wait for your comrades to write their own AH
Copy Code code as follows:
/*
* @go language to achieve public platform
*/
Package Main
Import (
"CRYPTO/SHA1"
"Encoding/xml"
"FMT"
"IO"
"Io/ioutil"
"Log"
"Net/http"
"Sort"
"Strings"
"Time"
)
Type Request struct {
Tousername string
Fromusername string
Createtime time. Duration
Msgtype string
Content string
MsgId int
}
Type Response struct {
Tousername string ' xml: ' Xml>tousername '
Fromusername string ' xml: ' Xml>fromusername '
Createtime string ' xml: ' Xml>createtime '
Msgtype string ' xml: ' Xml>msgtype '
Content string ' xml: ' Xml>content '
MsgId int ' xml: ' Xml>msgid '
}
Func str2sha1 (data string) string {
T: = SHA1. New ()
Io. WriteString (t, data)
Return FMT. Sprintf ("%x", T.sum (nil))
}
Func Action (w http. Responsewriter, R *http. Request) {
Postedmsg, err: = Ioutil. ReadAll (R.body)
If Err!= nil {
Log. Fatal (ERR)
}
R.body.close ()
V: = request{}
Xml. Unmarshal (postedmsg, &v)
if V.msgtype = = "Text" {
V: = Request{v.tousername, V.fromusername, V.createtime, V.msgtype, V.content, V.msgid}
Output, err: = XML. Marshalindent (V, "", "")
If Err!= nil {
Fmt. Printf ("error:%v\n", err)
}
Fmt. fprintf (w, string (output))
else if V.msgtype = = "Event" {
Content: = ' "Welcome attention
My micro-letter "'
V: = Request{v.tousername, V.fromusername, V.createtime, V.msgtype, Content, V.msgid}
Output, err: = XML. Marshalindent (V, "", "")
If Err!= nil {
Fmt. Printf ("error:%v\n", err)
}
Fmt. fprintf (w, string (output))
}
}
Func checksignature (w http. Responsewriter, R *http. Request) {
R.parseform ()
var token string = "Your token"
var signature string = strings. Join (r.form["signature"], "")
var timestamp string = strings. Join (r.form["timestamp"], "")
var nonce string = strings. Join (r.form["nonce"], "")
var echostr string = strings. Join (r.form["Echostr"], "")
Tmps: = []string{token, timestamp, nonce}
Sort. Strings (Tmps)
TMPSTR: = Tmps[0] + tmps[1] + tmps[2]
TMP: = STR2SHA1 (TMPSTR)
if tmp = = Signature {
Fmt. fprintf (W, ECHOSTR)
}
}
Func Main () {
http. Handlefunc ("/check", Checksignature)
http. Handlefunc ("/", action)
http. Listenandserve (": 8080", nil)
}