Skip to content

feat: add forwardingScore support to send text/media#86

Open
luanwg wants to merge 1 commit into
evolution-foundation:mainfrom
luanwg:feature/forwarding-score
Open

feat: add forwardingScore support to send text/media#86
luanwg wants to merge 1 commit into
evolution-foundation:mainfrom
luanwg:feature/forwarding-score

Conversation

@luanwg

@luanwg luanwg commented Jun 19, 2026

Copy link
Copy Markdown

Summary

WhatsApp renders the Encaminhada badge when ContextInfo.ForwardingScore > 0. Evolution Go v1 did not expose this field, so forwarded messages arrived without the badge in the recipient's WhatsApp.

Changes

  • TextStruct and MediaStruct accept optional forwardingScore (uint32)
  • SendDataStruct carries it through to SendMessage
  • SendMessage applies it to the ContextInfo of every message type (text, image, video, ptv, audio, document, poll, sticker, location, contact, interactive, list)

Usage

POST /send/text
{
  "number": "5511999999999",
  "text": "forwarded message",
  "forwardingScore": 1
}

When forwardingScore > 0, the recipient's WhatsApp shows the Encaminhada badge.

Summary by Sourcery

Add support for propagating WhatsApp message forwarding metadata through send text and media APIs so clients can mark messages as forwarded.

New Features:

  • Allow text send requests to specify an optional forwarding score field.
  • Allow media send requests (file and URL) to specify an optional forwarding score field that is carried through message sending.

Enhancements:

  • Propagate the forwarding score through the internal send pipeline and apply it to the ContextInfo of all supported WhatsApp message types when present.

WhatsApp renders the Encaminhada badge when ContextInfo.ForwardingScore > 0.
Evolution Go v1 did not expose this field, so forwarded messages arrived
without the badge in the recipient's WhatsApp.

Changes:
- TextStruct and MediaStruct accept optional forwardingScore (uint32)
- SendDataStruct carries it through to SendMessage
- SendMessage applies it to the ContextInfo of every message type
  (text, image, video, ptv, audio, document, poll, sticker, location,
  contact, interactive, list)
Copilot AI review requested due to automatic review settings June 19, 2026 23:50
@sourcery-ai

sourcery-ai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds optional forwardingScore support to text and media sending flows, threading it from API payload structs through SendDataStruct into SendMessage, where it is applied to the ContextInfo.ForwardingScore field for all supported WhatsApp message types when greater than zero.

Sequence diagram for applying forwardingScore in SendMessage

sequenceDiagram
    actor Client
    participant sendService
    participant SendMessage
    participant WhatsAppServer

    Client->>sendService: POST /send/text (forwardingScore)
    sendService->>sendService: sendTextWithRetry
    sendService->>SendMessage: SendMessage(instance, msg, ExtendedTextMessage, SendDataStruct)
    alt [ForwardingScore > 0]
        SendMessage->>SendMessage: set ContextInfo.ForwardingScore from SendDataStruct.ForwardingScore
    end
    SendMessage->>WhatsAppServer: send msg with ContextInfo.ForwardingScore
    WhatsAppServer-->>Client: message rendered with Encaminhada badge
Loading

File-Level Changes

Change Details Files
Thread forwardingScore from incoming text/media requests through SendDataStruct into SendMessage and apply it to ContextInfo.ForwardingScore for all supported message types.
  • Extend SendDataStruct, TextStruct, and MediaStruct with an optional ForwardingScore field (pointer to uint32) and expose it on the JSON API for text/media sends.
  • Populate SendDataStruct.ForwardingScore from TextStruct and MediaStruct in sendTextWithRetry, sendMediaFileWithRetry, and sendMediaUrlWithRetry.
  • In SendMessage, when ForwardingScore is non-nil and > 0, set ContextInfo.ForwardingScore on the appropriate message variant (text, image, video, ptv, audio, document, document-with-caption, poll, sticker, location, contact, interactive, and list), guarding all assignments with nil checks.
pkg/sendMessage/service/send_service.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • The SendMessage forwardingScore application introduces a long, repetitive switch over messageType; consider extracting a helper (e.g. applyForwardingScore(msg *waE2E.Message, messageType string, score uint32)) or reusing any existing message-type handling pattern to reduce duplication and lower the risk of missing future message types.
  • In SendMessage, you dereference data.ForwardingScore without a nil-guard on data itself; if SendMessage can ever be called with a nil SendDataStruct (or if that changes in the future), this will panic, so it may be safer to early-return or guard data != nil before accessing it.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `SendMessage` forwardingScore application introduces a long, repetitive switch over `messageType`; consider extracting a helper (e.g. `applyForwardingScore(msg *waE2E.Message, messageType string, score uint32)`) or reusing any existing message-type handling pattern to reduce duplication and lower the risk of missing future message types.
- In `SendMessage`, you dereference `data.ForwardingScore` without a nil-guard on `data` itself; if `SendMessage` can ever be called with a nil `SendDataStruct` (or if that changes in the future), this will panic, so it may be safer to early-return or guard `data != nil` before accessing it.

## Individual Comments

### Comment 1
<location path="pkg/sendMessage/service/send_service.go" line_range="2201-2210" />
<code_context>
+	// Apply ForwardingScore to whichever ContextInfo was set above.
</code_context>
<issue_to_address>
**suggestion:** The large switch for setting `ForwardingScore` is repetitive and may be brittle as new message types are added.

Consider extracting a helper like `getContextInfo(msg *waE2E.Message, messageType string) *waE2E.ContextInfo` that encapsulates the mapping (including the `DocumentWithCaptionMessage` special case), and then set `ctx.ForwardingScore` only 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:

```golang
	// Apply ForwardingScore to whichever ContextInfo was set above.
	// WhatsApp renders "Encaminhada" when ContextInfo.ForwardingScore > 0.
	if data.ForwardingScore != nil && *data.ForwardingScore > 0 {
		if ctxInfo := getContextInfo(msg, messageType); ctxInfo != nil {
			ctxInfo.ForwardingScore = data.ForwardingScore
		}

```

To fully implement the refactor:

1. Define the helper in `pkg/sendMessage/service/send_service.go` (near the other send helpers / below this function):

```go
func getContextInfo(msg *waE2E.Message, messageType string) *waE2E.ContextInfo {
	switch messageType {
	case "ExtendedTextMessage":
		if msg.ExtendedTextMessage != nil {
			return msg.ExtendedTextMessage.ContextInfo
		}
	case "ImageMessage":
		if msg.ImageMessage != nil {
			return msg.ImageMessage.ContextInfo
		}
	case "DocumentWithCaptionMessage":
		// Use the same logic that currently exists in the large switch, e.g.:
		// - If the DocumentWithCaptionMessage has its own ContextInfo, return it
		// - Otherwise, return the underlying DocumentMessage.ContextInfo (or whatever the current special-case logic is)
		// Example skeleton (adjust field names to match the existing code):
		if msg.DocumentWithCaptionMessage != nil {
			if msg.DocumentWithCaptionMessage.ContextInfo != nil {
				return msg.DocumentWithCaptionMessage.ContextInfo
			}
			if msg.DocumentWithCaptionMessage.DocumentMessage != nil {
				return msg.DocumentWithCaptionMessage.DocumentMessage.ContextInfo
			}
		}
	// Add remaining message types currently handled in the big ForwardingScore switch:
	//   - VideoMessage
	//   - AudioMessage
	//   - DocumentMessage
	//   - etc.
	// Each case should mirror the existing logic used to select which ContextInfo gets ForwardingScore.
	}
	return nil
}
```

2. For every `case` that was previously present in the large `switch messageType { ... }` (that set `ForwardingScore` directly on `msg.<Type>.ContextInfo`), add a corresponding `case` in `getContextInfo` that returns the same `ContextInfo` pointer instead of setting the score.

3. Ensure the special `DocumentWithCaptionMessage` handling in `getContextInfo` exactly mirrors the current behavior from the old switch (including any fallback to `DocumentMessage.ContextInfo` or other fields).

Once this helper contains all the cases, the call site you edited will correctly centralize the mapping and reduce duplication.
</issue_to_address>

### Comment 2
<location path="pkg/sendMessage/service/send_service.go" line_range="2225" />
<code_context>
+			if msg.AudioMessage != nil && msg.AudioMessage.ContextInfo != nil {
+				msg.AudioMessage.ContextInfo.ForwardingScore = data.ForwardingScore
+			}
+		case "DocumentMessage":
+			if msg.DocumentMessage != nil && msg.DocumentMessage.ContextInfo != nil {
+				msg.DocumentMessage.ContextInfo.ForwardingScore = data.ForwardingScore
</code_context>
<issue_to_address>
**nitpick:** The nested `DocumentWithCaptionMessage` condition is quite dense and could be made more readable.

Consider extracting `msg.DocumentWithCaptionMessage.Message.DocumentMessage` into a local variable (and/or splitting the checks into separate `if` blocks or early returns) to simplify the condition and make future changes less error‑prone.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +2201 to +2210
// 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 {

Copy link
Copy Markdown

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 ForwardingScore is repetitive and may be brittle as new message types are added.

Consider extracting a helper like getContextInfo(msg *waE2E.Message, messageType string) *waE2E.ContextInfo that encapsulates the mapping (including the DocumentWithCaptionMessage special case), and then set ctx.ForwardingScore only 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:

	// Apply ForwardingScore to whichever ContextInfo was set above.
	// WhatsApp renders "Encaminhada" when ContextInfo.ForwardingScore > 0.
	if data.ForwardingScore != nil && *data.ForwardingScore > 0 {
		if ctxInfo := getContextInfo(msg, messageType); ctxInfo != nil {
			ctxInfo.ForwardingScore = data.ForwardingScore
		}

To fully implement the refactor:

  1. Define the helper in pkg/sendMessage/service/send_service.go (near the other send helpers / below this function):
func getContextInfo(msg *waE2E.Message, messageType string) *waE2E.ContextInfo {
	switch messageType {
	case "ExtendedTextMessage":
		if msg.ExtendedTextMessage != nil {
			return msg.ExtendedTextMessage.ContextInfo
		}
	case "ImageMessage":
		if msg.ImageMessage != nil {
			return msg.ImageMessage.ContextInfo
		}
	case "DocumentWithCaptionMessage":
		// Use the same logic that currently exists in the large switch, e.g.:
		// - If the DocumentWithCaptionMessage has its own ContextInfo, return it
		// - Otherwise, return the underlying DocumentMessage.ContextInfo (or whatever the current special-case logic is)
		// Example skeleton (adjust field names to match the existing code):
		if msg.DocumentWithCaptionMessage != nil {
			if msg.DocumentWithCaptionMessage.ContextInfo != nil {
				return msg.DocumentWithCaptionMessage.ContextInfo
			}
			if msg.DocumentWithCaptionMessage.DocumentMessage != nil {
				return msg.DocumentWithCaptionMessage.DocumentMessage.ContextInfo
			}
		}
	// Add remaining message types currently handled in the big ForwardingScore switch:
	//   - VideoMessage
	//   - AudioMessage
	//   - DocumentMessage
	//   - etc.
	// Each case should mirror the existing logic used to select which ContextInfo gets ForwardingScore.
	}
	return nil
}
  1. For every case that was previously present in the large switch messageType { ... } (that set ForwardingScore directly on msg.<Type>.ContextInfo), add a corresponding case in getContextInfo that returns the same ContextInfo pointer instead of setting the score.

  2. Ensure the special DocumentWithCaptionMessage handling in getContextInfo exactly mirrors the current behavior from the old switch (including any fallback to DocumentMessage.ContextInfo or other fields).

Once this helper contains all the cases, the call site you edited will correctly centralize the mapping and reduce duplication.

if msg.AudioMessage != nil && msg.AudioMessage.ContextInfo != nil {
msg.AudioMessage.ContextInfo.ForwardingScore = data.ForwardingScore
}
case "DocumentMessage":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: The nested DocumentWithCaptionMessage condition is quite dense and could be made more readable.

Consider extracting msg.DocumentWithCaptionMessage.Message.DocumentMessage into a local variable (and/or splitting the checks into separate if blocks or early returns) to simplify the condition and make future changes less error‑prone.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for propagating WhatsApp forwarding metadata (ContextInfo.ForwardingScore) through the send pipeline so recipients can see the forwarded (“Encaminhada”) badge when applicable.

Changes:

  • Added optional forwardingScore to TextStruct and MediaStruct request payloads.
  • Plumbed ForwardingScore through SendDataStruct into SendMessage.
  • Set ContextInfo.ForwardingScore inside SendMessage across multiple message types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2247 to +2250
case "InteractiveMessage":
if msg.InteractiveMessage != nil && msg.InteractiveMessage.ContextInfo != nil {
msg.InteractiveMessage.ContextInfo.ForwardingScore = data.ForwardingScore
}
Comment on lines +2251 to +2254
case "ListMessage":
if msg.ListMessage != nil && msg.ListMessage.ContextInfo != nil {
msg.ListMessage.ContextInfo.ForwardingScore = data.ForwardingScore
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants