Skip to content

Commit

Permalink
feat(chat): support list and search (#60)
Browse files Browse the repository at this point in the history
* feat(chat): support list and search

* revert it

* chore(test): drop message id constant
  • Loading branch information
crispgm authored Nov 29, 2023
1 parent b69151b commit 51fa5ab
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 32 deletions.
13 changes: 13 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
62 changes: 49 additions & 13 deletions api_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -184,19 +202,37 @@ 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
err := bot.GetAPIRequest("GetChatInfo", fmt.Sprintf(getChatURL, chatID, bot.userIDType), true, nil, &respData)
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
Expand Down
29 changes: 29 additions & 0 deletions api_chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lark

import (
"fmt"
"strings"
"testing"
"time"

Expand All @@ -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{
Expand Down
25 changes: 12 additions & 13 deletions api_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}
}
5 changes: 1 addition & 4 deletions lark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
testUserID string
testUserUnionID string
testGroupChatID string
testMessageID string
testWebhookV1 string
testWebhookV2 string
testWebhookV2Signed string
Expand All @@ -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")
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions msg_buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 51fa5ab

Please sign in to comment.