-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contact): support user info (#67)
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package lark | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
getUserInfoURL = "/open-apis/contact/v3/users/%s?user_id_type=%s" | ||
batchGetUserInfoURL = "/open-apis/contact/v3/users/batch?%s" | ||
) | ||
|
||
// GetUserInfoResponse . | ||
type GetUserInfoResponse struct { | ||
BaseResponse | ||
Data struct { | ||
User UserInfo | ||
} | ||
} | ||
|
||
// BatchGetUserInfoResponse . | ||
type BatchGetUserInfoResponse struct { | ||
BaseResponse | ||
Data struct { | ||
Items []UserInfo | ||
} | ||
} | ||
|
||
// UserInfo . | ||
type UserInfo struct { | ||
OpenID string `json:"open_id,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
UserID string `json:"user_id,omitempty"` | ||
ChatID string `json:"chat_id,omitempty"` | ||
UnionID string `json:"union_id,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
EnglishName string `json:"en_name,omitempty"` | ||
NickName string `json:"nickname,omitempty"` | ||
Mobile string `json:"mobile,omitempty"` | ||
MobileVisible bool `json:"mobile_visible,omitempty"` | ||
Gender int `json:"gender,omitempty"` | ||
Avatar UserAvatar `json:"avatar,omitempty"` | ||
Status UserStatus `json:"status,omitempty"` | ||
City string `json:"city,omitempty"` | ||
Country string `json:"country,omitempty"` | ||
WorkStation string `json:"work_station,omitempty"` | ||
JoinTime int `json:"join_time,omitempty"` | ||
EmployeeNo string `json:"employee_no,omitempty"` | ||
EmployeeType int `json:"employee_type,omitempty"` | ||
EnterpriseEmail string `json:"enterprise_email,omitempty"` | ||
Geo string `json:"geo,omitempty"` | ||
JobTitle string `json:"job_title,omitempty"` | ||
JobLevelID string `json:"job_level_id,omitempty"` | ||
JobFamilyID string `json:"job_family_id,omitempty"` | ||
DepartmentIDs []string `json:"department_ids,omitempty"` | ||
LeaderUserID string `json:"leader_user_id,omitempty"` | ||
IsTenantManager bool `json:"is_tenant_manager,omitempty"` | ||
} | ||
|
||
// UserAvatar . | ||
type UserAvatar struct { | ||
Avatar72 string `json:"avatar_72,omitempty"` | ||
Avatar240 string `json:"avatar_240,omitempty"` | ||
Avatar640 string `json:"avatar_640,omitempty"` | ||
AvatarOrigin string `json:"avatar_origin,omitempty"` | ||
} | ||
|
||
// UserStatus . | ||
type UserStatus struct { | ||
IsFrozen bool | ||
IsResigned bool | ||
IsActivated bool | ||
IsExited bool | ||
IsUnjoin bool | ||
} | ||
|
||
// GetUserInfo gets contact info | ||
func (bot Bot) GetUserInfo(userID *OptionalUserID) (*GetUserInfoResponse, error) { | ||
url := fmt.Sprintf(getUserInfoURL, userID.RealID, userID.UIDType) | ||
var respData GetUserInfoResponse | ||
err := bot.GetAPIRequest("GetUserInfo", url, true, nil, &respData) | ||
return &respData, err | ||
} | ||
|
||
// BatchGetUserInfo gets contact info in batch | ||
func (bot Bot) BatchGetUserInfo(userIDType string, userIDs ...string) (*BatchGetUserInfoResponse, error) { | ||
if len(userIDs) == 0 || len(userIDs) > 50 { | ||
return nil, ErrParamExceedInputLimit | ||
} | ||
v := url.Values{} | ||
v.Set("user_id_type", userIDType) | ||
for _, userID := range userIDs { | ||
v.Add("user_ids", userID) | ||
} | ||
url := fmt.Sprintf(batchGetUserInfoURL, v.Encode()) | ||
var respData BatchGetUserInfoResponse | ||
err := bot.GetAPIRequest("GetUserInfo", url, true, nil, &respData) | ||
return &respData, err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package lark | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetUserInfo(t *testing.T) { | ||
resp, err := bot.GetUserInfo(WithUserID(testUserID)) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, resp.Data.User.Name, "David") | ||
} | ||
bresp, err := bot.BatchGetUserInfo(UIDUserID, testUserID) | ||
if assert.NoError(t, err) { | ||
if assert.NotEmpty(t, bresp.Data.Items) { | ||
assert.Equal(t, bresp.Data.Items[0].Name, "David") | ||
} | ||
} | ||
} |
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