diff --git a/bot_test.go b/bot_test.go index 8a69621..8ad73be 100644 --- a/bot_test.go +++ b/bot_test.go @@ -2,6 +2,7 @@ package openwechat import ( "testing" + "time" ) func TestLogin(t *testing.T) { @@ -85,3 +86,27 @@ func TestGroups(t *testing.T) { } t.Log(groups) } + +func TestPinUser(t *testing.T) { + bot := DefaultBot(Desktop) + if err := bot.Login(); err != nil { + t.Error(err) + return + } + user, err := bot.GetCurrentUser() + if err != nil { + t.Error(err) + return + } + friends, err := user.Friends() + if err != nil { + t.Error(err) + return + } + if friends.Count() > 0 { + f := friends.First() + f.Pin() + time.Sleep(time.Second * 5) + f.UnPin() + } +} diff --git a/caller.go b/caller.go index f78549b..798dd17 100644 --- a/caller.go +++ b/caller.go @@ -336,6 +336,15 @@ func (c *Caller) WebWxStatusAsRead(request *BaseRequest, info *LoginInfo, msg *M return parseBaseResponseError(resp) } +// WebWxRelationPin 将联系人是否置顶 +func (c *Caller) WebWxRelationPin(request *BaseRequest, user *User, op uint8) error { + resp, err := c.Client.WebWxRelationPin(request, op, user) + if err != nil { + return err + } + return parseBaseResponseError(resp) +} + // 处理响应返回的结果是否正常 func parseBaseResponseError(resp *http.Response) error { defer resp.Body.Close() diff --git a/client.go b/client.go index 8260eee..2203496 100644 --- a/client.go +++ b/client.go @@ -674,3 +674,19 @@ func (c *Client) WebWxStatusAsRead(request *BaseRequest, info *LoginInfo, msg *M req.Header.Add("Content-Type", jsonContentType) return c.Do(req) } + +// WebWxRelationPin 联系人置顶接口 +func (c *Client) WebWxRelationPin(request *BaseRequest, op uint8, user *User) (*http.Response, error) { + path, _ := url.Parse(c.domain.BaseHost() + webwxoplog) + content := map[string]interface{}{ + "BaseRequest": request, + "CmdId": 3, + "OP": op, + "RemarkName": user.RemarkName, + "UserName": user.UserName, + } + body, _ := ToBuffer(content) + req, _ := http.NewRequest(http.MethodPost, path.String(), body) + req.Header.Add("Content-Type", jsonContentType) + return c.Do(req) +} diff --git a/user.go b/user.go index d63d3c2..4a07011 100644 --- a/user.go +++ b/user.go @@ -110,6 +110,18 @@ func (u *User) IsMP() bool { return u.VerifyFlag == 8 || u.VerifyFlag == 24 || u.VerifyFlag == 136 } +// Pin 将联系人置顶 +func (u *User) Pin() error { + req := u.Self.Bot.storage.Request + return u.Self.Bot.Caller.WebWxRelationPin(req, u, 1) +} + +// UnPin 将联系人取消置顶 +func (u *User) UnPin() error { + req := u.Self.Bot.storage.Request + return u.Self.Bot.Caller.WebWxRelationPin(req, u, 0) +} + // Self 自己,当前登录用户对象 type Self struct { *User