Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contact): support user info #67

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions api_contact.go
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
}
20 changes: 20 additions & 0 deletions api_contact_test.go
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")
}
}
}
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var (
ErrBotTypeError = errors.New("Bot type error")
ErrParamUserID = errors.New("Param error: UserID")
ErrParamMessageID = errors.New("Param error: Message ID")
ErrParamExceedInputLimit = errors.New("Param error: Exceed input limit")
ErrMessageTypeNotSuppored = errors.New("Message type not supported")
ErrEncryptionNotEnabled = errors.New("Encryption is not enabled")
ErrCustomHTTPClientNotSet = errors.New("Custom HTTP client not set")
Expand Down
Loading