1
0
forked from lxh/go-wxhelper
go-wxhelper/tcpserver/parse.go
2023-12-11 10:53:21 +08:00

47 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tcpserver
import (
"encoding/json"
"go-wechat/common/current"
"go-wechat/model"
"go-wechat/types"
"log"
"net"
"strings"
)
// parse
// @description: 解析消息
// @param msg
func parse(remoteAddr net.Addr, msg []byte) {
var m model.Message
if err := json.Unmarshal(msg, &m); err != nil {
log.Printf("[%s]消息解析失败: %v", remoteAddr, err)
log.Printf("[%s]消息内容: %d -> %v", remoteAddr, len(msg), string(msg))
return
}
// 记录原始数据
m.Raw = string(msg)
// 提取出群成员信息
// Sys类型的消息正文不包含微信 Id所以不需要处理
if m.IsGroup() && m.Type != types.MsgTypeSys {
// 群消息,处理一下消息和发信人
groupUser := strings.Split(m.Content, "\n")[0]
groupUser = strings.ReplaceAll(groupUser, ":", "")
// 如果两个id一致说明是系统发的
if m.FromUser != groupUser {
m.GroupUser = groupUser
}
// 用户的操作单独提出来处理一下
m.Content = strings.Join(strings.Split(m.Content, "\n")[1:], "\n")
}
log.Printf("%s\n消息来源: %s\n群成员: %s\n消息类型: %v\n消息内容: %s", remoteAddr, m.FromUser, m.GroupUser, m.Type, m.Content)
// 插件不为空,开始执行
if p := current.GetRobotMessageHandler(); p != nil {
p(&m)
}
}