-
Notifications
You must be signed in to change notification settings - Fork 205
feat: add forwardingScore support to send text/media #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,14 +58,15 @@ type sendService struct { | |
| } | ||
|
|
||
| type SendDataStruct struct { | ||
| Id string | ||
| Number string | ||
| Delay int32 | ||
| MentionAll bool | ||
| MentionedJID []string | ||
| FormatJid *bool | ||
| Quoted QuotedStruct | ||
| MediaHandle string | ||
| Id string | ||
| Number string | ||
| Delay int32 | ||
| MentionAll bool | ||
| MentionedJID []string | ||
| FormatJid *bool | ||
| Quoted QuotedStruct | ||
| MediaHandle string | ||
| ForwardingScore *uint32 | ||
| } | ||
|
|
||
| type QuotedStruct struct { | ||
|
|
@@ -74,14 +75,15 @@ type QuotedStruct struct { | |
| } | ||
|
|
||
| type TextStruct struct { | ||
| Number string `json:"number"` | ||
| Text string `json:"text"` | ||
| Id string `json:"id"` | ||
| Delay int32 `json:"delay"` | ||
| MentionedJID []string `json:"mentionedJid"` | ||
| MentionAll bool `json:"mentionAll"` | ||
| FormatJid *bool `json:"formatJid,omitempty"` | ||
| Quoted QuotedStruct `json:"quoted"` | ||
| Number string `json:"number"` | ||
| Text string `json:"text"` | ||
| Id string `json:"id"` | ||
| Delay int32 `json:"delay"` | ||
| MentionedJID []string `json:"mentionedJid"` | ||
| MentionAll bool `json:"mentionAll"` | ||
| FormatJid *bool `json:"formatJid,omitempty"` | ||
| Quoted QuotedStruct `json:"quoted"` | ||
| ForwardingScore *uint32 `json:"forwardingScore,omitempty"` | ||
| } | ||
|
|
||
| type LinkStruct struct { | ||
|
|
@@ -100,17 +102,18 @@ type LinkStruct struct { | |
| } | ||
|
|
||
| type MediaStruct struct { | ||
| Number string `json:"number"` | ||
| Url string `json:"url"` | ||
| Type string `json:"type"` | ||
| Caption string `json:"caption"` | ||
| Filename string `json:"filename"` | ||
| Id string `json:"id"` | ||
| Delay int32 `json:"delay"` | ||
| MentionedJID []string `json:"mentionedJid"` | ||
| MentionAll bool `json:"mentionAll"` | ||
| FormatJid *bool `json:"formatJid,omitempty"` | ||
| Quoted QuotedStruct `json:"quoted"` | ||
| Number string `json:"number"` | ||
| Url string `json:"url"` | ||
| Type string `json:"type"` | ||
| Caption string `json:"caption"` | ||
| Filename string `json:"filename"` | ||
| Id string `json:"id"` | ||
| Delay int32 `json:"delay"` | ||
| MentionedJID []string `json:"mentionedJid"` | ||
| MentionAll bool `json:"mentionAll"` | ||
| FormatJid *bool `json:"formatJid,omitempty"` | ||
| Quoted QuotedStruct `json:"quoted"` | ||
| ForwardingScore *uint32 `json:"forwardingScore,omitempty"` | ||
| } | ||
|
|
||
| type PollStruct struct { | ||
|
|
@@ -614,13 +617,14 @@ func (s *sendService) sendTextWithRetry(data *TextStruct, instance *instance_mod | |
| } | ||
|
|
||
| message, err := s.SendMessage(instance, msg, "ExtendedTextMessage", &SendDataStruct{ | ||
| Id: data.Id, | ||
| Number: data.Number, | ||
| Quoted: data.Quoted, | ||
| Delay: data.Delay, | ||
| MentionAll: data.MentionAll, | ||
| MentionedJID: data.MentionedJID, | ||
| FormatJid: data.FormatJid, | ||
| Id: data.Id, | ||
| Number: data.Number, | ||
| Quoted: data.Quoted, | ||
| Delay: data.Delay, | ||
| MentionAll: data.MentionAll, | ||
| MentionedJID: data.MentionedJID, | ||
| FormatJid: data.FormatJid, | ||
| ForwardingScore: data.ForwardingScore, | ||
| }) | ||
|
|
||
| if err != nil { | ||
|
|
@@ -1161,14 +1165,15 @@ func (s *sendService) sendMediaFileWithRetry(data *MediaStruct, fileData []byte, | |
| } | ||
|
|
||
| message, err := s.SendMessage(instance, media, mediaType, &SendDataStruct{ | ||
| Id: data.Id, | ||
| Number: data.Number, | ||
| Quoted: data.Quoted, | ||
| Delay: data.Delay, | ||
| MentionAll: data.MentionAll, | ||
| MentionedJID: data.MentionedJID, | ||
| FormatJid: data.FormatJid, | ||
| MediaHandle: uploaded.Handle, | ||
| Id: data.Id, | ||
| Number: data.Number, | ||
| Quoted: data.Quoted, | ||
| Delay: data.Delay, | ||
| MentionAll: data.MentionAll, | ||
| MentionedJID: data.MentionedJID, | ||
| FormatJid: data.FormatJid, | ||
| MediaHandle: uploaded.Handle, | ||
| ForwardingScore: data.ForwardingScore, | ||
| }) | ||
|
|
||
| if err != nil { | ||
|
|
@@ -1450,14 +1455,15 @@ func (s *sendService) sendMediaUrlWithRetry(data *MediaStruct, instance *instanc | |
|
|
||
| messageStart := time.Now() | ||
| message, err := s.SendMessage(instance, media, mediaType, &SendDataStruct{ | ||
| Id: data.Id, | ||
| Number: data.Number, | ||
| Quoted: data.Quoted, | ||
| Delay: data.Delay, | ||
| MentionAll: data.MentionAll, | ||
| MentionedJID: data.MentionedJID, | ||
| FormatJid: data.FormatJid, | ||
| MediaHandle: uploaded.Handle, | ||
| Id: data.Id, | ||
| Number: data.Number, | ||
| Quoted: data.Quoted, | ||
| Delay: data.Delay, | ||
| MentionAll: data.MentionAll, | ||
| MentionedJID: data.MentionedJID, | ||
| FormatJid: data.FormatJid, | ||
| MediaHandle: uploaded.Handle, | ||
| ForwardingScore: data.ForwardingScore, | ||
| }) | ||
|
|
||
| if err != nil { | ||
|
|
@@ -2192,6 +2198,63 @@ func (s *sendService) SendMessage(instance *instance_model.Instance, msg *waE2E. | |
| } | ||
| } | ||
|
|
||
| // Apply ForwardingScore to whichever ContextInfo was set above. | ||
| // WhatsApp renders "Encaminhada" when ContextInfo.ForwardingScore > 0. | ||
| if data.ForwardingScore != nil && *data.ForwardingScore > 0 { | ||
| switch messageType { | ||
| case "ExtendedTextMessage": | ||
| if msg.ExtendedTextMessage != nil && msg.ExtendedTextMessage.ContextInfo != nil { | ||
| msg.ExtendedTextMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "ImageMessage": | ||
| if msg.ImageMessage != nil && msg.ImageMessage.ContextInfo != nil { | ||
| msg.ImageMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "VideoMessage": | ||
| if msg.VideoMessage != nil && msg.VideoMessage.ContextInfo != nil { | ||
| msg.VideoMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "PtvMessage": | ||
| if msg.PtvMessage != nil && msg.PtvMessage.ContextInfo != nil { | ||
| msg.PtvMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "AudioMessage": | ||
| if msg.AudioMessage != nil && msg.AudioMessage.ContextInfo != nil { | ||
| msg.AudioMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "DocumentMessage": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: The nested Consider extracting |
||
| if msg.DocumentMessage != nil && msg.DocumentMessage.ContextInfo != nil { | ||
| msg.DocumentMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } else if msg.DocumentWithCaptionMessage != nil && msg.DocumentWithCaptionMessage.Message != nil && msg.DocumentWithCaptionMessage.Message.DocumentMessage != nil && msg.DocumentWithCaptionMessage.Message.DocumentMessage.ContextInfo != nil { | ||
| msg.DocumentWithCaptionMessage.Message.DocumentMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "PollCreationMessage": | ||
| if msg.PollCreationMessage != nil && msg.PollCreationMessage.ContextInfo != nil { | ||
| msg.PollCreationMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "StickerMessage": | ||
| if msg.StickerMessage != nil && msg.StickerMessage.ContextInfo != nil { | ||
| msg.StickerMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "LocationMessage": | ||
| if msg.LocationMessage != nil && msg.LocationMessage.ContextInfo != nil { | ||
| msg.LocationMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "ContactMessage": | ||
| if msg.ContactMessage != nil && msg.ContactMessage.ContextInfo != nil { | ||
| msg.ContactMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
| case "InteractiveMessage": | ||
| if msg.InteractiveMessage != nil && msg.InteractiveMessage.ContextInfo != nil { | ||
| msg.InteractiveMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
|
Comment on lines
+2247
to
+2250
|
||
| case "ListMessage": | ||
| if msg.ListMessage != nil && msg.ListMessage.ContextInfo != nil { | ||
| msg.ListMessage.ContextInfo.ForwardingScore = data.ForwardingScore | ||
| } | ||
|
Comment on lines
+2251
to
+2254
|
||
| } | ||
| } | ||
|
|
||
| isGroup := strings.Contains(data.Number, "@g.us") | ||
| isNewsletter := strings.Contains(data.Number, "@newsletter") | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: The large switch for setting
ForwardingScoreis repetitive and may be brittle as new message types are added.Consider extracting a helper like
getContextInfo(msg *waE2E.Message, messageType string) *waE2E.ContextInfothat encapsulates the mapping (including theDocumentWithCaptionMessagespecial case), and then setctx.ForwardingScoreonly if the returned context is non-nil. Centralizing this logic would reduce duplication and make it easier to keep in sync as new message types are added.Suggested implementation:
To fully implement the refactor:
pkg/sendMessage/service/send_service.go(near the other send helpers / below this function):For every
casethat was previously present in the largeswitch messageType { ... }(that setForwardingScoredirectly onmsg.<Type>.ContextInfo), add a correspondingcaseingetContextInfothat returns the sameContextInfopointer instead of setting the score.Ensure the special
DocumentWithCaptionMessagehandling ingetContextInfoexactly mirrors the current behavior from the old switch (including any fallback toDocumentMessage.ContextInfoor other fields).Once this helper contains all the cases, the call site you edited will correctly centralize the mapping and reduce duplication.