diff --git a/go-wecomchan/go.mod b/go-wecomchan/go.mod index 388cf93..88016bf 100644 --- a/go-wecomchan/go.mod +++ b/go-wecomchan/go.mod @@ -3,3 +3,4 @@ module go/wecomchan go 1.16 require github.com/go-redis/redis/v8 v8.10.0 +require github.com/julienschmidt/httprouter v1.3.0 diff --git a/go-wecomchan/go.sum b/go-wecomchan/go.sum index 97595ea..44249e0 100644 --- a/go-wecomchan/go.sum +++ b/go-wecomchan/go.sum @@ -22,6 +22,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index d808b7a..f68e728 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -12,9 +12,11 @@ import ( "net/http" "os" "reflect" + "strings" "time" "github.com/go-redis/redis/v8" + "github.com/julienschmidt/httprouter" ) /*------------------------------- 环境变量配置 begin -------------------------------*/ @@ -236,22 +238,48 @@ func InitJsonData(msgType string) JsonData { func main() { // 设置日志内容显示文件名和行号 log.SetFlags(log.LstdFlags | log.Lshortfile) - wecomChan := func(res http.ResponseWriter, req *http.Request) { - // 获取token - accessToken := GetAccessToken() - // 默认token有效 - tokenValid := true - + wecomChan := func(res http.ResponseWriter, req *http.Request, ps httprouter.Params) { _ = req.ParseForm() - sendkey := req.FormValue("sendkey") + sendkey := req.Form.Get("sendkey") + urlpath := ps.ByName("urlpath") + if len(sendkey) == 0 && len(urlpath) > 6 && urlpath != "wecomchan" { + sendkey = urlpath[1 : len(urlpath)-5] + } if sendkey != Sendkey { log.Panicln("sendkey 错误,请检查") } - msgContent := req.FormValue("msg") - msgType := req.FormValue("msg_type") + msgContent := req.Form.Get("msg") + if len(msgContent) == 0 { + textArray := make([]string, 3) + lenOfArr := 0 + title := req.Form.Get("title") + if (len(title)) > 0 { + textArray[lenOfArr] = title + lenOfArr++ + } + text := req.Form.Get("text") + if (len(text)) > 0 { + textArray[lenOfArr] = text + lenOfArr++ + } + desp := req.Form.Get("desp") + if (len(desp)) > 0 { + textArray[lenOfArr] = desp + lenOfArr++ + } + msgContent = strings.Join(textArray[0:lenOfArr], "\n") + } + msgType := req.Form.Get("msg_type") + if len(msgType) == 0 { + msgType = "text" + } log.Println("mes_type=", msgType) // 默认mediaId为空 mediaId := "" + // 获取token + accessToken := GetAccessToken() + // 默认token有效 + tokenValid := true if msgType != "image" { log.Println("消息类型不是图片") } else { @@ -297,6 +325,8 @@ func main() { res.Header().Set("Content-type", "application/json") _, _ = res.Write([]byte(postStatus)) } - http.HandleFunc("/wecomchan", wecomChan) - log.Fatal(http.ListenAndServe(":8080", nil)) + router := httprouter.New() + router.GET("/*urlpath", wecomChan) + router.POST("/*urlpath", wecomChan) + log.Fatal(http.ListenAndServe(":8080", router)) }