这个不是全部的代码哦,只是一个演示可以验证跟接受post传过来的消息并且能返回消息,中间的回复逻辑就待需要各位同志们自己写了哈
复制代码 代码如下:
/*
*@go语言实现公众平台
*/
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 := `"欢迎关注
我的微信"`
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 = "你的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)
}
Go语言,微信公众平台
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]