1
0
forked from lxh/go-wxhelper
go-wxhelper/tcpserver/parse.go

47 lines
1.3 KiB
Go
Raw Normal View History

2023-12-11 10:44:23 +08:00
package tcpserver
2023-09-21 17:33:59 +08:00
import (
"encoding/json"
2023-12-11 10:44:23 +08:00
"go-wechat/common/current"
2024-07-05 09:32:39 +08:00
"go-wechat/model/dto"
2023-09-21 17:33:59 +08:00
"go-wechat/types"
"log"
"net"
"strings"
)
2023-12-11 10:44:23 +08:00
// parse
2023-09-21 17:33:59 +08:00
// @description: 解析消息
// @param msg
2023-12-11 10:44:23 +08:00
func parse(remoteAddr net.Addr, msg []byte) {
2024-07-05 09:32:39 +08:00
var m dto.Message
2023-09-21 17:33:59 +08:00
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
}
2023-12-11 10:44:23 +08:00
// 记录原始数据
m.Raw = string(msg)
2023-09-21 17:33:59 +08:00
// 提取出群成员信息
2023-12-07 22:38:49 +08:00
// Sys类型的消息正文不包含微信 Id所以不需要处理
if m.IsGroup() && m.Type != types.MsgTypeSys {
2023-12-04 14:17:32 +08:00
// 群消息,处理一下消息和发信人
groupUser := strings.Split(m.Content, "\n")[0]
groupUser = strings.ReplaceAll(groupUser, ":", "")
// 如果两个id一致说明是系统发的
if m.FromUser != groupUser {
m.GroupUser = groupUser
2023-09-21 17:33:59 +08:00
}
2023-12-04 14:17:32 +08:00
// 用户的操作单独提出来处理一下
m.Content = strings.Join(strings.Split(m.Content, "\n")[1:], "\n")
2023-09-21 17:33:59 +08:00
}
2023-12-04 14:17:32 +08:00
log.Printf("%s\n消息来源: %s\n群成员: %s\n消息类型: %v\n消息内容: %s", remoteAddr, m.FromUser, m.GroupUser, m.Type, m.Content)
2023-12-11 10:44:23 +08:00
// 插件不为空,开始执行
if p := current.GetRobotMessageHandler(); p != nil {
p(&m)
}
2023-09-21 17:33:59 +08:00
}