Skip to content

Commit

Permalink
feat: 增加返回消息源数据,获取方式为 ctx.Event.RawMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Feb 7, 2023
1 parent 40df7bd commit cce2f26
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 107 deletions.
1 change: 1 addition & 0 deletions engine/robot/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package robot
const (
EventGroupChat = "EventGroupChat" // 群聊消息事件
EventPrivateChat = "EventPrivateChat" // 私聊消息事件
EventSubscription = "EventSubscription" // 订阅号消息事件
EventSelfMessage = "EventSelfMessage" // 自己发的消息事件
EventFriendVerify = "EventFriendVerify" // 好友请求事件
EventTransfer = "EventTransfer" // 好友转账事件
Expand Down
1 change: 1 addition & 0 deletions engine/robot/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Event struct {
FromName string // 消息来源昵称
FromGroup string // 消息来源群id
FromGroupName string // 消息来源群名称
RawMessage string // 原始消息
Message *Message // 消息内容
FriendVerify *FriendVerify // 好友验证消息
Transfer *Transfer // 转账消息
Expand Down
170 changes: 111 additions & 59 deletions framework/qianxun/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
return
}
resp := string(recv)
event := buildEvent(resp)
event := buildEvent(resp, f)
handler(event, f)
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(`{"code":0}`))
Expand All @@ -67,88 +67,140 @@ func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
}
}

func buildEvent(resp string) *robot.Event {
event := robot.Event{RobotWxId: gjson.Get(resp, "wxid").String()}
func buildEvent(resp string, f *Framework) *robot.Event {
var event robot.Event
switch gjson.Get(resp, "event").Int() {
case eventAccountChange:
// todo
case eventGroupChat:
switch gjson.Get(resp, "data.data.msgType").Int() {
case 10000:
event.Type = robot.EventSystem
event.Message = &robot.Message{
Content: gjson.Get(resp, "data.data.msg").String(),
msgType := gjson.Get(resp, "data.data.msgType").Int()
if msgType == 10000 { // 系统消息
event = robot.Event{
Type: robot.EventSystem,
Message: &robot.Message{
Content: gjson.Get(resp, "data.data.msg").String(),
},
}
default:
event.Type = robot.EventGroupChat
event.FromUniqueID = gjson.Get(resp, "data.data.fromWxid").String()
event.FromGroup = gjson.Get(resp, "data.data.fromWxid").String()
event.FromGroupName = ""
event.FromWxId = gjson.Get(resp, "data.data.finalFromWxid").String()
event.FromName = ""
event.Message = &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
} else { // 群聊
event = robot.Event{
Type: robot.EventGroupChat,
FromUniqueID: gjson.Get(resp, "data.data.fromWxid").String(),
FromGroup: gjson.Get(resp, "data.data.fromWxid").String(),
FromGroupName: "",
FromWxId: gjson.Get(resp, "data.data.finalFromWxid").String(),
FromName: "",
Message: &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
},
}
if gjson.Get(resp, fmt.Sprintf("data.data.atWxidList.#(==%s)", event.RobotWxId)).Exists() {
if !strings.Contains(event.Message.Content, "@所有人") {
event.IsAtMe = true
}
}
for _, group := range robot.WxBot.GroupList {
if group.WxId == event.FromGroup {
event.FromGroupName = group.Nick
break
}
}
}
case eventPrivateChat:
event.Type = robot.EventPrivateChat
event.FromUniqueID = gjson.Get(resp, "data.data.fromWxid").String()
event.FromWxId = gjson.Get(resp, "data.data.fromWxid").String()
event.FromName = ""
event.IsAtMe = true
event.Message = &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
fromType := gjson.Get(resp, "data.data.fromType").Int()
if fromType == 3 { // 公众号
event = robot.Event{
Type: robot.EventSubscription,
FromUniqueID: gjson.Get(resp, "data.data.fromWxid").String(),
FromWxId: gjson.Get(resp, "data.data.fromWxid").String(),
FromName: "",
Message: &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
},
}
} else { // 私聊
event = robot.Event{
Type: robot.EventPrivateChat,
FromUniqueID: gjson.Get(resp, "data.data.fromWxid").String(),
FromWxId: gjson.Get(resp, "data.data.fromWxid").String(),
FromName: "",
IsAtMe: true,
Message: &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
},
}
for _, friend := range robot.WxBot.FriendsList {
if friend.WxId == event.FromWxId {
event.FromName = friend.Nick
break
}
}
}
case eventSelfMessage:
event.Type = robot.EventSelfMessage
event.Message = &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
event = robot.Event{
Type: robot.EventSelfMessage,
Message: &robot.Message{
Type: gjson.Get(resp, "data.data.msgType").Int(),
Content: gjson.Get(resp, "data.data.msg").String(),
},
}
case eventTransfer:
event.Type = robot.EventTransfer
event.Transfer = &robot.Transfer{
FromWxId: gjson.Get(resp, "data.data.fromWxid").String(),
MsgSource: gjson.Get(resp, "data.data.msgSource").Int(),
TransferType: gjson.Get(resp, "data.data.transType").Int(),
Money: gjson.Get(resp, "data.data.money").String(),
Memo: gjson.Get(resp, "data.data.memo").String(),
TransferId: gjson.Get(resp, "data.data.transferid").String(),
TransferTime: gjson.Get(resp, "data.data.invalidtime").String(),
event = robot.Event{
Type: robot.EventTransfer,
Transfer: &robot.Transfer{
FromWxId: gjson.Get(resp, "data.data.fromWxid").String(),
MsgSource: gjson.Get(resp, "data.data.msgSource").Int(),
TransferType: gjson.Get(resp, "data.data.transType").Int(),
Money: gjson.Get(resp, "data.data.money").String(),
Memo: gjson.Get(resp, "data.data.memo").String(),
TransferId: gjson.Get(resp, "data.data.transferid").String(),
TransferTime: gjson.Get(resp, "data.data.invalidtime").String(),
},
}
case eventMessageWithdraw:
event.Type = robot.EventMessageWithdraw
event.Withdraw = &robot.Withdraw{
FromType: gjson.Get(resp, "data.data.fromType").Int(),
MsgSource: gjson.Get(resp, "data.data.msgSource").Int(),
Msg: gjson.Get(resp, "data.data.msg").String(),
}
if event.Withdraw.FromType == 1 {
event.Withdraw.FromWxId = gjson.Get(resp, "data.data.fromWxid").String()
} else if event.Withdraw.FromType == 2 {
event.Withdraw.FromGroup = gjson.Get(resp, "data.data.fromWxid").String()
event.Withdraw.FromWxId = gjson.Get(resp, "data.data.finalFromWxid").String()
fromType := gjson.Get(resp, "data.data.fromType").Int()
if fromType == 1 {
event = robot.Event{
Type: robot.EventMessageWithdraw,
Withdraw: &robot.Withdraw{
FromType: fromType,
FromWxId: gjson.Get(resp, "data.data.fromWxid").String(),
MsgSource: gjson.Get(resp, "data.data.msgSource").Int(),
Msg: gjson.Get(resp, "data.data.msg").String(),
},
}
} else if fromType == 2 {
event = robot.Event{
Type: robot.EventMessageWithdraw,
Withdraw: &robot.Withdraw{
FromType: fromType,
FromGroup: gjson.Get(resp, "data.data.fromWxid").String(),
FromWxId: gjson.Get(resp, "data.data.finalFromWxid").String(),
MsgSource: gjson.Get(resp, "data.data.msgSource").Int(),
Msg: gjson.Get(resp, "data.data.msg").String(),
},
}
}
case eventFriendVerify:
event.Type = robot.EventFriendVerify
event.FriendVerify = &robot.FriendVerify{
WxId: gjson.Get(resp, "data.data.wxid").String(),
Nick: gjson.Get(resp, "data.data.nick").String(),
V3: gjson.Get(resp, "data.data.v3").String(),
V4: gjson.Get(resp, "data.data.v4").String(),
AvatarUrl: gjson.Get(resp, "data.data.avatarMinUrl").String(),
Content: gjson.Get(resp, "data.data.content").String(),
Scene: gjson.Get(resp, "data.data.scene").String(),
event = robot.Event{
Type: robot.EventFriendVerify,
FriendVerify: &robot.FriendVerify{
WxId: gjson.Get(resp, "data.data.wxid").String(),
Nick: gjson.Get(resp, "data.data.nick").String(),
V3: gjson.Get(resp, "data.data.v3").String(),
V4: gjson.Get(resp, "data.data.v4").String(),
AvatarUrl: gjson.Get(resp, "data.data.avatarMinUrl").String(),
Content: gjson.Get(resp, "data.data.content").String(),
Scene: gjson.Get(resp, "data.data.scene").String(),
},
}
case eventPayment:
// todo
}

event.RobotWxId = gjson.Get(resp, "wxid").String()
event.RawMessage = resp
return &event
}
109 changes: 61 additions & 48 deletions framework/vlw/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
return
}
resp := string(recv)
event := buildEvent(resp)
event := buildEvent(resp, f)
handler(event, f)
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(`{"code":0}`))
Expand All @@ -66,28 +66,31 @@ func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
}
}

func buildEvent(resp string) *robot.Event {
event := robot.Event{RobotWxId: gjson.Get(resp, "content.robot_wxid").String()}
func buildEvent(resp string, f *Framework) *robot.Event {
var event robot.Event
switch gjson.Get(resp, "Event").String() {
case eventGroupChat:
// 根据消息类型细分多种事件
switch gjson.Get(resp, "content.type").Int() {
case 10000:
event.Type = robot.EventSystem
event.Message = &robot.Message{
Content: gjson.Get(resp, "content.msg").String(),
contentType := gjson.Get(resp, "content.type").Int()
if contentType == 10000 {
event = robot.Event{
Type: robot.EventSystem,
Message: &robot.Message{
Content: gjson.Get(resp, "content.msg").String(),
},
}
default:
event.Type = robot.EventGroupChat
event.FromUniqueID = gjson.Get(resp, "content.from_group").String()
event.FromGroup = gjson.Get(resp, "content.from_group").String()
event.FromGroupName = gjson.Get(resp, "content.from_group_name").String()
event.FromWxId = gjson.Get(resp, "content.from_wxid").String()
event.FromName = gjson.Get(resp, "content.from_name").String()
event.Message = &robot.Message{
Id: gjson.Get(resp, "content.msg_id").String(),
Type: gjson.Get(resp, "content.type").Int(),
Content: gjson.Get(resp, "content.msg").String(),
} else {
event = robot.Event{
Type: robot.EventGroupChat,
FromUniqueID: gjson.Get(resp, "content.from_group").String(),
FromGroup: gjson.Get(resp, "content.from_group").String(),
FromGroupName: gjson.Get(resp, "content.from_group_name").String(),
FromWxId: gjson.Get(resp, "content.from_wxid").String(),
FromName: gjson.Get(resp, "content.from_name").String(),
Message: &robot.Message{
Id: gjson.Get(resp, "content.msg_id").String(),
Type: gjson.Get(resp, "content.type").Int(),
Content: gjson.Get(resp, "content.msg").String(),
},
}
if gjson.Get(resp, fmt.Sprintf("content.msg_source.atuserlist.#(wxid==%s)", event.RobotWxId)).Exists() {
if !gjson.Get(resp, "content.msg_source.atuserlist.#(nickname==@所有人)").Exists() {
Expand All @@ -96,38 +99,45 @@ func buildEvent(resp string) *robot.Event {
}
}
case eventPrivateChat:
event.FromUniqueID = gjson.Get(resp, "content.from_wxid").String()
event.FromWxId = gjson.Get(resp, "content.from_wxid").String()
event.FromName = gjson.Get(resp, "content.from_name").String()

// 根据消息类型细分多种事件
switch gjson.Get(resp, "content.type").Int() {
case 2000:
event.Type = robot.EventTransfer
event.Transfer = &robot.Transfer{
FromWxId: gjson.Get(resp, "content.from_wxid").String(),
MsgSource: gjson.Get(gjson.Get(resp, "content.msg").String(), "paysubtype").Int(),
Money: gjson.Get(gjson.Get(resp, "content.msg").String(), "money").String(),
Memo: gjson.Get(gjson.Get(resp, "content.msg").String(), "pay_momo").String(),
TransferId: gjson.Get(gjson.Get(resp, "content.msg").String(), "payer_pay_id").String(),
contentType := gjson.Get(resp, "content.type").Int()
if contentType == 2000 {
event = robot.Event{
Type: robot.EventTransfer,
FromUniqueID: gjson.Get(resp, "content.from_wxid").String(),
FromWxId: gjson.Get(resp, "content.from_wxid").String(),
FromName: gjson.Get(resp, "content.from_name").String(),
Transfer: &robot.Transfer{
FromWxId: gjson.Get(resp, "content.from_wxid").String(),
MsgSource: gjson.Get(gjson.Get(resp, "content.msg").String(), "paysubtype").Int(),
Money: gjson.Get(gjson.Get(resp, "content.msg").String(), "money").String(),
Memo: gjson.Get(gjson.Get(resp, "content.msg").String(), "pay_momo").String(),
TransferId: gjson.Get(gjson.Get(resp, "content.msg").String(), "payer_pay_id").String(),
},
}
default:
event.Type = robot.EventPrivateChat
event.IsAtMe = true
event.Message = &robot.Message{
Id: gjson.Get(resp, "content.msg_id").String(),
Type: gjson.Get(resp, "content.type").Int(),
Content: gjson.Get(resp, "content.msg").String(),
} else {
event = robot.Event{
Type: robot.EventPrivateChat,
IsAtMe: true,
FromUniqueID: gjson.Get(resp, "content.from_wxid").String(),
FromWxId: gjson.Get(resp, "content.from_wxid").String(),
FromName: gjson.Get(resp, "content.from_name").String(),
Message: &robot.Message{
Id: gjson.Get(resp, "content.msg_id").String(),
Type: gjson.Get(resp, "content.type").Int(),
Content: gjson.Get(resp, "content.msg").String(),
},
}
}
case eventDeviceCallback:
switch gjson.Get(resp, "content.type").Int() {
case 1:
event.Type = robot.EventSelfMessage // 可能不准确,待反馈
event.Message = &robot.Message{
Id: gjson.Get(resp, "content.msg_id").String(),
Type: gjson.Get(resp, "content.type").Int(),
Content: gjson.Get(resp, "content.msg").String(),
contentType := gjson.Get(resp, "content.type").Int()
if contentType == 1 {
event = robot.Event{
Type: robot.EventSelfMessage, // 可能不准确,待反馈
Message: &robot.Message{
Id: gjson.Get(resp, "content.msg_id").String(),
Type: gjson.Get(resp, "content.type").Int(),
Content: gjson.Get(resp, "content.msg").String(),
},
}
}
case eventFriendVerify:
Expand All @@ -144,5 +154,8 @@ func buildEvent(resp string) *robot.Event {
case eventDownloadFile:
case eventGroupEstablish:
}

event.RobotWxId = gjson.Get(resp, "content.robot_wxid").String()
event.RawMessage = resp
return &event
}

0 comments on commit cce2f26

Please sign in to comment.