添加消息正文格式化

This commit is contained in:
eatMoreApple 2021-07-31 17:35:15 +08:00
parent 721846f267
commit c46fb58824
3 changed files with 13 additions and 4 deletions

View File

@ -73,7 +73,7 @@ func main() {
groups, err := self.Groups()
fmt.Println(groups, err)
// 阻塞主goroutine, 知道发生异常或者用户主动退出
// 阻塞主goroutine, 直到发生异常或者用户主动退出
bot.Block()
}
```

6
bot.go
View File

@ -200,7 +200,7 @@ func (b *Bot) webInit() error {
if err = b.Caller.WebWxStatusNotify(req, resp, info); err != nil {
return err
}
// 开启协程,轮获取是否有新的消息返回
// 开启协程,轮获取是否有新的消息返回
go func() {
if b.GetMessageErrorHandler == nil {
b.GetMessageErrorHandler = b.stopAsyncCALL
@ -212,7 +212,7 @@ func (b *Bot) webInit() error {
return nil
}
// 轮请求
// 轮请求
// 根据状态码判断是否有新的请求
func (b *Bot) asyncCall() error {
var (
@ -220,7 +220,7 @@ func (b *Bot) asyncCall() error {
resp *SyncCheckResponse
)
for b.Alive() {
// 长轮检查是否有消息返回
// 长轮检查是否有消息返回
resp, err = b.Caller.SyncCheck(b.storage.LoginInfo, b.storage.Response)
if err != nil {
return err

View File

@ -310,7 +310,10 @@ func (m *Message) Get(key string) (value interface{}, exist bool) {
// 消息初始化,根据不同的消息作出不同的处理
func (m *Message) init(bot *Bot) {
m.Bot = bot
// 如果是群消息
if m.IsSendByGroup() {
// 将Username和正文分开
data := strings.Split(m.Content, ":<br/>")
m.Content = strings.Join(data[1:], "")
m.senderInGroupUserName = data[0]
@ -320,6 +323,7 @@ func (m *Message) init(bot *Bot) {
if displayName == "" {
displayName = receiver.NickName
}
// 判断是不是@消息
atFlag := "@" + displayName
index := len(atFlag) + 1 + 1
if strings.HasPrefix(m.Content, atFlag) && unicode.IsSpace(rune(m.Content[index])) {
@ -335,6 +339,11 @@ func (m *Message) init(bot *Bot) {
{
m.Content = strings.Replace(m.Content, `<br/>`, "\n", -1)
}
// 格式化文本消息中的emoji表情
if m.IsText() {
m.Content = FormatEmoji(m.Content)
}
}
// 发送消息的结构体