Browse Source

feat: update .gitignore and enhance environment variable handling

- Added 'bin' to .gitignore to exclude binary files from version control.
- Improved environment variable handling in root.go by normalizing variable names to lowercase.
- Added protocol message handling in webhook.go to manage message edits and revocations.
- Enhanced webhook forwarding logic to skip specific protocol messages.
main
Aldino Kemal 9 months ago
parent
commit
31c57f459d
  1. 1
      .gitignore
  2. 28
      src/cmd/root.go
  3. 18
      src/infrastructure/whatsapp/init.go
  4. 29
      src/infrastructure/whatsapp/webhook.go

1
.gitignore

@ -9,3 +9,4 @@ main.exe
src/pkged.go src/pkged.go
storages storages
.env .env
bin

28
src/cmd/root.go

@ -3,6 +3,7 @@ package cmd
import ( import (
"context" "context"
"embed" "embed"
"fmt"
"os" "os"
"strings" "strings"
"time" "time"
@ -68,45 +69,46 @@ func init() {
// initEnvConfig loads configuration from environment variables // initEnvConfig loads configuration from environment variables
func initEnvConfig() { func initEnvConfig() {
fmt.Println(viper.AllSettings())
// Application settings // Application settings
if envPort := viper.GetString("APP_PORT"); envPort != "" {
if envPort := viper.GetString("app_port"); envPort != "" {
config.AppPort = envPort config.AppPort = envPort
} }
if envDebug := viper.GetBool("APP_DEBUG"); envDebug {
if envDebug := viper.GetBool("app_debug"); envDebug {
config.AppDebug = envDebug config.AppDebug = envDebug
} }
if envOs := viper.GetString("APP_OS"); envOs != "" {
if envOs := viper.GetString("app_os"); envOs != "" {
config.AppOs = envOs config.AppOs = envOs
} }
if envBasicAuth := viper.GetString("APP_BASIC_AUTH"); envBasicAuth != "" {
if envBasicAuth := viper.GetString("app_basic_auth"); envBasicAuth != "" {
credential := strings.Split(envBasicAuth, ",") credential := strings.Split(envBasicAuth, ",")
config.AppBasicAuthCredential = credential config.AppBasicAuthCredential = credential
} }
if envChatFlushInterval := viper.GetInt("APP_CHAT_FLUSH_INTERVAL"); envChatFlushInterval > 0 {
if envChatFlushInterval := viper.GetInt("app_chat_flush_interval"); envChatFlushInterval > 0 {
config.AppChatFlushIntervalDays = envChatFlushInterval config.AppChatFlushIntervalDays = envChatFlushInterval
} }
// Database settings // Database settings
if envDBURI := viper.GetString("DB_URI"); envDBURI != "" {
if envDBURI := viper.GetString("db_uri"); envDBURI != "" {
config.DBURI = envDBURI config.DBURI = envDBURI
} }
// WhatsApp settings // WhatsApp settings
if envAutoReply := viper.GetString("WHATSAPP_AUTO_REPLY"); envAutoReply != "" {
if envAutoReply := viper.GetString("whatsapp_auto_reply"); envAutoReply != "" {
config.WhatsappAutoReplyMessage = envAutoReply config.WhatsappAutoReplyMessage = envAutoReply
} }
if envWebhook := viper.GetString("WHATSAPP_WEBHOOK"); envWebhook != "" {
if envWebhook := viper.GetString("whatsapp_webhook"); envWebhook != "" {
webhook := strings.Split(envWebhook, ",") webhook := strings.Split(envWebhook, ",")
config.WhatsappWebhook = webhook config.WhatsappWebhook = webhook
} }
if envWebhookSecret := viper.GetString("WHATSAPP_WEBHOOK_SECRET"); envWebhookSecret != "" {
if envWebhookSecret := viper.GetString("whatsapp_webhook_secret"); envWebhookSecret != "" {
config.WhatsappWebhookSecret = envWebhookSecret config.WhatsappWebhookSecret = envWebhookSecret
} }
if envAccountValidation := viper.GetBool("WHATSAPP_ACCOUNT_VALIDATION"); envAccountValidation {
config.WhatsappAccountValidation = envAccountValidation
if viper.IsSet("whatsapp_account_validation") {
config.WhatsappAccountValidation = viper.GetBool("whatsapp_account_validation")
} }
if envChatStorage := viper.GetBool("WHATSAPP_CHAT_STORAGE"); !envChatStorage {
config.WhatsappChatStorage = envChatStorage
if viper.IsSet("whatsapp_chat_storage") {
config.WhatsappChatStorage = viper.GetBool("whatsapp_chat_storage")
} }
} }

18
src/infrastructure/whatsapp/init.go

@ -247,9 +247,18 @@ 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 {
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 && if len(config.WhatsappWebhook) > 0 &&
!strings.Contains(evt.Info.SourceString(), "broadcast") &&
!isFromMySelf(evt.Info.SourceString()) {
!strings.Contains(evt.Info.SourceString(), "broadcast") {
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)
@ -259,9 +268,10 @@ func handleWebhookForward(ctx context.Context, evt *events.Message) {
} }
func handleReceipt(_ context.Context, evt *events.Receipt) { func handleReceipt(_ context.Context, evt *events.Receipt) {
if evt.Type == types.ReceiptTypeRead || evt.Type == types.ReceiptTypeReadSelf {
switch evt.Type {
case types.ReceiptTypeRead, types.ReceiptTypeReadSelf:
log.Infof("%v was read by %s at %s", evt.MessageIDs, evt.SourceString(), evt.Timestamp) log.Infof("%v was read by %s at %s", evt.MessageIDs, evt.SourceString(), evt.Timestamp)
} else if evt.Type == types.ReceiptTypeDelivered {
case types.ReceiptTypeDelivered:
log.Infof("%s was delivered to %s at %s", evt.MessageIDs[0], evt.SourceString(), evt.Timestamp) log.Infof("%s was delivered to %s at %s", evt.MessageIDs[0], evt.SourceString(), evt.Timestamp)
} }
} }

29
src/infrastructure/whatsapp/webhook.go

@ -5,12 +5,13 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"go.mau.fi/whatsmeow/types"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
"time" "time"
"go.mau.fi/whatsmeow/types"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config" "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error" pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -109,6 +110,32 @@ func createPayload(ctx context.Context, evt *events.Message) (map[string]interfa
body["timestamp"] = timestamp body["timestamp"] = timestamp
} }
// Handle protocol messages (revoke, etc.)
if protocolMessage := evt.Message.GetProtocolMessage(); protocolMessage != nil {
protocolType := protocolMessage.GetType().String()
switch protocolType {
case "REVOKE":
body["action"] = "message_revoked"
if key := protocolMessage.GetKey(); key != nil {
body["revoked_message_id"] = key.GetID()
body["revoked_from_me"] = key.GetFromMe()
if key.GetRemoteJID() != "" {
body["revoked_chat"] = key.GetRemoteJID()
}
}
case "MESSAGE_EDIT":
body["action"] = "message_edited"
if editedMessage := protocolMessage.GetEditedMessage(); editedMessage != nil {
if editedText := editedMessage.GetExtendedTextMessage(); editedText != nil {
body["edited_text"] = editedText.GetText()
} else if editedConv := editedMessage.GetConversation(); editedConv != "" {
body["edited_text"] = editedConv
}
}
}
}
if audioMedia := evt.Message.GetAudioMessage(); audioMedia != nil { if audioMedia := evt.Message.GetAudioMessage(); audioMedia != nil {
path, err := ExtractMedia(ctx, config.PathMedia, audioMedia) path, err := ExtractMedia(ctx, config.PathMedia, audioMedia)
if err != nil { if err != nil {

Loading…
Cancel
Save