From 8531e09d47518b5f0d8c89e0e7481804e4b7b9ce Mon Sep 17 00:00:00 2001 From: liudong Date: Tue, 13 Sep 2022 15:31:07 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=96=B0=E5=A2=9E=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=9C=BA=E5=99=A8=E4=BA=BA=E5=8F=91=E7=94=9F?= =?UTF-8?q?=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- clients/wechat/client.go | 23 ++++++++++++++ clients/wechat/comment.go | 64 +++++++++++++++++++++++++++++++++++++++ clients/wechat/robot.go | 46 ++++++++++++++++++++++++++++ go.mod | 1 + 5 files changed, 136 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 723ef36..5817819 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +*_test.go \ No newline at end of file diff --git a/clients/wechat/client.go b/clients/wechat/client.go index 38c7937..dcd3620 100644 --- a/clients/wechat/client.go +++ b/clients/wechat/client.go @@ -41,7 +41,30 @@ func (c *weChatClient) Get(fullPath string, param, header map[string]string) (re return } } + if param == nil { + param = map[string]string{} + } + param["access_token"] = c.AccessToken + return c.HttpClient.Get(fullPath, param, header) +} +func (c *weChatClient) Post(fullPath string, param, header map[string]string, body map[string]interface{}) (response []byte, err error) { + if time.Now().Unix() > c.TokenExpireTime { + err = c.Auth() + if err != nil { + err = errors.WithMessage(err, "auth failed") + return + } + } + if param == nil { + param = map[string]string{} + } + param["access_token"] = c.AccessToken + if header == nil { + header = map[string]string{} + } + header["Content-Type"] = thttp.ContentJson + return c.HttpClient.Post(fullPath, param, header, body) } // 重新获取token diff --git a/clients/wechat/comment.go b/clients/wechat/comment.go index ecd4201..e197dc6 100644 --- a/clients/wechat/comment.go +++ b/clients/wechat/comment.go @@ -1 +1,65 @@ package wechat + +import ( + "encoding/json" + "github.com/pkg/errors" + "github.com/tidwall/gjson" + "strconv" +) + +type WeChatUser struct { + Name string `json:"name"` + Department []int `json:"department"` + Userid string `json:"userid"` +} + +// 获取部门下的成员 +//department id +// fetchChild 是否查找子部门 +func (c *weChatClient) GetUsersByDepartmentID(departmentId int, fetchChild bool) (users []*WeChatUser, err error) { + url := "/cgi-bin/user/simplelist" + fetch_child := "0" + if fetchChild { + fetch_child = "1" + } + param := map[string]string{ + "department_id": strconv.Itoa(departmentId), + "fetch_child": fetch_child, + } + response, err := c.Get(url, param, nil) + if err != nil { + err = errors.WithStack(err) + return + } + responseStr := string(response) + if gjson.Get(responseStr, "errcode").Int() != 0 { + err = errors.Errorf("get department failed:%v", responseStr) + return + } + userStr := gjson.Get(responseStr, "userlist").String() + users = make([]*WeChatUser, 0) + err = json.Unmarshal([]byte(userStr), &users) + if err != nil { + err = errors.Errorf("get department failed:%+v", errors.WithStack(err)) + } + return +} + +// 通过名字查询用户ID +// departmentId 部门ID +// fetchChild 是否查找子部门 +func (c *weChatClient) GetUserIDbyName(userName string, departmentId int, fetchChild bool) (userId string, err error) { + + users, err := c.GetUsersByDepartmentID(departmentId, fetchChild) + if err != nil { + err = errors.WithStack(err) + return + } + for _, u := range users { + if u.Name == userName { + userId = u.Userid + return + } + } + return +} diff --git a/clients/wechat/robot.go b/clients/wechat/robot.go index ecd4201..53cc306 100644 --- a/clients/wechat/robot.go +++ b/clients/wechat/robot.go @@ -1 +1,47 @@ package wechat + +import ( + client "github.com/DolphinDong/gotools/http" + "github.com/mitchellh/mapstructure" + "github.com/pkg/errors" + "github.com/tidwall/gjson" +) + +const ( + robotUrl = "/cgi-bin/webhook/send" +) + +type RobotMessage struct { + MsgType string `mapstructure:"msgtype"` + Text struct { + Content string `mapstructure:"content"` + MentionedList []string `mapstructure:"mentioned_list"` + MentionedMobileList []string `mapstructure:"mentioned_mobile_list"` + } `mapstructure:"text"` +} + +// 发送机器人消息 +func SendRobotMessage(robotKey, weChatUrl string, message RobotMessage) error { + c := client.Client{BaseUrl: weChatUrl} + parm := map[string]string{"key": robotKey} + header := map[string]string{"Content-Type": client.ContentJson} + + msg := map[string]interface{}{} + err := mapstructure.Decode(message, &msg) + if err != nil { + err = errors.WithStack(err) + return err + } + + response, err := c.Post(robotUrl, parm, header, msg) + if err != nil { + err = errors.WithStack(err) + return err + } + responseStr := string(response) + if gjson.Get(responseStr, "errcode").Int() != 0 { + err = errors.Errorf("send robot message failed:%v", responseStr) + return err + } + return nil +} diff --git a/go.mod b/go.mod index 1780385..5357cf6 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/go-playground/validator/v10 v10.11.0 + github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 github.com/tidwall/gjson v1.14.3 )