getIDByEmail gets user ids by emails, returns email to userID mapping. https://open.larksuite.com/document/server-docs/contact-v3/user/batch_get_id
(ctx context.Context, emails []string)
| 186 | // getIDByEmail gets user ids by emails, returns email to userID mapping. |
| 187 | // https://open.larksuite.com/document/server-docs/contact-v3/user/batch_get_id |
| 188 | func (p *provider) getIDByEmail(ctx context.Context, emails []string) (map[string]string, error) { |
| 189 | userID := make(map[string]string) |
| 190 | var emailsToGet []string |
| 191 | for _, email := range emails { |
| 192 | id, ok := userIDCache.Get(email) |
| 193 | if ok { |
| 194 | // user.UserID == "" means the user is not found on lark. |
| 195 | if id != "" { |
| 196 | userID[email] = id |
| 197 | } |
| 198 | } else { |
| 199 | emailsToGet = append(emailsToGet, email) |
| 200 | } |
| 201 | } |
| 202 | if len(emailsToGet) == 0 { |
| 203 | return userID, nil |
| 204 | } |
| 205 | |
| 206 | const url = "https://open.larksuite.com/open-apis/contact/v3/users/batch_get_id" |
| 207 | body, err := json.Marshal(&getIDByEmailRequest{Emails: emailsToGet}) |
| 208 | if err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | |
| 212 | b, err := p.do(ctx, http.MethodPost, url, body) |
| 213 | if err != nil { |
| 214 | return nil, errors.Wrapf(err, "failed to get user id by email") |
| 215 | } |
| 216 | |
| 217 | var response emailsFindResponse |
| 218 | if err := json.Unmarshal([]byte(b), &response); err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | |
| 222 | if response.Code != 0 { |
| 223 | return nil, errors.Errorf("failed to get id by email, code %d, msg %s", response.Code, response.Msg) |
| 224 | } |
| 225 | |
| 226 | for _, user := range response.Data.UserList { |
| 227 | if user.UserID == "" { |
| 228 | continue |
| 229 | } |
| 230 | // user.UserID == "" means the user is not found on lark. |
| 231 | // We store "" into the cache to prevent finding every time. |
| 232 | userID[user.Email] = user.UserID |
| 233 | userIDCache.Add(user.Email, user.UserID) |
| 234 | } |
| 235 | |
| 236 | return userID, nil |
| 237 | } |
| 238 | |
| 239 | // https://open.larksuite.com/document/server-docs/im-v1/message/create |
| 240 | func (p *provider) sendMessage(ctx context.Context, userID string, messageCard *WebhookCard) error { |