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.

fix: mitigate WhatsApp 463 send failures

Aldino Kemalec032260dc22
diff --git a/readme.md b/readme.md
index 4bfacbc9..2ba92b75 100644
--- a/readme.md
+++ b/readme.md
@@ -199,6 +199,7 @@ To use environment variables:
| `APP_BASE_PATH` | Base path for subpath deployment | - | `APP_BASE_PATH=/gowa` |
| `APP_TRUSTED_PROXIES` | Trusted proxy IP ranges for reverse proxy | - | `APP_TRUSTED_PROXIES=0.0.0.0/0` |
| `DB_URI` | Database connection URI | `file:storages/whatsapp.db?_foreign_keys=on` | `DB_URI=postgres://user:pass@host/db` |
+| `DB_KEYS_URI` | Optional database URI for encryption/session key cache. Leave blank to use `DB_URI`; avoid in-memory storage in production because restarts can lose WhatsApp session state. | - | `DB_KEYS_URI=file:storages/whatsapp-keys.db?_foreign_keys=on` |
| `WHATSAPP_AUTO_REPLY` | Auto-reply message | - | `WHATSAPP_AUTO_REPLY="Auto reply message"` |
| `WHATSAPP_AUTO_MARK_READ` | Auto-mark incoming messages as read | `false` | `WHATSAPP_AUTO_MARK_READ=true` |
| `WHATSAPP_AUTO_DOWNLOAD_MEDIA` | Auto-download media from incoming messages | `true` | `WHATSAPP_AUTO_DOWNLOAD_MEDIA=false` |
diff --git a/src/.env.example b/src/.env.example
index 935aaa1d..9ba56ac1 100644
--- a/src/.env.example
+++ b/src/.env.example
@@ -9,7 +9,7 @@ APP_TRUSTED_PROXIES=0.0.0.0/0
# Database Settings
DB_URI=file:storages/whatsapp.db?_foreign_keys=on
-DB_KEYS_URI=file::memory:?cache=shared&_foreign_keys=on
+DB_KEYS_URI=
# WhatsApp Settings
WHATSAPP_AUTO_REPLY="Auto reply message"
diff --git a/src/cmd/root.go b/src/cmd/root.go
index a696dfc3..618dd719 100644
--- a/src/cmd/root.go
+++ b/src/cmd/root.go
@@ -233,7 +233,7 @@ func initFlags() {
&config.DBKeysURI,
"db-keys-uri", "",
config.DBKeysURI,
- `the database uri to store the keys database uri (by default, we'll use the same database uri). database uri --db-keys-uri <string> | example: --db-keys-uri="file::memory:?cache=shared&_foreign_keys=on"`,
+ `the database uri to store the optional keys cache (by default, we'll use the same database uri). avoid in-memory storage in production. database uri --db-keys-uri <string> | example: --db-keys-uri="file:storages/whatsapp-keys.db?_foreign_keys=on"`,
)
// WhatsApp flags
diff --git a/src/infrastructure/whatsapp/device_manager.go b/src/infrastructure/whatsapp/device_manager.go
index 0e33fbe4..88a3890b 100644
--- a/src/infrastructure/whatsapp/device_manager.go
+++ b/src/infrastructure/whatsapp/device_manager.go
@@ -591,12 +591,7 @@ func (m *DeviceManager) configureKeysStore(ctx context.Context, device *store.De
innerStore := sqlstore.NewSQLStore(m.keys, *device.ID)
syncKeysDevice(ctx, m.store, m.keys)
- device.Identities = innerStore
- device.Sessions = innerStore
- device.PreKeys = innerStore
- device.SenderKeys = innerStore
- device.MsgSecrets = innerStore
- device.PrivacyTokens = innerStore
+ applyKeyCacheStore(device, innerStore)
return nil
}
diff --git a/src/infrastructure/whatsapp/device_manager_test.go b/src/infrastructure/whatsapp/device_manager_test.go
index c5112eef..980d59f8 100644
--- a/src/infrastructure/whatsapp/device_manager_test.go
+++ b/src/infrastructure/whatsapp/device_manager_test.go
@@ -1,10 +1,41 @@
package whatsapp
import (
+ "errors"
"testing"
"time"
+
+ "go.mau.fi/whatsmeow/store"
)
+func TestApplyKeyCacheStorePreservesPrivacyTokens(t *testing.T) {
+ primaryStore := &store.NoopStore{Error: errors.New("primary")}
+ keyCacheStore := &store.NoopStore{Error: errors.New("keys")}
+ device := &store.Device{}
+ device.SetAllStores(primaryStore)
+
+ applyKeyCacheStore(device, keyCacheStore)
+
+ if device.Identities != keyCacheStore {
+ t.Fatal("expected identities to use key cache store")
+ }
+ if device.Sessions != keyCacheStore {
+ t.Fatal("expected sessions to use key cache store")
+ }
+ if device.PreKeys != keyCacheStore {
+ t.Fatal("expected prekeys to use key cache store")
+ }
+ if device.SenderKeys != keyCacheStore {
+ t.Fatal("expected sender keys to use key cache store")
+ }
+ if device.MsgSecrets != keyCacheStore {
+ t.Fatal("expected message secrets to use key cache store")
+ }
+ if device.PrivacyTokens != primaryStore {
+ t.Fatal("expected privacy tokens to stay on primary store")
+ }
+}
+
func TestListDevices_SortsByCreatedAtAscending(t *testing.T) {
manager := &DeviceManager{
devices: make(map[string]*DeviceInstance),
diff --git a/src/infrastructure/whatsapp/init.go b/src/infrastructure/whatsapp/init.go
index 47cb073c..affe9a17 100644
--- a/src/infrastructure/whatsapp/init.go
+++ b/src/infrastructure/whatsapp/init.go
@@ -88,12 +88,7 @@ func InitWaCLI(ctx context.Context, storeContainer, keysStoreContainer *sqlstore
innerStore := sqlstore.NewSQLStore(keysStoreContainer, *device.ID)
syncKeysDevice(ctx, primaryDB, keysContainer)
- device.Identities = innerStore
- device.Sessions = innerStore
- device.PreKeys = innerStore
- device.SenderKeys = innerStore
- device.MsgSecrets = innerStore
- device.PrivacyTokens = innerStore
+ applyKeyCacheStore(device, innerStore)
}
instanceID := ""
diff --git a/src/infrastructure/whatsapp/key_cache.go b/src/infrastructure/whatsapp/key_cache.go
new file mode 100644
index 00000000..12991ee5
--- /dev/null
+++ b/src/infrastructure/whatsapp/key_cache.go
@@ -0,0 +1,11 @@
+package whatsapp
+
+import "go.mau.fi/whatsmeow/store"
+
+func applyKeyCacheStore(device *store.Device, keyCacheStore store.AllSessionSpecificStores) {
+ device.Identities = keyCacheStore
+ device.Sessions = keyCacheStore
+ device.PreKeys = keyCacheStore
+ device.SenderKeys = keyCacheStore
+ device.MsgSecrets = keyCacheStore
+}
diff --git a/src/pkg/error/whatsapp_error.go b/src/pkg/error/whatsapp_error.go
index 1c71b6b9..7bb2da07 100644
--- a/src/pkg/error/whatsapp_error.go
+++ b/src/pkg/error/whatsapp_error.go
@@ -70,8 +70,26 @@ func (e WaUploadMediaError) StatusCode() int {
return http.StatusInternalServerError
}
+type WaReachoutTimelockError string
+
+// Error for complying the error interface
+func (e WaReachoutTimelockError) Error() string {
+ return string(e)
+}
+
+// ErrCode will return the error code based on the error data type
+func (e WaReachoutTimelockError) ErrCode() string {
+ return "WA_REACHOUT_TIMELOCK"
+}
+
+// StatusCode will return the HTTP status code based on the error data type
+func (e WaReachoutTimelockError) StatusCode() int {
+ return http.StatusTooManyRequests
+}
+
const (
- ErrInvalidJID = InvalidJID("your JID is invalid")
- ErrUserNotRegistered = InvalidJID("user is not registered")
- ErrWaCLI = WaCliError("your WhatsApp CLI is invalid or empty")
+ ErrInvalidJID = InvalidJID("your JID is invalid")
+ ErrUserNotRegistered = InvalidJID("user is not registered")
+ ErrWaCLI = WaCliError("your WhatsApp CLI is invalid or empty")
+ ErrWaReachoutTimelock = WaReachoutTimelockError("WhatsApp rejected this send due to reachout timelock or privacy-token state. Try sending to an existing conversation, have the recipient message you first, and make sure WhatsApp privacy tokens are persisted.")
)
diff --git a/src/usecase/send.go b/src/usecase/send.go
index 24578f70..f415f8dd 100644
--- a/src/usecase/send.go
+++ b/src/usecase/send.go
@@ -54,7 +54,7 @@ func NewSendService(appService app.IAppUsecase, chatStorageRepo domainChatStorag
func (service serviceSend) wrapSendMessage(ctx context.Context, client *whatsmeow.Client, recipient types.JID, msg *waE2E.Message, content string) (whatsmeow.SendResponse, error) {
ts, err := client.SendMessage(ctx, recipient, msg)
if err != nil {
- return whatsmeow.SendResponse{}, err
+ return whatsmeow.SendResponse{}, normalizeSendError(err)
}
// Store the sent message using chatstorage
@@ -81,6 +81,16 @@ func (service serviceSend) wrapSendMessage(ctx context.Context, client *whatsmeo
return ts, nil
}
+func normalizeSendError(err error) error {
+ if err == nil {
+ return nil
+ }
+ if errors.Is(err, whatsmeow.ErrServerReturnedError) && strings.Contains(err.Error(), "463") {
+ return pkgError.ErrWaReachoutTimelock
+ }
+ return err
+}
+
func (service serviceSend) SendText(ctx context.Context, request domainSend.MessageRequest) (response domainSend.GenericResponse, err error) {
err = validations.ValidateSendMessage(ctx, request)
if err != nil {
diff --git a/src/usecase/send_test.go b/src/usecase/send_test.go
index 10e42656..cd94acc3 100644
--- a/src/usecase/send_test.go
+++ b/src/usecase/send_test.go
@@ -2,9 +2,13 @@ package usecase
import (
"context"
+ "errors"
+ "net/http"
"testing"
"github.com/aldinokemal/go-whatsapp-web-multidevice/infrastructure/whatsapp"
+ pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
+ "go.mau.fi/whatsmeow"
)
func TestWithoutCancelPreservesDeviceContext(t *testing.T) {
@@ -61,3 +65,21 @@ func TestResolveDocumentMIME(t *testing.T) {
})
}
}
+
+func TestNormalizeSendErrorMapsReachoutTimelock(t *testing.T) {
+ err := normalizeSendError(errors.Join(whatsmeow.ErrServerReturnedError, errors.New("server returned error 463")))
+
+ genericErr, ok := err.(pkgError.GenericError)
+ if !ok {
+ t.Fatalf("expected generic error, got %T", err)
+ }
+ if got := genericErr.ErrCode(); got != "WA_REACHOUT_TIMELOCK" {
+ t.Fatalf("expected WA_REACHOUT_TIMELOCK code, got %q", got)
+ }
+ if got := genericErr.StatusCode(); got != http.StatusTooManyRequests {
+ t.Fatalf("expected status %d, got %d", http.StatusTooManyRequests, got)
+ }
+ if got := genericErr.Error(); got != "WhatsApp rejected this send due to reachout timelock or privacy-token state. Try sending to an existing conversation, have the recipient message you first, and make sure WhatsApp privacy tokens are persisted." {
+ t.Fatalf("unexpected error message: %q", got)
+ }
+}