Skip to content

Commit 7ae6746

Browse files
committed
refactor: standalone functions for building outcoming message for send, update, and reply (#69)
chore: recover revive
1 parent f2fccac commit 7ae6746

File tree

8 files changed

+104
-27
lines changed

8 files changed

+104
-27
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
uses: actions/setup-go@v3
2727
with:
2828
go-version: '1.13.0'
29-
#
30-
# - name: lint
31-
# uses: morphy2k/revive-action@v2
29+
30+
- name: lint
31+
uses: morphy2k/revive-action@v2
3232

3333
- name: build
3434
run: ./scripts/test.sh

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## v1.14.0
4+
5+
- refactor(im): build reply and update message with standalone methods
6+
- feat(im): UpdateMessage supports text and post in addition to card
7+
- feat(im): ReplyMessage suports reply in thread
8+
39
## v1.13.3
410

511
- feat(event): support message_recalled, message_reaction_created, and message_reaction_deleted

api_message.go

+29-21
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ type PostMessageResponse struct {
2727

2828
// IMMessageRequest .
2929
type IMMessageRequest struct {
30-
ReceiveID string `json:"receive_id"`
31-
Content string `json:"content"`
32-
MsgType string `json:"msg_type"`
33-
UUID string `json:"uuid,omitempty"`
30+
Content string `json:"content"`
31+
MsgType string `json:"msg_type,omitempty"`
32+
ReceiveID string `json:"receive_id,omitempty"`
33+
UUID string `json:"uuid,omitempty"`
34+
ReplyInThread bool `json:"reply_in_thread,omitempty"`
3435
}
3536

3637
// IMSender .
@@ -56,19 +57,20 @@ type IMBody struct {
5657

5758
// IMMessage .
5859
type IMMessage struct {
59-
MessageID string `json:"message_id"`
60-
UpperMessageID string `json:"upper_message_id"`
61-
RootID string `json:"root_id"`
62-
ParentID string `json:"parent_id"`
63-
ChatID string `json:"chat_id"`
64-
MsgType string `json:"msg_type"`
65-
CreateTime string `json:"create_time"`
66-
UpdateTime string `json:"update_time"`
67-
Deleted bool `json:"deleted"`
68-
Updated bool `json:"updated"`
69-
Sender IMSender
70-
Mentions []IMMention
71-
Body IMBody
60+
MessageID string `json:"message_id"`
61+
UpperMessageID string `json:"upper_message_id"`
62+
RootID string `json:"root_id"`
63+
ParentID string `json:"parent_id"`
64+
ThreadID string `json:"thread_id"`
65+
ChatID string `json:"chat_id"`
66+
MsgType string `json:"msg_type"`
67+
CreateTime string `json:"create_time"`
68+
UpdateTime string `json:"update_time"`
69+
Deleted bool `json:"deleted"`
70+
Updated bool `json:"updated"`
71+
Sender IMSender `json:"sender"`
72+
Mentions []IMMention `json:"mentions"`
73+
Body IMBody `json:"body"`
7274
}
7375

7476
// ReactionResponse .
@@ -266,7 +268,7 @@ func (bot Bot) PostMessage(om OutcomingMessage) (*PostMessageResponse, error) {
266268

267269
// ReplyMessage replies a message
268270
func (bot Bot) ReplyMessage(om OutcomingMessage) (*PostMessageResponse, error) {
269-
req, err := BuildMessage(om)
271+
req, err := buildReplyMessage(om)
270272
if err != nil {
271273
return nil, err
272274
}
@@ -299,16 +301,22 @@ func (bot Bot) DeleteReaction(messageID string, reactionID string) (*ReactionRes
299301

300302
// UpdateMessage updates a message
301303
func (bot Bot) UpdateMessage(messageID string, om OutcomingMessage) (*UpdateMessageResponse, error) {
302-
if om.MsgType != MsgInteractive {
304+
if om.MsgType != MsgInteractive &&
305+
om.MsgType != MsgText &&
306+
om.MsgType != MsgPost {
303307
return nil, ErrMessageType
304308
}
305-
req, err := BuildMessage(om)
309+
req, err := buildUpdateMessage(om)
306310
if err != nil {
307311
return nil, err
308312
}
309313
url := fmt.Sprintf(updateMessageURL, messageID)
310314
var respData UpdateMessageResponse
311-
err = bot.PatchAPIRequest("UpdateMessage", url, true, req, &respData)
315+
if om.MsgType == MsgInteractive {
316+
err = bot.PatchAPIRequest("UpdateMessage", url, true, req, &respData)
317+
} else {
318+
err = bot.PutAPIRequest("UpdateMessage", url, true, req, &respData)
319+
}
312320
return &respData, err
313321
}
314322

api_message_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,17 @@ func TestReplyMessage(t *testing.T) {
5959
assert.Equal(t, 0, resp.Code)
6060
assert.NotEmpty(t, resp.Data.MessageID)
6161
}
62-
resp, err = bot.PostTextMentionAndReply("PostTextMentionAndReply", testUserOpenID, WithChatID(testGroupChatID), resp.Data.MessageID)
62+
replyID := resp.Data.MessageID
63+
resp, err = bot.PostTextMentionAndReply("PostTextMentionAndReply", testUserOpenID, WithChatID(testGroupChatID), replyID)
64+
if assert.NoError(t, err) {
65+
assert.Equal(t, 0, resp.Code)
66+
assert.NotEmpty(t, resp.Data.MessageID)
67+
}
68+
// Reply with Outcoming Message
69+
mb := NewMsgBuffer(MsgText)
70+
tb := NewTextBuilder()
71+
om := mb.Text(tb.Text("Reply raw").Render()).BindReply(replyID).ReplyInThread(true).Build()
72+
resp, err = bot.ReplyMessage(om)
6373
if assert.NoError(t, err) {
6474
assert.Equal(t, 0, resp.Code)
6575
assert.NotEmpty(t, resp.Data.MessageID)
@@ -210,6 +220,20 @@ func TestPostPostMessage(t *testing.T) {
210220
if assert.NoError(t, err) {
211221
assert.Equal(t, 0, resp.Code)
212222
assert.NotEmpty(t, resp.Data.MessageID)
223+
224+
newOM := NewMsgBuffer(MsgPost).
225+
BindOpenChatID(testGroupChatID).
226+
Post(
227+
NewPostBuilder().
228+
Title("modified title").
229+
TextTag("modified content", 1, true).
230+
Render(),
231+
).
232+
Build()
233+
_, err = bot.UpdateMessage(resp.Data.MessageID, newOM)
234+
if assert.NoError(t, err) {
235+
assert.Equal(t, 0, resp.Code)
236+
}
213237
}
214238
}
215239

@@ -446,7 +470,7 @@ func TestPinMessages(t *testing.T) {
446470
}
447471
}
448472

449-
func TestReactionMessage(t *testing.T) {
473+
func TestMessageReactions(t *testing.T) {
450474
msg := NewMsgBuffer(MsgText)
451475
om := msg.BindEmail(testUserEmail).Text("hello, world").Build()
452476
resp, err := bot.PostMessage(om)

message.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type OutcomingMessage struct {
2727
ChatID string `json:"chat_id,omitempty"`
2828
UnionID string `json:"-"`
2929
// For reply
30-
RootID string `json:"root_id,omitempty"`
30+
RootID string `json:"root_id,omitempty"`
31+
ReplyInThread bool `json:"reply_in_thread,omitempty"`
3132
// Sign for notification bot
3233
Sign string `json:"sign"`
3334
// Timestamp for sign

message_v2.go

+30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ func BuildMessage(om OutcomingMessage) (*IMMessageRequest, error) {
2323
return &req, nil
2424
}
2525

26+
func buildReplyMessage(om OutcomingMessage) (*IMMessageRequest, error) {
27+
req := IMMessageRequest{
28+
MsgType: string(om.MsgType),
29+
Content: buildContent(om),
30+
ReceiveID: buildReceiveID(om),
31+
}
32+
if req.Content == "" {
33+
return nil, ErrMessageNotBuild
34+
}
35+
if om.ReplyInThread == true {
36+
req.ReplyInThread = om.ReplyInThread
37+
}
38+
39+
return &req, nil
40+
}
41+
42+
func buildUpdateMessage(om OutcomingMessage) (*IMMessageRequest, error) {
43+
req := IMMessageRequest{
44+
Content: buildContent(om),
45+
}
46+
if om.MsgType != MsgInteractive {
47+
req.MsgType = om.MsgType
48+
}
49+
if req.Content == "" {
50+
return nil, ErrMessageNotBuild
51+
}
52+
53+
return &req, nil
54+
}
55+
2656
func buildContent(om OutcomingMessage) string {
2757
var (
2858
content = ""

msg_buf.go

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ func (m *MsgBuffer) BindReply(rootID string) *MsgBuffer {
7676
return m
7777
}
7878

79+
// ReplyInThread replies message in thread
80+
func (m *MsgBuffer) ReplyInThread(replyInThread bool) *MsgBuffer {
81+
m.message.ReplyInThread = replyInThread
82+
return m
83+
}
84+
7985
// WithSign generates sign for notification bot check
8086
func (m *MsgBuffer) WithSign(secret string, ts int64) *MsgBuffer {
8187
m.message.Sign, _ = GenSign(secret, ts)

msg_buf_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func TestBindingUserIDs(t *testing.T) {
4141
mb.Clear()
4242
msgReplyID := mb.BindReply("om_f779ffe0ffa3d1b94fc1ef5fcb6f1063").Build()
4343
assert.Equal(t, "om_f779ffe0ffa3d1b94fc1ef5fcb6f1063", msgReplyID.RootID)
44+
mb.ReplyInThread(true)
45+
assert.False(t, msgReplyID.ReplyInThread)
4446
}
4547

4648
func TestMsgShareChat(t *testing.T) {

0 commit comments

Comments
 (0)