-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(mail): support sharing emails to IM chats #637
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ba354f
feat(mail): add +share-to-chat shortcut to share emails as IM cards
chanthuang 067a7c7
fix(mail): regenerate SKILL.md from skill-template instead of manual …
chanthuang 4b633e3
fix(mail): add docstrings and use real validation path in tests
chanthuang a164ef0
test(mail): add dry-run E2E test for +share-to-chat
chanthuang 55df8d5
fix(mail): include request body and params in dry-run output
chanthuang 783f5f0
style(mail): gofmt dry-run test file
chanthuang b8697bc
test(mail): assert exact API call count in dry-run test
chanthuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,118 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package mail | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/larksuite/cli/internal/output" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| // validReceiveIDTypes enumerates accepted --receive-id-type values. | ||
| var validReceiveIDTypes = map[string]bool{ | ||
| "chat_id": true, | ||
| "open_id": true, | ||
| "user_id": true, | ||
| "union_id": true, | ||
| "email": true, | ||
| } | ||
|
|
||
| // MailShareToChat shares an email or thread as a card to a Lark IM chat. | ||
| var MailShareToChat = common.Shortcut{ | ||
| Service: "mail", | ||
| Command: "+share-to-chat", | ||
| Description: "Share an email or thread as a card to a Lark IM chat.", | ||
| Risk: "write", | ||
| Scopes: []string{ | ||
| "mail:user_mailbox.message:readonly", | ||
| "im:message", | ||
| "im:message.send_as_user", | ||
| }, | ||
| AuthTypes: []string{"user"}, | ||
| HasFormat: true, | ||
| Flags: []common.Flag{ | ||
| {Name: "message-id", Desc: "Message ID to share (mutually exclusive with --thread-id)"}, | ||
| {Name: "thread-id", Desc: "Thread ID to share (mutually exclusive with --message-id)"}, | ||
| {Name: "receive-id", Desc: "Receiver ID. Type determined by --receive-id-type.", Required: true}, | ||
| {Name: "receive-id-type", Default: "chat_id", Desc: "Receiver ID type: chat_id (default), open_id, user_id, union_id, email"}, | ||
| {Name: "mailbox", Default: "me", Desc: "Mailbox email address (default: me)"}, | ||
| }, | ||
| DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { | ||
| mailboxID := resolveMailboxID(runtime) | ||
| msgID := runtime.Str("message-id") | ||
| threadID := runtime.Str("thread-id") | ||
| receiveID := runtime.Str("receive-id") | ||
| receiveIDType := runtime.Str("receive-id-type") | ||
|
|
||
| var createBody map[string]interface{} | ||
| if threadID != "" { | ||
| createBody = map[string]interface{}{"thread_id": threadID} | ||
| } else { | ||
| createBody = map[string]interface{}{"message_id": msgID} | ||
| } | ||
|
|
||
| return common.NewDryRunAPI(). | ||
| Desc("Share email card: create share token → send card to IM chat"). | ||
| POST(mailboxPath(mailboxID, "messages", "share_token")). | ||
| Body(createBody). | ||
| POST(mailboxPath(mailboxID, "share_tokens", "<card_id>", "send")). | ||
| Params(map[string]interface{}{"receive_id_type": receiveIDType}). | ||
| Body(map[string]interface{}{"receive_id": receiveID}) | ||
| }, | ||
| Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| msgID := runtime.Str("message-id") | ||
| threadID := runtime.Str("thread-id") | ||
| if msgID == "" && threadID == "" { | ||
| return output.ErrValidation("either --message-id or --thread-id is required") | ||
| } | ||
| if msgID != "" && threadID != "" { | ||
| return output.ErrValidation("--message-id and --thread-id are mutually exclusive") | ||
| } | ||
| idType := runtime.Str("receive-id-type") | ||
| if !validReceiveIDTypes[idType] { | ||
| return output.ErrValidation("--receive-id-type must be one of: chat_id, open_id, user_id, union_id, email") | ||
| } | ||
| return nil | ||
| }, | ||
| Execute: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| msgID := runtime.Str("message-id") | ||
| threadID := runtime.Str("thread-id") | ||
| receiveID := runtime.Str("receive-id") | ||
| receiveIDType := runtime.Str("receive-id-type") | ||
| mailboxID := resolveMailboxID(runtime) | ||
|
|
||
| var createBody map[string]interface{} | ||
| if threadID != "" { | ||
| createBody = map[string]interface{}{"thread_id": threadID} | ||
| } else { | ||
| createBody = map[string]interface{}{"message_id": msgID} | ||
| } | ||
| createResp, err := runtime.CallAPI("POST", | ||
| mailboxPath(mailboxID, "messages", "share_token"), | ||
| nil, createBody) | ||
| if err != nil { | ||
| return fmt.Errorf("create share token: %w", err) | ||
| } | ||
| cardID, _ := createResp["card_id"].(string) | ||
| if cardID == "" { | ||
| return fmt.Errorf("create share token: response missing card_id") | ||
| } | ||
|
|
||
| sendResp, err := runtime.CallAPI("POST", | ||
| mailboxPath(mailboxID, "share_tokens", cardID, "send"), | ||
| map[string]interface{}{"receive_id_type": receiveIDType}, | ||
| map[string]interface{}{"receive_id": receiveID}) | ||
| if err != nil { | ||
| return fmt.Errorf("share token created (card_id=%s) but send failed: %w", cardID, err) | ||
| } | ||
|
|
||
| runtime.Out(map[string]interface{}{ | ||
| "card_id": cardID, | ||
| "im_message_id": sendResp["message_id"], | ||
| }, nil) | ||
| return nil | ||
| }, | ||
| } | ||
This file contains hidden or 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,190 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package mail | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/httpmock" | ||
| ) | ||
|
|
||
| func TestShareToChatValidationErrors(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "missing both message-id and thread-id", | ||
| args: []string{"+share-to-chat", "--receive-id", "oc_xxx"}, | ||
| wantErr: "either --message-id or --thread-id is required", | ||
| }, | ||
| { | ||
| name: "both message-id and thread-id", | ||
| args: []string{"+share-to-chat", "--message-id", "m1", "--thread-id", "t1", "--receive-id", "oc_xxx"}, | ||
| wantErr: "--message-id and --thread-id are mutually exclusive", | ||
| }, | ||
| { | ||
| name: "invalid receive-id-type", | ||
| args: []string{"+share-to-chat", "--message-id", "m1", "--receive-id", "oc_xxx", "--receive-id-type", "invalid"}, | ||
| wantErr: "--receive-id-type must be one of", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| f, stdout, _, _ := mailShortcutTestFactory(t) | ||
| err := runMountedMailShortcut(t, MailShareToChat, tt.args, f, stdout) | ||
| if err == nil { | ||
| t.Fatalf("expected error containing %q, got nil", tt.wantErr) | ||
| } | ||
| if !strings.Contains(err.Error(), tt.wantErr) { | ||
| t.Fatalf("expected error containing %q, got %q", tt.wantErr, err.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestShareToChatExecuteWithMessageID(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/messages/share_token", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{ | ||
| "card_id": "card_001", | ||
| }, | ||
| }, | ||
| }) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/share_tokens/card_001/send", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{ | ||
| "message_id": "om_001", | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| err := runMountedMailShortcut(t, MailShareToChat, []string{ | ||
| "+share-to-chat", "--message-id", "m1", "--receive-id", "oc_xxx", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got %v", err) | ||
| } | ||
| out := stdout.String() | ||
| if !strings.Contains(out, "card_001") { | ||
| t.Errorf("expected output to contain card_id, got %s", out) | ||
| } | ||
| if !strings.Contains(out, "om_001") { | ||
| t.Errorf("expected output to contain im_message_id, got %s", out) | ||
| } | ||
| } | ||
|
|
||
| func TestShareToChatExecuteWithThreadID(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/messages/share_token", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{ | ||
| "card_id": "card_002", | ||
| }, | ||
| }, | ||
| }) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/share_tokens/card_002/send", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{ | ||
| "message_id": "om_002", | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| err := runMountedMailShortcut(t, MailShareToChat, []string{ | ||
| "+share-to-chat", "--thread-id", "t1", "--receive-id", "user@example.com", "--receive-id-type", "email", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got %v", err) | ||
| } | ||
| out := stdout.String() | ||
| if !strings.Contains(out, "card_002") { | ||
| t.Errorf("expected output to contain card_id, got %s", out) | ||
| } | ||
| } | ||
|
|
||
| func TestShareToChatStep1Failure(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/messages/share_token", | ||
| Body: map[string]interface{}{ | ||
| "code": 4034, | ||
| "msg": "message not found", | ||
| }, | ||
| }) | ||
|
|
||
| err := runMountedMailShortcut(t, MailShareToChat, []string{ | ||
| "+share-to-chat", "--message-id", "bad_id", "--receive-id", "oc_xxx", | ||
| }, f, stdout) | ||
| if err == nil { | ||
| t.Fatal("expected error for step 1 failure, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "create share token") { | ||
| t.Errorf("expected error to mention 'create share token', got %q", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func TestShareToChatStep2Failure(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/messages/share_token", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{ | ||
| "card_id": "card_003", | ||
| }, | ||
| }, | ||
| }) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/share_tokens/card_003/send", | ||
| Body: map[string]interface{}{ | ||
| "code": 4046, | ||
| "msg": "user not in chat", | ||
| }, | ||
| }) | ||
|
|
||
| err := runMountedMailShortcut(t, MailShareToChat, []string{ | ||
| "+share-to-chat", "--message-id", "m1", "--receive-id", "oc_not_in", | ||
| }, f, stdout) | ||
| if err == nil { | ||
| t.Fatal("expected error for step 2 failure, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "card_003") { | ||
| t.Errorf("expected error to contain card_id, got %q", err.Error()) | ||
| } | ||
| if !strings.Contains(err.Error(), "send failed") { | ||
| t.Errorf("expected error to mention 'send failed', got %q", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func TestValidReceiveIDTypes(t *testing.T) { | ||
| expected := []string{"chat_id", "open_id", "user_id", "union_id", "email"} | ||
| for _, typ := range expected { | ||
| if !validReceiveIDTypes[typ] { | ||
| t.Errorf("expected %q to be a valid receive ID type", typ) | ||
| } | ||
| } | ||
| if validReceiveIDTypes["invalid"] { | ||
| t.Error("expected 'invalid' to not be a valid receive ID type") | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,5 +20,6 @@ | |
| MailDraftEdit, | ||
| MailForward, | ||
| MailSignature, | ||
| MailShareToChat, | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.