github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/message.go (about) 1 package client 2 3 import ( 4 "github.com/LagrangeDev/LagrangeGo/client/packets/pb/action" 5 "github.com/LagrangeDev/LagrangeGo/client/packets/pb/message" 6 message2 "github.com/LagrangeDev/LagrangeGo/message" 7 "github.com/LagrangeDev/LagrangeGo/utils" 8 "github.com/LagrangeDev/LagrangeGo/utils/crypto" 9 "github.com/RomiChan/protobuf/proto" 10 ) 11 12 func (c *QQClient) SendRawMessage(route *message.RoutingHead, body *message.MessageBody) (resp *action.SendMessageResponse, err error) { 13 msg := &message.Message{ 14 RoutingHead: route, 15 ContentHead: &message.ContentHead{ 16 Type: 1, 17 SubType: proto.Some(uint32(0)), 18 DivSeq: proto.Some(uint32(0)), 19 }, 20 Body: body, 21 Seq: proto.Some(c.getSequence()), 22 Rand: proto.Some(crypto.RandU32()), 23 } 24 // grp_id not null 25 if (route.Grp != nil && route.Grp.GroupCode.IsSome()) || (route.GrpTmp != nil && route.GrpTmp.GroupUin.IsSome()) { 26 msg.Ctrl = &message.MessageControl{MsgFlag: int32(utils.TimeStamp())} 27 } 28 29 data, err := proto.Marshal(msg) 30 if err != nil { 31 return 32 } 33 34 packet, err := c.sendUniPacketAndWait("MessageSvc.PbSendMsg", data) 35 if err != nil { 36 return 37 } 38 resp = &action.SendMessageResponse{} 39 err = proto.Unmarshal(packet, resp) 40 if err != nil { 41 return 42 } 43 return 44 } 45 46 func (c *QQClient) SendGroupMessage(groupUin uint32, elements []message2.IMessageElement) (resp *action.SendMessageResponse, err error) { 47 elements = c.preprocessGroupMessage(groupUin, elements) 48 body := message2.PackElementsToBody(elements) 49 route := &message.RoutingHead{ 50 Grp: &message.Grp{GroupCode: proto.Some(groupUin)}, 51 } 52 return c.SendRawMessage(route, body) 53 } 54 55 func (c *QQClient) SendPrivateMessage(uin uint32, elements []message2.IMessageElement) (resp *action.SendMessageResponse, err error) { 56 elements = c.preprocessPrivateMessage(uin, elements) 57 body := message2.PackElementsToBody(elements) 58 route := &message.RoutingHead{ 59 C2C: &message.C2C{ 60 Uid: proto.Some(c.GetUid(uin)), 61 }, 62 } 63 return c.SendRawMessage(route, body) 64 } 65 66 func (c *QQClient) SendTempMessage(groupID uint32, uin uint32, elements []message2.IMessageElement) (resp *action.SendMessageResponse, err error) { 67 body := message2.PackElementsToBody(elements) 68 route := &message.RoutingHead{ 69 GrpTmp: &message.GrpTmp{ 70 GroupUin: proto.Some(groupID), 71 ToUin: proto.Some(uin), 72 }, 73 } 74 return c.SendRawMessage(route, body) 75 } 76 77 func (c *QQClient) preprocessGroupMessage(groupUin uint32, elements []message2.IMessageElement) []message2.IMessageElement { 78 for _, element := range elements { 79 switch elem := element.(type) { 80 case *message2.AtElement: 81 member, _ := c.GetCachedMemberInfo(elem.Target, groupUin) 82 if member != nil { 83 elem.UID = member.Uid 84 if member.MemberCard != "" { 85 elem.Display = "@" + member.MemberCard 86 } else { 87 elem.Display = "@" + member.MemberName 88 } 89 } 90 case *message2.GroupImageElement: 91 _, err := c.ImageUploadGroup(groupUin, elem) 92 if err != nil { 93 c.errorln(err) 94 } 95 if elem.MsgInfo == nil { 96 c.errorln("ImageUploadGroup failed") 97 continue 98 } 99 default: 100 } 101 } 102 return elements 103 } 104 105 func (c *QQClient) preprocessPrivateMessage(targetUin uint32, elements []message2.IMessageElement) []message2.IMessageElement { 106 for _, element := range elements { 107 switch elem := element.(type) { 108 case *message2.FriendImageElement: 109 targetUid := c.GetUid(targetUin) 110 _, err := c.ImageUploadPrivate(targetUid, elem) 111 if err != nil { 112 c.errorln(err) 113 } 114 if elem.MsgInfo == nil { 115 c.errorln("ImageUploadPrivate failed") 116 continue 117 } 118 default: 119 } 120 } 121 return elements 122 }