@@ -19,6 +19,7 @@ package msgconv
1919import (
2020 "bytes"
2121 "context"
22+ "encoding/json"
2223 "errors"
2324 "fmt"
2425 "image"
@@ -99,7 +100,11 @@ func (mc *MessageConverter) ToWhatsApp(
99100
100101 switch content .MsgType {
101102 case event .MsgText , event .MsgNotice , event .MsgEmote :
102- message = mc .constructTextMessage (ctx , content , contextInfo )
103+ var err error
104+ message , err = mc .constructTextMessage (ctx , content , evt .Content .Raw , contextInfo )
105+ if err != nil {
106+ return nil , nil , err
107+ }
103108 case event .MessageType (event .EventSticker .Type ), event .MsgImage , event .MsgVideo , event .MsgAudio , event .MsgFile :
104109 uploaded , thumbnail , mime , err := mc .reuploadFileToWhatsApp (ctx , content )
105110 if err != nil {
@@ -304,7 +309,16 @@ func (mc *MessageConverter) parseText(ctx context.Context, content *event.Messag
304309 return
305310}
306311
307- func (mc * MessageConverter ) constructTextMessage (ctx context.Context , content * event.MessageEventContent , contextInfo * waE2E.ContextInfo ) * waE2E.Message {
312+ func (mc * MessageConverter ) constructTextMessage (
313+ ctx context.Context ,
314+ content * event.MessageEventContent ,
315+ raw map [string ]any ,
316+ contextInfo * waE2E.ContextInfo ,
317+ ) (* waE2E.Message , error ) {
318+ groupInvite , ok := raw [GroupInviteMetaField ].(map [string ]any )
319+ if ok {
320+ return mc .constructGroupInviteMessage (ctx , content , groupInvite , contextInfo )
321+ }
308322 text , mentions := mc .parseText (ctx , content )
309323 if len (mentions ) > 0 {
310324 contextInfo .MentionedJID = mentions
@@ -315,7 +329,44 @@ func (mc *MessageConverter) constructTextMessage(ctx context.Context, content *e
315329 }
316330 mc .convertURLPreviewToWhatsApp (ctx , content , etm )
317331
318- return & waE2E.Message {ExtendedTextMessage : etm }
332+ return & waE2E.Message {ExtendedTextMessage : etm }, nil
333+ }
334+
335+ func (mc * MessageConverter ) constructGroupInviteMessage (
336+ ctx context.Context ,
337+ content * event.MessageEventContent ,
338+ inviteMeta map [string ]any ,
339+ contextInfo * waE2E.ContextInfo ,
340+ ) (* waE2E.Message , error ) {
341+ payload , err := json .Marshal (inviteMeta )
342+ if err != nil {
343+ return nil , fmt .Errorf ("failed to marshal invite meta: %w" , err )
344+ }
345+ var parsedInviteMeta waid.GroupInviteMeta
346+ err = json .Unmarshal (payload , & parsedInviteMeta )
347+ if err != nil {
348+ return nil , fmt .Errorf ("failed to parse invite meta: %w" , err )
349+ }
350+ text , mentions := mc .parseText (ctx , content )
351+ if len (mentions ) > 0 {
352+ contextInfo .MentionedJID = mentions
353+ }
354+ groupType := waE2E .GroupInviteMessage_DEFAULT
355+ if parsedInviteMeta .IsParentGroup {
356+ groupType = waE2E .GroupInviteMessage_PARENT
357+ }
358+ return & waE2E.Message {
359+ GroupInviteMessage : & waE2E.GroupInviteMessage {
360+ GroupJID : proto .String (parsedInviteMeta .JID .String ()),
361+ InviteCode : proto .String (parsedInviteMeta .Code ),
362+ InviteExpiration : proto .Int64 (parsedInviteMeta .Expiration ),
363+ GroupName : proto .String (parsedInviteMeta .GroupName ),
364+ JPEGThumbnail : nil ,
365+ Caption : proto .String (text ),
366+ ContextInfo : contextInfo ,
367+ GroupType : groupType .Enum (),
368+ },
369+ }, nil
319370}
320371
321372func (mc * MessageConverter ) convertPill (displayname , mxid , eventID string , ctx format.Context ) string {
0 commit comments