From b4d790df532e601b66245b2c35a3cf2749730a1f Mon Sep 17 00:00:00 2001 From: mcoo <26184088+mcoo@users.noreply.github.com> Date: Tue, 24 Oct 2023 21:49:02 +0800 Subject: [PATCH] feat: add friendMsg Parse --- go.mod | 1 + v2/events/events.go | 60 ++++++++++--- v2/events/events_easyjson.go | 160 ++++++++++++++--------------------- v2/example/main.go | 8 ++ v2/opqbot.go | 16 ++-- 5 files changed, 128 insertions(+), 117 deletions(-) diff --git a/go.mod b/go.mod index ab793c7..7dcac80 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/dop251/goja v0.0.0-20210427212725-462d53687b0d github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9 github.com/gorilla/websocket v1.4.2 + github.com/mailru/easyjson v0.7.7 // indirect github.com/mcoo/requests v0.0.2 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.8.2 // indirect diff --git a/v2/events/events.go b/v2/events/events.go index 5c978a7..a2f38f9 100644 --- a/v2/events/events.go +++ b/v2/events/events.go @@ -2,10 +2,11 @@ package events import ( "context" - "encoding/json" "strings" ) +//go:generate easyjson events.go + type EventName string const ( @@ -48,11 +49,20 @@ type IEvent interface { GetRawBytes() []byte GetEventName() EventName ParseGroupMsg() IGroupMsg + ParseFriendMsg() IFriendMsg ParseLoginSuccessEvent() ILoginSuccess ParseNetworkChangeEvent() INetworkChange PraseGroupJoinEvent() IGroupJoinEvent ExcludeBot() IEvent } +type IFriendMsg interface { + ICommonMsg + GetFriendUin() int64 + GetFriendUid() string + GetSenderUin() int64 + ParseTextMsg() ITextMsg + ParsePicMsg() IPicMsg +} type IGroupMsg interface { ICommonMsg ExcludeAtInfo() IGroupMsg @@ -63,15 +73,18 @@ type IGroupMsg interface { GetSenderNick() string GetSenderUin() int64 ParseTextMsg() ITextMsg + ParsePicMsg() IPicMsg ContainedPic() bool ContainedAt() bool - GetMsgSeq() int64 - GetMsgRandom() int64 IsFromBot() bool } type ITextMsg interface { GetTextContent() string } +type IPicMsg interface { + GetPics() []Image + GetTextContent() string +} type ILoginSuccess interface { GetLoginSuccessBot() (nick string, uin int64) } @@ -88,6 +101,8 @@ type ICommonMsg interface { GetMsgUid() int64 GetMsgType() MsgType GetMsgTime() int64 + GetMsgSeq() int64 + GetMsgRandom() int64 } func New(data []byte) (IEvent, error) { @@ -97,11 +112,9 @@ func New(data []byte) (IEvent, error) { return nil, err } event.rawEvent = data - return event, json.Unmarshal(data, event) + return event, err } -//go:generate easyjson events.go - type GroupInfo struct { GroupCard string `json:"GroupCard"` GroupCode int `json:"GroupCode"` @@ -115,6 +128,12 @@ type UserInfo struct { Nick string `json:"Nick"` Uin int64 `json:"Uin"` } +type Image struct { + FileId int64 `json:"FileId"` + FileMd5 string `json:"FileMd5"` + FileSize int `json:"FileSize"` + Url string `json:"Url"` +} //easyjson:json type EventStruct struct { @@ -126,9 +145,11 @@ type EventStruct struct { Content *string `json:"Content,omitempty"` MsgHead *struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -140,14 +161,9 @@ type EventStruct struct { C2CTempMessageHead interface{} `json:"C2CTempMessageHead"` } `json:"MsgHead,omitempty"` MsgBody *struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -260,12 +276,21 @@ func (e *EventStruct) ParseTextMsg() ITextMsg { func (e *EventStruct) ParseGroupMsg() IGroupMsg { return e } +func (e *EventStruct) ParseFriendMsg() IFriendMsg { + return e +} func (e *EventStruct) GetMsgUid() int64 { return e.CurrentPacket.EventData.MsgHead.MsgUid } func (e *EventStruct) GetGroupUin() int64 { return e.CurrentPacket.EventData.MsgHead.FromUin } +func (e *EventStruct) GetFriendUin() int64 { + return e.CurrentPacket.EventData.MsgHead.FromUin +} +func (e *EventStruct) GetFriendUid() string { + return e.CurrentPacket.EventData.MsgHead.FromUid +} func (e *EventStruct) GetGroupInfo() GroupInfo { return e.CurrentPacket.EventData.MsgHead.GroupInfo } @@ -283,3 +308,10 @@ func (e *EventStruct) GetLoginSuccessBot() (nick string, uin int64) { uin = *e.CurrentPacket.EventData.Uin return } + +func (e *EventStruct) ParsePicMsg() IPicMsg { + return e +} +func (e *EventStruct) GetPics() []Image { + return e.CurrentPacket.EventData.MsgBody.Images +} diff --git a/v2/events/events_easyjson.go b/v2/events/events_easyjson.go index 2d6415b..00ef2a4 100644 --- a/v2/events/events_easyjson.go +++ b/v2/events/events_easyjson.go @@ -97,9 +97,11 @@ func easyjson692db02bDecode(in *jlexer.Lexer, out *struct { Content *string `json:"Content,omitempty"` MsgHead *struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -111,14 +113,9 @@ func easyjson692db02bDecode(in *jlexer.Lexer, out *struct { C2CTempMessageHead interface{} `json:"C2CTempMessageHead"` } `json:"MsgHead,omitempty"` MsgBody *struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -170,9 +167,11 @@ func easyjson692db02bEncode(out *jwriter.Writer, in struct { Content *string `json:"Content,omitempty"` MsgHead *struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -184,14 +183,9 @@ func easyjson692db02bEncode(out *jwriter.Writer, in struct { C2CTempMessageHead interface{} `json:"C2CTempMessageHead"` } `json:"MsgHead,omitempty"` MsgBody *struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -225,9 +219,11 @@ func easyjson692db02bDecode1(in *jlexer.Lexer, out *struct { Content *string `json:"Content,omitempty"` MsgHead *struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -239,14 +235,9 @@ func easyjson692db02bDecode1(in *jlexer.Lexer, out *struct { C2CTempMessageHead interface{} `json:"C2CTempMessageHead"` } `json:"MsgHead,omitempty"` MsgBody *struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -313,9 +304,11 @@ func easyjson692db02bDecode1(in *jlexer.Lexer, out *struct { if out.MsgHead == nil { out.MsgHead = new(struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -336,14 +329,9 @@ func easyjson692db02bDecode1(in *jlexer.Lexer, out *struct { } else { if out.MsgBody == nil { out.MsgBody = new(struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -381,9 +369,11 @@ func easyjson692db02bEncode1(out *jwriter.Writer, in struct { Content *string `json:"Content,omitempty"` MsgHead *struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -395,14 +385,9 @@ func easyjson692db02bEncode1(out *jwriter.Writer, in struct { C2CTempMessageHead interface{} `json:"C2CTempMessageHead"` } `json:"MsgHead,omitempty"` MsgBody *struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -539,14 +524,9 @@ func easyjson692db02bEncode4(out *jwriter.Writer, in struct { out.RawByte('}') } func easyjson692db02bDecode3(in *jlexer.Lexer, out *struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -581,31 +561,16 @@ func easyjson692db02bDecode3(in *jlexer.Lexer, out *struct { in.Delim('[') if out.Images == nil { if !in.IsDelim(']') { - out.Images = make([]struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - }, 0, 1) + out.Images = make([]Image, 0, 1) } else { - out.Images = []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - }{} + out.Images = []Image{} } } else { out.Images = (out.Images)[:0] } for !in.IsDelim(']') { - var v1 struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } - easyjson692db02bDecode5(in, &v1) + var v1 Image + easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events1(in, &v1) out.Images = append(out.Images, v1) in.WantComma() } @@ -628,7 +593,7 @@ func easyjson692db02bDecode3(in *jlexer.Lexer, out *struct { } for !in.IsDelim(']') { var v2 UserInfo - easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events1(in, &v2) + easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events2(in, &v2) out.AtUinLists = append(out.AtUinLists, v2) in.WantComma() } @@ -661,14 +626,9 @@ func easyjson692db02bDecode3(in *jlexer.Lexer, out *struct { } } func easyjson692db02bEncode3(out *jwriter.Writer, in struct { - SubMsgType int `json:"SubMsgType"` - Content string `json:"Content"` - Images []struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` - } `json:"Images"` + SubMsgType int `json:"SubMsgType"` + Content string `json:"Content"` + Images []Image `json:"Images"` AtUinLists []UserInfo `json:"AtUinLists"` Video interface{} `json:"Video"` Voice interface{} `json:"Voice"` @@ -697,7 +657,7 @@ func easyjson692db02bEncode3(out *jwriter.Writer, in struct { if v3 > 0 { out.RawByte(',') } - easyjson692db02bEncode5(out, v4) + easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events1(out, v4) } out.RawByte(']') } @@ -713,7 +673,7 @@ func easyjson692db02bEncode3(out *jwriter.Writer, in struct { if v5 > 0 { out.RawByte(',') } - easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events1(out, v6) + easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events2(out, v6) } out.RawByte(']') } @@ -742,7 +702,7 @@ func easyjson692db02bEncode3(out *jwriter.Writer, in struct { } out.RawByte('}') } -func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events1(in *jlexer.Lexer, out *UserInfo) { +func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events2(in *jlexer.Lexer, out *UserInfo) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -775,7 +735,7 @@ func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events1(in *jlexer.Lexer, out in.Consumed() } } -func easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events1(out *jwriter.Writer, in UserInfo) { +func easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events2(out *jwriter.Writer, in UserInfo) { out.RawByte('{') first := true _ = first @@ -791,12 +751,7 @@ func easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events1(out *jwriter.Writer, i } out.RawByte('}') } -func easyjson692db02bDecode5(in *jlexer.Lexer, out *struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` -}) { +func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events1(in *jlexer.Lexer, out *Image) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -833,12 +788,7 @@ func easyjson692db02bDecode5(in *jlexer.Lexer, out *struct { in.Consumed() } } -func easyjson692db02bEncode5(out *jwriter.Writer, in struct { - FileId int64 `json:"FileId"` - FileMd5 string `json:"FileMd5"` - FileSize int `json:"FileSize"` - Url string `json:"Url"` -}) { +func easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events1(out *jwriter.Writer, in Image) { out.RawByte('{') first := true _ = first @@ -866,9 +816,11 @@ func easyjson692db02bEncode5(out *jwriter.Writer, in struct { } func easyjson692db02bDecode2(in *jlexer.Lexer, out *struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -899,12 +851,16 @@ func easyjson692db02bDecode2(in *jlexer.Lexer, out *struct { switch key { case "FromUin": out.FromUin = int64(in.Int64()) + case "FromUid": + out.FromUid = string(in.String()) case "ToUin": out.ToUin = int64(in.Int64()) case "FromType": out.FromType = int(in.Int()) case "SenderUin": out.SenderUin = int64(in.Int64()) + case "SenderUid": + out.SenderUid = string(in.String()) case "SenderNick": out.SenderNick = string(in.String()) case "MsgType": @@ -920,7 +876,7 @@ func easyjson692db02bDecode2(in *jlexer.Lexer, out *struct { case "MsgUid": out.MsgUid = int64(in.Int64()) case "GroupInfo": - easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events2(in, &out.GroupInfo) + easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events3(in, &out.GroupInfo) case "C2CTempMessageHead": if m, ok := out.C2CTempMessageHead.(easyjson.Unmarshaler); ok { m.UnmarshalEasyJSON(in) @@ -941,9 +897,11 @@ func easyjson692db02bDecode2(in *jlexer.Lexer, out *struct { } func easyjson692db02bEncode2(out *jwriter.Writer, in struct { FromUin int64 `json:"FromUin"` + FromUid string `json:"FromUid"` ToUin int64 `json:"ToUin"` FromType int `json:"FromType"` SenderUin int64 `json:"SenderUin"` + SenderUid string `json:"SenderUid"` SenderNick string `json:"SenderNick"` MsgType int `json:"MsgType"` C2CCmd int `json:"C2cCmd"` @@ -962,6 +920,11 @@ func easyjson692db02bEncode2(out *jwriter.Writer, in struct { out.RawString(prefix[1:]) out.Int64(int64(in.FromUin)) } + { + const prefix string = ",\"FromUid\":" + out.RawString(prefix) + out.String(string(in.FromUid)) + } { const prefix string = ",\"ToUin\":" out.RawString(prefix) @@ -977,6 +940,11 @@ func easyjson692db02bEncode2(out *jwriter.Writer, in struct { out.RawString(prefix) out.Int64(int64(in.SenderUin)) } + { + const prefix string = ",\"SenderUid\":" + out.RawString(prefix) + out.String(string(in.SenderUid)) + } { const prefix string = ",\"SenderNick\":" out.RawString(prefix) @@ -1015,7 +983,7 @@ func easyjson692db02bEncode2(out *jwriter.Writer, in struct { { const prefix string = ",\"GroupInfo\":" out.RawString(prefix) - easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events2(out, in.GroupInfo) + easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events3(out, in.GroupInfo) } { const prefix string = ",\"C2CTempMessageHead\":" @@ -1030,7 +998,7 @@ func easyjson692db02bEncode2(out *jwriter.Writer, in struct { } out.RawByte('}') } -func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events2(in *jlexer.Lexer, out *GroupInfo) { +func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events3(in *jlexer.Lexer, out *GroupInfo) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -1073,7 +1041,7 @@ func easyjson692db02bDecodeGithubComOpqOscOPQBotV2Events2(in *jlexer.Lexer, out in.Consumed() } } -func easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events2(out *jwriter.Writer, in GroupInfo) { +func easyjson692db02bEncodeGithubComOpqOscOPQBotV2Events3(out *jwriter.Writer, in GroupInfo) { out.RawByte('{') first := true _ = first diff --git a/v2/example/main.go b/v2/example/main.go index 43c0d68..4427a6a 100644 --- a/v2/example/main.go +++ b/v2/example/main.go @@ -39,6 +39,14 @@ func main() { } }) + core.On(events.EventNameFriendMsg, func(ctx context.Context, event events.IEvent) { + if event.GetMsgType() == events.MsgTypeFriendsMsg { + friendMsg := event.ParseFriendMsg() + if friendMsg.ParseTextMsg().GetTextContent() == "test" && friendMsg.GetSenderUin() != event.GetCurrentQQ() { + apiBuilder.New(apiUrl, event.GetCurrentQQ()).SendMsg().FriendMsg().ToUin(friendMsg.GetFriendUin()).TextMsg("test").Do(ctx) + } + } + }) err = core.ListenAndWait(context.Background()) if err != nil { panic(err) diff --git a/v2/opqbot.go b/v2/opqbot.go index 2891787..5d380c3 100644 --- a/v2/opqbot.go +++ b/v2/opqbot.go @@ -2,6 +2,13 @@ package OPQBot import ( "context" + "net/url" + "os" + "os/signal" + "runtime/debug" + "sync" + "time" + "github.com/charmbracelet/log" "github.com/gorilla/websocket" "github.com/jasonlvhit/gocron" @@ -9,12 +16,6 @@ import ( "github.com/opq-osc/OPQBot/v2/errors" "github.com/opq-osc/OPQBot/v2/events" "github.com/rotisserie/eris" - "net/url" - "os" - "os/signal" - "runtime/debug" - "sync" - "time" ) type Core struct { @@ -110,12 +111,13 @@ func (c *Core) ListenAndWait(ctx context.Context) (e error) { c.err = err return } + log.Debug(string(message)) event, err := events.New(message) if err != nil { log.Error("error:", eris.ToString(err, true)) continue } - log.Debug(string(message)) + var callbacks []events.EventCallbackFunc c.lock.RLock() callbacks = c.events[event.GetEventName()]