Skip to content

Commit

Permalink
add: 新增企业微信机器人发生消息
Browse files Browse the repository at this point in the history
  • Loading branch information
liudong committed Sep 13, 2022
1 parent aabadc4 commit 8531e09
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
*_test.go
23 changes: 23 additions & 0 deletions clients/wechat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions clients/wechat/comment.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions clients/wechat/robot.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 8531e09

Please sign in to comment.