Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

feat(webhook): add option to forward receipts from all linked devices (#753)

Yamatab756a430c705
diff --git a/docs/webhook-payload.md b/docs/webhook-payload.md
index 7bed1772..24368fc5 100644
--- a/docs/webhook-payload.md
+++ b/docs/webhook-payload.md
@@ -447,12 +447,19 @@ Triggered when a message is read by the recipient (they opened the chat and saw
}
```
-Receipts are forwarded from each user's primary device only, so a contact with several linked devices does not produce
+By default, contact receipts are forwarded from each user's primary device only, so a contact with several linked devices does not produce
duplicate events. Read receipts generated by the current account itself (`"read"`, or `"read-self"` when read receipts
are disabled in privacy settings) are forwarded from any of its devices, so opening a chat on WhatsApp Web, Desktop or
another linked device is reported just like opening it on the phone. For these, `payload.from` is the account's own JID
(without the device suffix) and `payload.ids` are the IDs of the incoming messages that were read.
+Set `WHATSAPP_WEBHOOK_ALL_DEVICE_RECEIPTS=true` or `--webhook-all-device-receipts=true` to also forward
+contacts' linked-device receipts, including when their primary phone is offline. The flag overrides the environment
+setting, including an explicit `--webhook-all-device-receipts=false`. This opt-in can produce duplicate or out-of-order
+acknowledgements for the same message. Consumers should apply updates idempotently per account, chat and message ID,
+and never downgrade a message from read to delivered when an older acknowledgement arrives. The payload continues
+to use canonical message IDs and sender JIDs without device suffixes.
+
### Receipt Event Fields
| **Field** | **Type** | **Description** |
diff --git a/readme.md b/readme.md
index 8a523e90..14882298 100644
--- a/readme.md
+++ b/readme.md
@@ -279,6 +279,7 @@ To use environment variables:
| `WHATSAPP_WEBHOOK_INSECURE_SKIP_VERIFY` | Skip TLS verification for webhooks (insecure) | `false` | `WHATSAPP_WEBHOOK_INSECURE_SKIP_VERIFY=true` |
| `WHATSAPP_WEBHOOK_EVENTS` | Whitelist of events to forward (comma-separated, empty = all) | - | `WHATSAPP_WEBHOOK_EVENTS=message,message.ack` |
| `WHATSAPP_WEBHOOK_IGNORE_JIDS` | JIDs/wildcards to skip when forwarding (comma-separated) | - | `[email protected]` |
+| `WHATSAPP_WEBHOOK_ALL_DEVICE_RECEIPTS` | Forward all linked-device receipts; consumers must handle duplicates and out-of-order updates | `false` | `WHATSAPP_WEBHOOK_ALL_DEVICE_RECEIPTS=true` |
| `WHATSAPP_WEBHOOK_DEVICE_MERGE_GLOBAL` | Per-device webhook adds to the global URLs instead of replacing them | `false` | `WHATSAPP_WEBHOOK_DEVICE_MERGE_GLOBAL=true` |
| `WHATSAPP_ACCOUNT_VALIDATION` | Enable account validation | `true` | `WHATSAPP_ACCOUNT_VALIDATION=false` |
| `WHATSAPP_PRESENCE_ON_CONNECT` | Presence on connect: `available`, `unavailable`, or `none` | `unavailable` | `WHATSAPP_PRESENCE_ON_CONNECT=unavailable` |
diff --git a/src/cmd/receipt_config_test.go b/src/cmd/receipt_config_test.go
new file mode 100644
index 00000000..1fc015bd
--- /dev/null
+++ b/src/cmd/receipt_config_test.go
@@ -0,0 +1,55 @@
+package cmd
+
+import (
+ "os"
+ "os/exec"
+ "strconv"
+ "testing"
+
+ "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
+ "github.com/stretchr/testify/require"
+)
+
+func TestReceiptConfigPrecedence(t *testing.T) {
+ // Each process isolates Viper and the other globals changed by initEnvConfig.
+ if os.Getenv("GOWA_RECEIPT_CONFIG_HELPER") == "1" {
+ original := config.WhatsappWebhookAllDeviceReceipts
+ flag := rootCmd.PersistentFlags().Lookup("webhook-all-device-receipts")
+ originalChanged := flag.Changed
+ defer func() {
+ config.WhatsappWebhookAllDeviceReceipts = original
+ flag.Changed = originalChanged
+ }()
+ if value := os.Getenv("GOWA_RECEIPT_TEST_FLAG"); value != "" {
+ require.NoError(t, rootCmd.ParseFlags([]string{"--webhook-all-device-receipts=" + value}))
+ }
+ initEnvConfig()
+ want, err := strconv.ParseBool(os.Getenv("GOWA_RECEIPT_TEST_WANT"))
+ require.NoError(t, err)
+ require.Equal(t, want, config.WhatsappWebhookAllDeviceReceipts)
+ return
+ }
+
+ for _, tc := range []struct {
+ name, env, flag string
+ want bool
+ }{
+ {"default disabled", "", "", false},
+ {"environment enabled", "true", "", true},
+ {"environment disabled", "false", "", false},
+ {"explicit false overrides environment true", "true", "false", false},
+ {"explicit true overrides environment false", "false", "true", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ cmd := exec.Command(os.Args[0], "-test.run=^TestReceiptConfigPrecedence$")
+ cmd.Dir = t.TempDir()
+ cmd.Env = append(os.Environ(),
+ "GOWA_RECEIPT_CONFIG_HELPER=1",
+ "WHATSAPP_WEBHOOK_ALL_DEVICE_RECEIPTS="+tc.env,
+ "GOWA_RECEIPT_TEST_FLAG="+tc.flag,
+ "GOWA_RECEIPT_TEST_WANT="+strconv.FormatBool(tc.want))
+ output, err := cmd.CombinedOutput()
+ require.NoError(t, err, "%s", output)
+ })
+ }
+}
diff --git a/src/cmd/root.go b/src/cmd/root.go
index 09ef3529..8833e685 100644
--- a/src/cmd/root.go
+++ b/src/cmd/root.go
@@ -197,6 +197,9 @@ func initEnvConfig() {
}
config.WhatsappWebhookIgnoreJids = jids
}
+ if !rootCmd.PersistentFlags().Changed("webhook-all-device-receipts") && viper.IsSet("whatsapp_webhook_all_device_receipts") {
+ config.WhatsappWebhookAllDeviceReceipts = viper.GetBool("whatsapp_webhook_all_device_receipts")
+ }
if viper.IsSet("whatsapp_webhook_device_merge_global") {
config.WhatsappWebhookDeviceMergeGlobal = viper.GetBool("whatsapp_webhook_device_merge_global")
}
@@ -483,6 +486,12 @@ func initFlags() {
config.WhatsappWebhookIgnoreJids,
`comma-separated WhatsApp JIDs (or "@g.us"/"@s.whatsapp.net"/"@lid" wildcards) to skip when forwarding to webhooks --webhook-ignore-jids <list> | example: --webhook-ignore-jids="@g.us,[email protected]"`,
)
+ rootCmd.PersistentFlags().BoolVarP(
+ &config.WhatsappWebhookAllDeviceReceipts,
+ "webhook-all-device-receipts", "",
+ config.WhatsappWebhookAllDeviceReceipts,
+ `forward receipts from all linked devices instead of only the primary device --webhook-all-device-receipts <true/false> | example: --webhook-all-device-receipts=true`,
+ )
rootCmd.PersistentFlags().BoolVarP(
&config.WhatsappWebhookDeviceMergeGlobal,
"webhook-device-merge-global", "",
diff --git a/src/config/settings.go b/src/config/settings.go
index 377eb0da..284008d9 100644
--- a/src/config/settings.go
+++ b/src/config/settings.go
@@ -64,6 +64,7 @@ var (
WhatsappWebhookInsecureSkipVerify = false // Skip TLS certificate verification for webhooks (insecure)
WhatsappWebhookEvents []string // Whitelist of events to forward to webhook (empty = all events)
WhatsappWebhookIgnoreJids []string // JIDs (or "@g.us"/"@s.whatsapp.net"/"@lid" wildcards) to skip when forwarding to webhooks
+ WhatsappWebhookAllDeviceReceipts = false // Forward receipts from all linked devices, not only the primary device
WhatsappWebhookDeviceMergeGlobal = false // Per-device webhook adds to the global WHATSAPP_WEBHOOK targets instead of replacing them
WhatsappAutoRejectCall = false // Auto-reject incoming calls
WhatsappLogLevel = "ERROR"
diff --git a/src/infrastructure/whatsapp/event_receipt.go b/src/infrastructure/whatsapp/event_receipt.go
index 223f673c..f4e60b20 100644
--- a/src/infrastructure/whatsapp/event_receipt.go
+++ b/src/infrastructure/whatsapp/event_receipt.go
@@ -4,6 +4,7 @@ import (
"context"
"time"
+ "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/sirupsen/logrus"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/types"
@@ -99,20 +100,11 @@ func forwardReceiptToWebhook(ctx context.Context, evt *events.Receipt, deviceID
return forwardPayloadToConfiguredWebhooks(ctx, payload, "message.ack")
}
-// shouldForwardReceipt reports whether a receipt event should be forwarded.
-//
-// WhatsApp sends separate receipt events for each linked device (phone, web,
-// desktop, etc.) of a contact. For example, if a contact has 3 devices, you
-// would receive 3 "delivered" receipts for the same message. To avoid duplicate
-// webhooks we only forward the receipt from the primary device (Device == 0).
-//
-// The exception is a read receipt sent by one of the current account's own
-// devices (IsFromMe). It means the account owner read the chat on that device
-// (WhatsApp Web, Desktop or another companion), and only the device where the
-// chat was opened sends it, so it is not duplicated per device. Dropping it
-// would hide reads that did not happen on the phone.
+// shouldForwardReceipt suppresses duplicate contact receipts from linked devices
+// unless explicitly enabled. Own-device reads always pass because only the device
+// where the account owner opened the chat emits them.
func shouldForwardReceipt(evt *events.Receipt) bool {
- if evt.Sender.Device == 0 {
+ if config.WhatsappWebhookAllDeviceReceipts || evt.Sender.Device == 0 {
return true
}
return evt.IsFromMe && (evt.Type == types.ReceiptTypeRead || evt.Type == types.ReceiptTypeReadSelf)
diff --git a/src/infrastructure/whatsapp/event_receipt_test.go b/src/infrastructure/whatsapp/event_receipt_test.go
index 5ec0e367..13507cc2 100644
--- a/src/infrastructure/whatsapp/event_receipt_test.go
+++ b/src/infrastructure/whatsapp/event_receipt_test.go
@@ -5,6 +5,7 @@ import (
"testing"
"time"
+ "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/stretchr/testify/assert"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/store"
@@ -13,6 +14,9 @@ import (
)
func TestShouldForwardReceipt(t *testing.T) {
+ original := config.WhatsappWebhookAllDeviceReceipts
+ defer func() { config.WhatsappWebhookAllDeviceReceipts = original }()
+ config.WhatsappWebhookAllDeviceReceipts = false
contact := types.NewJID("628123456789", types.DefaultUserServer)
own := types.NewJID("628111111111", types.DefaultUserServer)
ownLID := types.NewJID("251556368777322", types.HiddenUserServer)
@@ -85,3 +89,39 @@ func TestCreateReceiptPayloadOwnLinkedDeviceReadSelf(t *testing.T) {
assert.Equal(t, own.String(), payload["from"], "device suffix must be stripped from the sender")
assert.Equal(t, "read-self", payload["receipt_type"])
}
+
+func receiptFromDevice(device uint16) *events.Receipt {
+ return &events.Receipt{
+ MessageSource: types.MessageSource{
+ Sender: types.JID{User: "6289685028129", Device: device, Server: types.DefaultUserServer},
+ },
+ }
+}
+
+func TestShouldForwardReceipt_PrimaryDeviceOnlyByDefault(t *testing.T) {
+ original := config.WhatsappWebhookAllDeviceReceipts
+ defer func() { config.WhatsappWebhookAllDeviceReceipts = original }()
+
+ config.WhatsappWebhookAllDeviceReceipts = false
+
+ if !shouldForwardReceipt(receiptFromDevice(0)) {
+ t.Error("receipt from the primary device (Device == 0) should be forwarded by default")
+ }
+ if shouldForwardReceipt(receiptFromDevice(2)) {
+ t.Error("receipt from a linked device should be skipped by default")
+ }
+}
+
+func TestShouldForwardReceipt_AllDevicesWhenEnabled(t *testing.T) {
+ original := config.WhatsappWebhookAllDeviceReceipts
+ defer func() { config.WhatsappWebhookAllDeviceReceipts = original }()
+
+ config.WhatsappWebhookAllDeviceReceipts = true
+
+ if !shouldForwardReceipt(receiptFromDevice(0)) {
+ t.Error("receipt from the primary device should be forwarded when all-device forwarding is enabled")
+ }
+ if !shouldForwardReceipt(receiptFromDevice(5)) {
+ t.Error("receipt from a linked device should be forwarded when all-device forwarding is enabled")
+ }
+}