-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liudong
committed
Sep 13, 2022
1 parent
aabadc4
commit 8531e09
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
.idea | ||
*_test.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters