Browse Source

Merge b02c56c531 into 2f438c799c

pull/305/merge
TheProcedural 9 months ago
committed by GitHub
parent
commit
6d19d65a71
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      src/.env.example
  2. 9
      src/cmd/root.go
  3. 1
      src/config/settings.go
  4. 12
      src/infrastructure/whatsapp/init.go

1
src/.env.example

@ -12,5 +12,6 @@ DB_URI="file:storages/whatsapp.db?_foreign_keys=on"
WHATSAPP_AUTO_REPLY="Auto reply message" 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=https://webhook.site/07b69616-5943-4c7f-a8be-db4819df699e,https://webhook.site/09a38aff-d11a-4a38-a176-3f3efa0b5e8b
WHATSAPP_WEBHOOK_SECRET=super-secret-key WHATSAPP_WEBHOOK_SECRET=super-secret-key
CALL_WEBHOOK_ON_MESSAGE_TO_SELF=false
WHATSAPP_ACCOUNT_VALIDATION=true WHATSAPP_ACCOUNT_VALIDATION=true
WHATSAPP_CHAT_STORAGE=true WHATSAPP_CHAT_STORAGE=true

9
src/cmd/root.go

@ -110,6 +110,9 @@ func initEnvConfig() {
if viper.IsSet("whatsapp_chat_storage") { if viper.IsSet("whatsapp_chat_storage") {
config.WhatsappChatStorage = viper.GetBool("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() { func initFlags() {
@ -185,6 +188,12 @@ func initFlags() {
config.WhatsappChatStorage, config.WhatsappChatStorage,
`enable or disable chat storage --chat-storage <true/false>. If you disable this, reply feature maybe not working properly | example: --chat-storage=true`, `enable or disable chat storage --chat-storage <true/false>. 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 <true/false> | example: --call-webhook-on-message-to-self=true`,
)
} }
func initApp() { func initApp() {

1
src/config/settings.go

@ -27,6 +27,7 @@ var (
WhatsappAutoReplyMessage string WhatsappAutoReplyMessage string
WhatsappWebhook []string WhatsappWebhook []string
WhatsappWebhookSecret = "secret" WhatsappWebhookSecret = "secret"
WhatsappCallWebhookOnMessageToSelf = false
WhatsappLogLevel = "ERROR" WhatsappLogLevel = "ERROR"
WhatsappSettingMaxImageSize int64 = 20000000 // 20MB WhatsappSettingMaxImageSize int64 = 20000000 // 20MB
WhatsappSettingMaxFileSize int64 = 50000000 // 50MB WhatsappSettingMaxFileSize int64 = 50000000 // 50MB

12
src/infrastructure/whatsapp/init.go

@ -247,18 +247,22 @@ func handleAutoReply(evt *events.Message) {
} }
func handleWebhookForward(ctx context.Context, 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 { if protocolMessage := evt.Message.GetProtocolMessage(); protocolMessage != nil {
protocolType := protocolMessage.GetType().String() protocolType := protocolMessage.GetType().String()
// Skip EPHEMERAL_SYNC_RESPONSE but allow REVOKE and MESSAGE_EDIT
if protocolType == "EPHEMERAL_SYNC_RESPONSE" { if protocolType == "EPHEMERAL_SYNC_RESPONSE" {
log.Debugf("Skipping webhook for EPHEMERAL_SYNC_RESPONSE message") log.Debugf("Skipping webhook for EPHEMERAL_SYNC_RESPONSE message")
return 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) { go func(evt *events.Message) {
if err := forwardToWebhook(ctx, evt); err != nil { if err := forwardToWebhook(ctx, evt); err != nil {
logrus.Error("Failed forward to webhook: ", err) logrus.Error("Failed forward to webhook: ", err)

Loading…
Cancel
Save