Skip to content
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

在使用 sdk 发送文本消息使用 larkim.MessageText 生成消息内容时 有一个体验不好的地方 #111

Open
twgcode opened this issue Aug 4, 2023 · 0 comments

Comments

@twgcode
Copy link

twgcode commented Aug 4, 2023

在使用 sdk 发送文本消息使用 larkim.MessageText 生成消息内容时 有一个体验不好的地方,业务代码
image
errInfo 会有换行符 导致 生成的消息内容不符合要求,然后发送消息直接失败

我看 larkim.MessageText 使用的这种构造方式
image

能不能改成类似这样的

import (
	"encoding/json"
	"strings"
)

type MessageText struct {
	builder strings.Builder
}

func NewMessageTextBuilder() *MessageText {
	m := &MessageText{}
	return m
}

func (t *MessageText) Text(text string) *MessageText {
	t.builder.WriteString(text)
	return t
}

func (t *MessageText) TextLine(text string) *MessageText {
	t.builder.WriteString(text)
	t.builder.WriteString("\n")
	return t
}

func (t *MessageText) Line() *MessageText {
	t.builder.WriteString("\n")
	return t
}

func (t *MessageText) AtUser(userId, name string) *MessageText {
	t.builder.WriteString(`<at user_id="`)
	t.builder.WriteString(userId)
	t.builder.WriteString(`">`)
	t.builder.WriteString(name)
	t.builder.WriteString("</at>")
	return t
}

func (t *MessageText) AtAll() *MessageText {
	t.builder.WriteString(`<at user_id="all">所有人</at>`)
	return t
}

func (t *MessageText) Build() (data string) {
	var tmp []byte
	tmp, _ = json.Marshal(map[string]string{"text": t.builder.String()})
	data = string(tmp) // TODO 后面优化成高效转化
	return
}

这样 感觉使用起来会舒服很多

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant