From 1957598c528ecb61215dbb05419b5df24a5653a1 Mon Sep 17 00:00:00 2001 From: Haibo Date: Thu, 1 Feb 2024 06:13:12 +0000 Subject: [PATCH] fix: larkim.MessageText builds invalid json strings --- service/im/v1/ext_model.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/service/im/v1/ext_model.go b/service/im/v1/ext_model.go index 25b00369..9fea527c 100644 --- a/service/im/v1/ext_model.go +++ b/service/im/v1/ext_model.go @@ -233,15 +233,11 @@ type MessageText struct { } func NewTextMsgBuilder() *MessageText { - m := &MessageText{} - m.builder.WriteString("{\"text\":\"") - return m + return &MessageText{} } func NewMessageTextBuilder() *MessageText { - m := &MessageText{} - m.builder.WriteString("{\"text\":\"") - return m + return &MessageText{} } func (t *MessageText) Text(text string) *MessageText { @@ -251,34 +247,36 @@ func (t *MessageText) Text(text string) *MessageText { func (t *MessageText) TextLine(text string) *MessageText { t.builder.WriteString(text) - t.builder.WriteString("\\n") + t.builder.WriteString("\n") return t } func (t *MessageText) Line() *MessageText { - t.builder.WriteString("\\n") + t.builder.WriteString("\n") return t } func (t *MessageText) AtUser(userId, name string) *MessageText { - t.builder.WriteString("") + t.builder.WriteString("\">") t.builder.WriteString(name) t.builder.WriteString("") return t - return t } func (t *MessageText) AtAll() *MessageText { - t.builder.WriteString("") - t.builder.WriteString("") + t.builder.WriteString("") return t } -func (t *MessageText) Build() string { - t.builder.WriteString("\"}") - return t.builder.String() +func (t *MessageText) Build() (string, error) { + m := map[string]string{"text": t.builder.String()} + bs, err := json.Marshal(m) + if err != nil { + return "", err + } + return string(bs), nil } /**