diff --git a/src/.env.example b/src/.env.example index 80a226a..fe465d2 100644 --- a/src/.env.example +++ b/src/.env.example @@ -12,5 +12,6 @@ DB_URI="file:storages/whatsapp.db?_foreign_keys=on" WHATSAPP_AUTO_REPLY="Auto reply message" WHATSAPP_WEBHOOK=https://webhook.site/07b69616-5943-4c7f-a8be-db4819df699e,https://webhook.site/09a38aff-d11a-4a38-a176-3f3efa0b5e8b WHATSAPP_WEBHOOK_SECRET=super-secret-key +CALL_WEBHOOK_ON_MESSAGE_TO_SELF=false WHATSAPP_ACCOUNT_VALIDATION=true -WHATSAPP_CHAT_STORAGE=true \ No newline at end of file +WHATSAPP_CHAT_STORAGE=true diff --git a/src/cmd/root.go b/src/cmd/root.go index 57bb5a6..f359e69 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -48,7 +48,7 @@ var ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Short: "Send free whatsapp API", - Long: `This application is from clone https://github.com/aldinokemal/go-whatsapp-web-multidevice, + Long: `This application is from clone https://github.com/aldinokemal/go-whatsapp-web-multidevice, you can send whatsapp over http api but your whatsapp account have to be multi device version`, } @@ -110,6 +110,9 @@ func initEnvConfig() { if viper.IsSet("whatsapp_chat_storage") { config.WhatsappChatStorage = viper.GetBool("whatsapp_chat_storage") } + if envCallWebhookOnMessageToSelf := viper.GetBool("CALL_WEBHOOK_ON_MESSAGE_TO_SELF"); envCallWebhookOnMessageToSelf { + config.WhatsappCallWebhookOnMessageToSelf = envCallWebhookOnMessageToSelf + } } func initFlags() { @@ -185,6 +188,12 @@ func initFlags() { config.WhatsappChatStorage, `enable or disable chat storage --chat-storage . If you disable this, reply feature maybe not working properly | example: --chat-storage=true`, ) + rootCmd.PersistentFlags().BoolVarP( + &config.WhatsappCallWebhookOnMessageToSelf, + "call-webhook-on-message-to-self", "", + config.WhatsappCallWebhookOnMessageToSelf, + `enable or disable webhook calls for messages from yourself --call-webhook-on-message-to-self | example: --call-webhook-on-message-to-self=true`, + ) } func initApp() { diff --git a/src/config/settings.go b/src/config/settings.go index 3da9376..84f6adf 100644 --- a/src/config/settings.go +++ b/src/config/settings.go @@ -24,16 +24,17 @@ var ( DBURI = "file:storages/whatsapp.db?_foreign_keys=on" - WhatsappAutoReplyMessage string - WhatsappWebhook []string - WhatsappWebhookSecret = "secret" - WhatsappLogLevel = "ERROR" - WhatsappSettingMaxImageSize int64 = 20000000 // 20MB - WhatsappSettingMaxFileSize int64 = 50000000 // 50MB - WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB - WhatsappSettingMaxDownloadSize int64 = 500000000 // 500MB - WhatsappTypeUser = "@s.whatsapp.net" - WhatsappTypeGroup = "@g.us" - WhatsappAccountValidation = true - WhatsappChatStorage = true + WhatsappAutoReplyMessage string + WhatsappWebhook []string + WhatsappWebhookSecret = "secret" + WhatsappCallWebhookOnMessageToSelf = false + WhatsappLogLevel = "ERROR" + WhatsappSettingMaxImageSize int64 = 20000000 // 20MB + WhatsappSettingMaxFileSize int64 = 50000000 // 50MB + WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB + WhatsappSettingMaxDownloadSize int64 = 500000000 // 500MB + WhatsappTypeUser = "@s.whatsapp.net" + WhatsappTypeGroup = "@g.us" + WhatsappAccountValidation = true + WhatsappChatStorage = true ) diff --git a/src/infrastructure/whatsapp/init.go b/src/infrastructure/whatsapp/init.go index 1eea4bb..bfb15c5 100644 --- a/src/infrastructure/whatsapp/init.go +++ b/src/infrastructure/whatsapp/init.go @@ -247,18 +247,22 @@ func handleAutoReply(evt *events.Message) { } func handleWebhookForward(ctx context.Context, evt *events.Message) { - // Skip webhook for specific protocol messages that shouldn't trigger webhooks if protocolMessage := evt.Message.GetProtocolMessage(); protocolMessage != nil { protocolType := protocolMessage.GetType().String() - // Skip EPHEMERAL_SYNC_RESPONSE but allow REVOKE and MESSAGE_EDIT if protocolType == "EPHEMERAL_SYNC_RESPONSE" { log.Debugf("Skipping webhook for EPHEMERAL_SYNC_RESPONSE message") return } } - if len(config.WhatsappWebhook) > 0 && - !strings.Contains(evt.Info.SourceString(), "broadcast") { + shouldCallWebhook := len(config.WhatsappWebhook) > 0 && + !strings.Contains(evt.Info.SourceString(), "broadcast") + + if isFromMySelf(evt.Info.SourceString()) && !config.WhatsappCallWebhookOnMessageToSelf { + shouldCallWebhook = false + } + + if shouldCallWebhook { go func(evt *events.Message) { if err := forwardToWebhook(ctx, evt); err != nil { logrus.Error("Failed forward to webhook: ", err)