diff --git a/api.go b/api.go index 18fa0ce..71f94e0 100644 --- a/api.go +++ b/api.go @@ -5,3 +5,16 @@ type BaseResponse struct { Code int `json:"code"` Msg string `json:"msg"` } + +// I18NNames . +type I18NNames struct { + ZhCN string `json:"zh_cn,omitempty"` + EnUS string `json:"en_us,omitempty"` + JaJP string `json:"ja_jp,omitempty"` +} + +// WithUserIDType . +func (bot *Bot) WithUserIDType(userIDType string) *Bot { + bot.userIDType = userIDType + return bot +} diff --git a/api_chat.go b/api_chat.go index eca4879..b7606ff 100644 --- a/api_chat.go +++ b/api_chat.go @@ -7,6 +7,8 @@ import ( const ( getChatURL = "/open-apis/im/v1/chats/%s?user_id_type=%s" + listChatURL = "/open-apis/im/v1/chats?user_id_type=%s&sort_type=%s&page_token=%s&page_size=%d" + searchChatURL = "/open-apis/im/v1/chats/search?user_id_type=%s&query=%s&page_token=%s&page_size=%d" updateChatURL = "/open-apis/im/v1/chats/%s?user_id_type=%s" createChatURL = "/open-apis/im/v1/chats?user_id_type=%s" deleteChatURL = "/open-apis/im/v1/chats/%s" @@ -26,13 +28,6 @@ type GetChatResponse struct { Data ChatInfo `json:"data"` } -// I18NNames . -type I18NNames struct { - ZhCN string `json:"zh_cn,omitempty"` - EnUS string `json:"en_us,omitempty"` - JaJP string `json:"ja_jp,omitempty"` -} - // ChatInfo entity of a chat, not every field is available for every API. type ChatInfo struct { ChatID string `json:"chat_id,omitempty"` @@ -56,6 +51,29 @@ type ChatInfo struct { External bool `json:"external,omitempty"` } +// ListChatResponse . +type ListChatResponse struct { + BaseResponse + + Data struct { + Items []ChatListInfo `json:"items"` + PageToken string `json:"page_token"` + HasMore bool `json:"has_more"` + } `json:"data"` +} + +// ChatListInfo . +type ChatListInfo struct { + ChatID string `json:"chat_id,omitempty"` + Name string `json:"name,omitempty"` + Avatar string `json:"avatar,omitempty"` + Description string `json:"description,omitempty"` + OwnerIDType string `json:"owner_id_type,omitempty"` + OwnerID string `json:"owner_id,omitempty"` + External bool `json:"external,omitempty"` + TenantKey string `json:"tenant_key"` +} + // CreateChatRequest . type CreateChatRequest struct { Name string `json:"name,omitempty"` @@ -184,12 +202,6 @@ type SetTopNoticeResponse = BaseResponse // DeleteTopNoticeResponse . type DeleteTopNoticeResponse = BaseResponse -// WithUserIDType . -func (bot *Bot) WithUserIDType(userIDType string) *Bot { - bot.userIDType = userIDType - return bot -} - // GetChat . func (bot Bot) GetChat(chatID string) (*GetChatResponse, error) { var respData GetChatResponse @@ -197,6 +209,30 @@ func (bot Bot) GetChat(chatID string) (*GetChatResponse, error) { return &respData, err } +// ListChat list chats +// sortType: ByCreateTimeAsc/ByActiveTimeDesc +func (bot Bot) ListChat(sortType string, pageToken string, pageSize int) (*ListChatResponse, error) { + var respData ListChatResponse + if sortType == "" { + sortType = "ByCreateTimeAsc" + } + err := bot.GetAPIRequest( + "ListChat", + fmt.Sprintf(listChatURL, bot.userIDType, sortType, pageToken, pageSize), + true, nil, &respData) + return &respData, err +} + +// SearchChat search chat +func (bot Bot) SearchChat(query string, pageToken string, pageSize int) (*ListChatResponse, error) { + var respData ListChatResponse + err := bot.GetAPIRequest( + "SearchChat", + fmt.Sprintf(searchChatURL, bot.userIDType, query, pageToken, pageSize), + true, nil, &respData) + return &respData, err +} + // CreateChat . func (bot Bot) CreateChat(req CreateChatRequest) (*CreateChatResponse, error) { var respData CreateChatResponse diff --git a/api_chat_test.go b/api_chat_test.go index f6310c9..299392f 100644 --- a/api_chat_test.go +++ b/api_chat_test.go @@ -2,6 +2,7 @@ package lark import ( "fmt" + "strings" "testing" "time" @@ -21,6 +22,34 @@ func TestChatInfo(t *testing.T) { } } +func TestChatList(t *testing.T) { + bot.WithUserIDType(UIDOpenID) + assert.Equal(t, UIDOpenID, bot.userIDType) + resp, err := bot.ListChat("ByCreateTimeAsc", "", 10) + if assert.NoError(t, err) { + assert.Equal(t, 0, resp.Code) + assert.NotEmpty(t, resp.Data.Items) + t.Log(resp.Data.Items[0]) + } +} + +func TestChatSearch(t *testing.T) { + bot.WithUserIDType(UIDOpenID) + assert.Equal(t, UIDOpenID, bot.userIDType) + resp, err := bot.SearchChat("go-lark", "", 10) + if assert.NoError(t, err) { + assert.Equal(t, 0, resp.Code) + if assert.NotEmpty(t, resp.Data.Items) { + for _, item := range resp.Data.Items { + if !strings.Contains(item.Name, "go-lark") { + t.Error(item.Name, "does not contain go-lark") + } + } + } + t.Log(resp.Data.Items) + } +} + func TestChatCRUD(t *testing.T) { bot.WithUserIDType(UIDOpenID) resp, err := bot.CreateChat(CreateChatRequest{ diff --git a/api_message_test.go b/api_message_test.go index 0a529ec..8c65ca6 100644 --- a/api_message_test.go +++ b/api_message_test.go @@ -409,13 +409,6 @@ func TestMessageCRUD(t *testing.T) { if assert.NoError(t, err) { t.Log(receipt.Data.ReadUsers) } - // receipt read - receiptOld, err := bot.MessageReadReceipt(testMessageID) - if assert.NoError(t, err) { - // failed because the message ID will be outdated after tens of days - // assert.NotEmpty(t, receiptOld.Data.ReadUsers) - t.Log(receiptOld.Data.ReadUsers) - } } func TestIdempotentMessage(t *testing.T) { @@ -437,12 +430,18 @@ func TestIdempotentMessage(t *testing.T) { } func TestPinMessages(t *testing.T) { - resp, err := bot.PinMessage(testMessageID) + msg := NewMsgBuffer(MsgText) + om := msg.BindEmail(testUserEmail).Text("hello, world").Build() + resp, err := bot.PostMessage(om) if assert.NoError(t, err) { - assert.Equal(t, 0, resp.Code) - assert.Equal(t, testMessageID, resp.Data.Pin.MessageID) - unpinResp, err := bot.UnpinMessage(testMessageID) - assert.NoError(t, err) - assert.Equal(t, 0, unpinResp.Code) + messageID := resp.Data.MessageID + resp, err := bot.PinMessage(messageID) + if assert.NoError(t, err) { + assert.Equal(t, 0, resp.Code) + assert.Equal(t, messageID, resp.Data.Pin.MessageID) + unpinResp, err := bot.UnpinMessage(messageID) + assert.NoError(t, err) + assert.Equal(t, 0, unpinResp.Code) + } } } diff --git a/lark_test.go b/lark_test.go index 8502574..d9cebfc 100644 --- a/lark_test.go +++ b/lark_test.go @@ -24,7 +24,6 @@ var ( testUserID string testUserUnionID string testGroupChatID string - testMessageID string testWebhookV1 string testWebhookV2 string testWebhookV2Signed string @@ -48,7 +47,6 @@ func newTestBot() *Bot { testUserUnionID = os.Getenv("LARK_UNION_ID") testUserOpenID = os.Getenv("LARK_OPEN_ID") testGroupChatID = os.Getenv("LARK_CHAT_ID") - testMessageID = os.Getenv("LARK_MESSAGE_ID") testWebhookV1 = os.Getenv("LARK_WEBHOOK_V1") testWebhookV2 = os.Getenv("LARK_WEBHOOK_V2") testWebhookV2Signed = os.Getenv("LARK_WEBHOOK_V2_SIGNED") @@ -58,8 +56,7 @@ func newTestBot() *Bot { len(testUserID) == 0 || len(testUserUnionID) == 0 || len(testUserOpenID) == 0 || - len(testGroupChatID) == 0 || - len(testMessageID) == 0 { + len(testGroupChatID) == 0 { panic("insufficient test environment") } return NewChatBot(testAppID, testAppSecret) diff --git a/msg_buf_test.go b/msg_buf_test.go index 1af4820..9f54e65 100644 --- a/msg_buf_test.go +++ b/msg_buf_test.go @@ -39,8 +39,8 @@ func TestBindingUserIDs(t *testing.T) { assert.Equal(t, "333444", msgUserID.UserID) mb.Clear() - msgReplyID := mb.BindReply(testMessageID).Build() - assert.Equal(t, testMessageID, msgReplyID.RootID) + msgReplyID := mb.BindReply("om_f779ffe0ffa3d1b94fc1ef5fcb6f1063").Build() + assert.Equal(t, "om_f779ffe0ffa3d1b94fc1ef5fcb6f1063", msgReplyID.RootID) } func TestMsgShareChat(t *testing.T) {