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: Add WhatsApp presence configuration on connect

Aldino Kemal61c29b00ab50
diff --git a/src/.env.example b/src/.env.example
index 3062da04..e744f089 100644
--- a/src/.env.example
+++ b/src/.env.example
@@ -22,6 +22,7 @@ WHATSAPP_WEBHOOK_INSECURE_SKIP_VERIFY=false
WHATSAPP_WEBHOOK_EVENTS=message,message.reaction,message.revoked,message.edited,message.ack,message.deleted,group.participants
WHATSAPP_WEBHOOK_INCLUDE_OUTGOING=false
WHATSAPP_ACCOUNT_VALIDATION=true
+WHATSAPP_PRESENCE_ON_CONNECT=unavailable
WHATSAPP_CHAT_STORAGE=true
# Chatwoot Integration
diff --git a/src/cmd/root.go b/src/cmd/root.go
index 31264631..d5d0a20a 100644
--- a/src/cmd/root.go
+++ b/src/cmd/root.go
@@ -143,6 +143,9 @@ func initEnvConfig() {
if viper.IsSet("whatsapp_auto_reject_call") {
config.WhatsappAutoRejectCall = viper.GetBool("whatsapp_auto_reject_call")
}
+ if envPresenceOnConnect := viper.GetString("whatsapp_presence_on_connect"); envPresenceOnConnect != "" {
+ config.WhatsappPresenceOnConnect = envPresenceOnConnect
+ }
// Chatwoot settings
if viper.IsSet("chatwoot_enabled") {
@@ -288,6 +291,12 @@ func initFlags() {
config.WhatsappAutoRejectCall,
`auto reject incoming calls --auto-reject-call <true/false> | example: --auto-reject-call=true`,
)
+ rootCmd.PersistentFlags().StringVarP(
+ &config.WhatsappPresenceOnConnect,
+ "presence-on-connect", "",
+ config.WhatsappPresenceOnConnect,
+ `presence to send on connect: "available", "unavailable", or "none" --presence-on-connect <string> | example: --presence-on-connect="unavailable"`,
+ )
// Chatwoot flags
rootCmd.PersistentFlags().BoolVarP(
diff --git a/src/config/settings.go b/src/config/settings.go
index 257edfae..5156e263 100644
--- a/src/config/settings.go
+++ b/src/config/settings.go
@@ -43,6 +43,7 @@ var (
WhatsappTypeGroup = "@g.us"
WhatsappTypeLid = "@lid"
WhatsappAccountValidation = true
+ WhatsappPresenceOnConnect = "unavailable" // Presence to send on connect: "available", "unavailable", or "none"
ChatStorageURI = "file:storages/chatstorage.db"
ChatStorageEnableForeignKeys = true
diff --git a/src/infrastructure/whatsapp/event_handler.go b/src/infrastructure/whatsapp/event_handler.go
index 70d99567..bfe6efd2 100644
--- a/src/infrastructure/whatsapp/event_handler.go
+++ b/src/infrastructure/whatsapp/event_handler.go
@@ -106,16 +106,36 @@ func handleDeleteForMe(ctx context.Context, evt *events.DeleteForMe, chatStorage
}
}
+func resolvePresenceOnConnect() (types.Presence, bool) {
+ switch config.WhatsappPresenceOnConnect {
+ case "available":
+ return types.PresenceAvailable, false
+ case "none":
+ return "", true
+ default:
+ return types.PresenceUnavailable, false
+ }
+}
+
+func sendConfiguredPresence(ctx context.Context, client *whatsmeow.Client) {
+ presence, skip := resolvePresenceOnConnect()
+ if skip {
+ log.Infof("Skipping presence on connect (configured: none)")
+ return
+ }
+ if err := client.SendPresence(ctx, presence); err != nil {
+ log.Warnf("Failed to send %s presence: %v", presence, err)
+ } else {
+ log.Infof("Marked self as %s", presence)
+ }
+}
+
func handleAppStateSyncComplete(_ context.Context, client *whatsmeow.Client, evt *events.AppStateSyncComplete) {
if client == nil {
return
}
if len(client.Store.PushName) > 0 && evt.Name == appstate.WAPatchCriticalBlock {
- if err := client.SendPresence(context.Background(), types.PresenceAvailable); err != nil {
- log.Warnf("Failed to send available presence: %v", err)
- } else {
- log.Infof("Marked self as available")
- }
+ sendConfiguredPresence(context.Background(), client)
}
}
@@ -181,13 +201,9 @@ func handleConnectionEvents(_ context.Context, client *whatsmeow.Client, instanc
return
}
- // Send presence available when connecting and when the pushname is changed.
+ // Send configured presence when connecting and when the pushname is changed.
// This makes sure that outgoing messages always have the right pushname.
- if err := client.SendPresence(context.Background(), types.PresenceAvailable); err != nil {
- log.Warnf("Failed to send available presence: %v", err)
- } else {
- log.Infof("Marked self as available")
- }
+ sendConfiguredPresence(context.Background(), client)
}
func handleStreamReplaced(_ context.Context) {
diff --git a/src/infrastructure/whatsapp/event_handler_test.go b/src/infrastructure/whatsapp/event_handler_test.go
new file mode 100644
index 00000000..845bd887
--- /dev/null
+++ b/src/infrastructure/whatsapp/event_handler_test.go
@@ -0,0 +1,64 @@
+package whatsapp
+
+import (
+ "testing"
+
+ "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
+ "go.mau.fi/whatsmeow/types"
+)
+
+func TestResolvePresenceOnConnect(t *testing.T) {
+ tests := []struct {
+ name string
+ configVal string
+ wantPres types.Presence
+ wantSkip bool
+ }{
+ {
+ name: "unavailable returns PresenceUnavailable",
+ configVal: "unavailable",
+ wantPres: types.PresenceUnavailable,
+ wantSkip: false,
+ },
+ {
+ name: "available returns PresenceAvailable",
+ configVal: "available",
+ wantPres: types.PresenceAvailable,
+ wantSkip: false,
+ },
+ {
+ name: "none skips sending presence",
+ configVal: "none",
+ wantPres: "",
+ wantSkip: true,
+ },
+ {
+ name: "empty string defaults to unavailable",
+ configVal: "",
+ wantPres: types.PresenceUnavailable,
+ wantSkip: false,
+ },
+ {
+ name: "unknown value defaults to unavailable",
+ configVal: "garbage",
+ wantPres: types.PresenceUnavailable,
+ wantSkip: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ origVal := config.WhatsappPresenceOnConnect
+ config.WhatsappPresenceOnConnect = tt.configVal
+ defer func() { config.WhatsappPresenceOnConnect = origVal }()
+
+ pres, skip := resolvePresenceOnConnect()
+ if skip != tt.wantSkip {
+ t.Errorf("resolvePresenceOnConnect() skip = %v, want %v", skip, tt.wantSkip)
+ }
+ if !skip && pres != tt.wantPres {
+ t.Errorf("resolvePresenceOnConnect() presence = %q, want %q", pres, tt.wantPres)
+ }
+ })
+ }
+}