Skip to content

Commit

Permalink
feat: 完善自己消息的回调,好友转账的回调,消息撤回的回调,系统消息的回调
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Feb 3, 2023
1 parent 953e219 commit 652bfe5
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 94 deletions.
11 changes: 9 additions & 2 deletions engine/robot/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package robot
const (
EventGroupChat = "EventGroupChat" // 群聊消息事件
EventPrivateChat = "EventPrivateChat" // 私聊消息事件
EventSelfMessage = "EventSelfMessage" // 自己发的消息事件
EventFriendVerify = "EventFriendVerify" // 好友请求事件
EventTransfer = "EventTransfer" // 好友转账事件
EventMessageWithdraw = "EventMessageWithdraw" // 消息撤回事件
EventGroupNameChange = "EventGroupNameChange" // 群名称变动事件
EventGroupMemberAdd = "EventGroupMemberAdd" // 群成员增加事件
EventSystem = "EventSystem" // 系统消息事件
EventGroupMemberIncrease = "EventGroupMemberIncrease" // 群成员增加事件
EventGroupMemberDecrease = "EventGroupMemberDecrease" // 群成员减少事件
EventInvitedInGroup = "EventInvitedInGroup" // 被邀请入群事件
)
Expand Down Expand Up @@ -99,6 +101,11 @@ func (ctx *Ctx) IsEventGroupChat() bool {
return ctx.Event.Type == EventGroupChat
}

// IsEventSelfMessage 判断消息是否是机器人自己发出的消息
func (ctx *Ctx) IsEventSelfMessage() bool {
return ctx.Event.Type == EventSelfMessage
}

// IsEventFriendVerify 判断消息是否是好友请求消息
func (ctx *Ctx) IsEventFriendVerify() bool {
return ctx.Event.Type == EventFriendVerify
Expand Down
52 changes: 42 additions & 10 deletions engine/robot/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package robot

// Event 记录一次回调事件
type Event struct {
Type string // 消息类型
RobotWxId string // 机器人微信id
IsAtMe bool // 机器人是否被@了,@所有人不算
FromUniqueID string // 消息来源唯一id, 私聊为发送者微信id, 群聊为群id
FromWxId string // 消息来源微信id
FromName string // 消息来源昵称
FromGroup string // 消息来源群id
FromGroupName string // 消息来源群名称
Message *Message // 消息内容
FriendVerify *FriendVerify // 好友验证消息
Type string // 消息类型
RobotWxId string // 机器人微信id
IsAtMe bool // 机器人是否被@了,@所有人不算
FromUniqueID string // 消息来源唯一id, 私聊为发送者微信id, 群聊为群id
FromWxId string // 消息来源微信id
FromName string // 消息来源昵称
FromGroup string // 消息来源群id
FromGroupName string // 消息来源群名称
Message *Message // 消息内容
FriendVerify *FriendVerify // 好友验证消息
Transfer *Transfer // 转账消息
Withdraw *Withdraw // 撤回消息
GroupMemberIncrease *GroupMemberIncrease // 群成员增加消息
GroupMemberDecrease *GroupMemberDecrease // 群成员减少消息
}

// Message 记录消息的具体内容
Expand All @@ -33,3 +37,31 @@ type FriendVerify struct {
Content string // 验证内容
Scene string // 验证场景
}

// Transfer 记录转账消息的具体内容
type Transfer struct {
FromWxId string // 发送者微信ID
MsgSource int64 // 消息来源 1:收到转账 2:对方接收转账 3:发出转账 4:自己接收转账 5:对方退还 6:自己退还
TransferType int64 // 转账类型 1:即时到账 2:延时到账
Money string // 转账金额,单位元
Memo string // 转账备注
TransferId string // 转账ID
TransferTime string // 转账时间,10位时间戳
}

// Withdraw 记录撤回消息的具体内容
type Withdraw struct {
FromType int64 // 消息来源 1:私聊 2:群聊
FromGroup string // 消息来源群ID
FromWxId string // 消息来源微信ID
MsgSource int64 // 消息来源 1:别人撤回 2:自己使用手机撤回 3:自己使用电脑撤回
Msg string // 消息内容
}

// GroupMemberIncrease 记录群成员增加消息的具体内容
type GroupMemberIncrease struct {
}

// GroupMemberDecrease 记录群成员减少消息的具体内容
type GroupMemberDecrease struct {
}
22 changes: 19 additions & 3 deletions engine/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,27 @@ loop:
func preProcessMessageEvent(e *Event) {
switch e.Type {
case EventPrivateChat:
log.Println(fmt.Sprintf("收到私聊(%s)消息 ==> %v", e.FromWxId, e.Message.Content))
log.Println(fmt.Sprintf("[回调]收到私聊(%s)消息 ==> %v", e.FromWxId, e.Message.Content))
case EventGroupChat:
log.Println(fmt.Sprintf("收到群聊(%s[%s])消息 ==> %v", e.FromGroup, e.FromWxId, e.Message.Content))
log.Println(fmt.Sprintf("[回调]收到群聊(%s[%s])消息 ==> %v", e.FromGroup, e.FromWxId, e.Message.Content))
case EventSelfMessage:
log.Println(fmt.Sprintf("[回调]收到自己发送的消息 ==> %v", e.Message.Content))
case EventFriendVerify:
log.Println(fmt.Sprintf("收到好友验证消息, wxId:%s, nick:%s, content:%s", e.FriendVerify.WxId, e.FriendVerify.Nick, e.FriendVerify.Content))
log.Println(fmt.Sprintf("[回调]收到好友验证消息, wxId:%s, nick:%s, content:%s", e.FriendVerify.WxId, e.FriendVerify.Nick, e.FriendVerify.Content))
case EventTransfer:
if len(e.Transfer.Memo) > 0 {
log.Println(fmt.Sprintf("[回调]收到转账消息, wxId:%s, money:%s, memo:%s", e.Transfer.FromWxId, e.Transfer.Money, e.Transfer.Memo))
} else {
log.Println(fmt.Sprintf("[回调]收到转账消息, wxId:%s, money:%s", e.Transfer.FromWxId, e.Transfer.Money))
}
case EventMessageWithdraw:
if e.Withdraw.FromType == 1 {
log.Println(fmt.Sprintf("[回调]收到撤回私聊(%s)消息 ==> %s", e.Withdraw.FromWxId, e.Withdraw.Msg))
} else if e.Withdraw.FromType == 2 {
log.Println(fmt.Sprintf("[回调]收到撤回群聊(%s[%s])消息 ==> %s", e.Withdraw.FromGroup, e.Withdraw.FromWxId, e.Withdraw.Msg))
}
case EventSystem:
log.Println(fmt.Sprintf("[回调]收到系统消息 ==> %s", e.Message.Content))
}
}

Expand Down
135 changes: 90 additions & 45 deletions framework/qianxun/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/tidwall/gjson"
"github.com/yqchilde/pkgs/net"

"github.com/yqchilde/pkgs/net"
"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/robot"
)
Expand Down Expand Up @@ -48,50 +48,9 @@ func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
log.Errorf("[千寻] 接收回调错误, error: %v", err)
return
}
body := string(recv)
event := robot.Event{RobotWxId: gjson.Get(body, "wxid").String()}
switch gjson.Get(body, "event").Int() {
case eventPrivateChat:
event.Type = robot.EventPrivateChat
event.FromUniqueID = gjson.Get(body, "data.data.fromWxid").String()
event.FromWxId = gjson.Get(body, "data.data.fromWxid").String()
event.FromName = ""
event.IsAtMe = true
event.Message = &robot.Message{
Id: "",
Type: gjson.Get(body, "data.data.msgType").Int(),
Content: gjson.Get(body, "data.data.msg").String(),
}
case eventGroupChat:
event.Type = robot.EventGroupChat
event.FromUniqueID = gjson.Get(body, "data.data.fromWxid").String()
event.FromGroup = gjson.Get(body, "data.data.fromWxid").String()
event.FromGroupName = ""
event.FromWxId = gjson.Get(body, "data.data.finalFromWxid").String()
event.FromName = ""
event.Message = &robot.Message{
Id: "",
Type: gjson.Get(body, "data.data.msgType").Int(),
Content: gjson.Get(body, "data.data.msg").String(),
}
if gjson.Get(body, fmt.Sprintf("data.data.atWxidList.#(==%s)", event.RobotWxId)).Exists() {
if !strings.Contains(event.Message.Content, "@所有人") {
event.IsAtMe = true
}
}
case eventFriendVerify:
event.Type = robot.EventFriendVerify
event.FriendVerify = &robot.FriendVerify{
WxId: gjson.Get(body, "data.data.wxid").String(),
Nick: gjson.Get(body, "data.data.nick").String(),
V3: gjson.Get(body, "data.data.v3").String(),
V4: gjson.Get(body, "data.data.v4").String(),
AvatarUrl: gjson.Get(body, "data.data.avatarMinUrl").String(),
Content: gjson.Get(body, "data.data.content").String(),
Scene: gjson.Get(body, "data.data.scene").String(),
}
}
handler(&event, f)
resp := string(recv)
event := buildEvent(resp)
handler(event, f)
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(`{"code":0}`))
})
Expand All @@ -108,3 +67,89 @@ func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
log.Fatalf("[千寻] WxBot回调服务启动失败, error: %v", err)
}
}

func buildEvent(resp string) *robot.Event {
event := robot.Event{RobotWxId: gjson.Get(resp, "wxid").String()}
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(),
}
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(),
}
if gjson.Get(resp, fmt.Sprintf("data.data.atWxidList.#(==%s)", event.RobotWxId)).Exists() {
if !strings.Contains(event.Message.Content, "@所有人") {
event.IsAtMe = true
}
}
}
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(),
}
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(),
}
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(),
}
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()
}
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(),
}
case eventPayment:
// todo
}
return &event
}
Loading

0 comments on commit 652bfe5

Please sign in to comment.