Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

适配POST模式传参,兼容ServerChan模式传参 #36

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go-wecomchan/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions go-wecomchan/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
52 changes: 41 additions & 11 deletions go-wecomchan/wecomchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"net/http"
"os"
"reflect"
"strings"
"time"

"github.com/go-redis/redis/v8"
"github.com/julienschmidt/httprouter"
)

/*------------------------------- 环境变量配置 begin -------------------------------*/
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}