From 6258f1537ba10bc24af1236dab93d4d2285a41d0 Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Tue, 1 Apr 2025 17:27:08 +0530 Subject: [PATCH] Add allowlist --- internal/client.go | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/internal/client.go b/internal/client.go index 7d46f0f..ff35bcb 100644 --- a/internal/client.go +++ b/internal/client.go @@ -6,6 +6,7 @@ import ( "fmt" "math/rand/v2" "os" + "slices" "strconv" "time" @@ -19,23 +20,22 @@ import ( ) const autoResponseTimeMapJsonFileName = "auto-response-time-map.json" +const allowlistFileName = "allowlist.json" type Client struct { WAClient *whatsmeow.Client message string autoResponseTimeMap map[string]string + allowlist []string } func NewClient(waClient *whatsmeow.Client) *Client { autoResponseTimeMap := map[string]string{} + allowlist := []string{} fileInfo, _ := os.Stat(autoResponseTimeMapJsonFileName) if fileInfo != nil { - if fileInfo.IsDir() { - panic(fmt.Errorf("expected %s to be a file, but found dir", autoResponseTimeMapJsonFileName)) - } - bytes, err := os.ReadFile(autoResponseTimeMapJsonFileName) if err != nil { @@ -49,10 +49,27 @@ func NewClient(waClient *whatsmeow.Client) *Client { } } + fileInfo, _ = os.Stat(allowlistFileName) + + if fileInfo != nil { + bytes, err := os.ReadFile(allowlistFileName) + + if err != nil { + panic(fmt.Errorf("error reading %s: %w", allowlistFileName, err)) + } + + err = json.Unmarshal(bytes, &allowlist) + + if err != nil { + panic(err) + } + } + return &Client{ WAClient: waClient, message: waautoresponder.AutoResponderMessage, autoResponseTimeMap: autoResponseTimeMap, + allowlist: allowlist, } } @@ -128,20 +145,22 @@ func (client *Client) updateAutoResponseTime(userId string) { func (client *Client) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.Message: - if v.Info.IsGroup { - return - } + chatUserId := v.Info.Chat.User if v.Info.IsFromMe { return } - // Ignore business chats - if v.Info.VerifiedName != nil { - return - } + if !slices.Contains(client.allowlist, chatUserId) { + if v.Info.IsGroup { + return + } - chatUserId := v.Info.Chat.User + // Ignore business chats + if v.Info.VerifiedName != nil { + return + } + } if client.hasAutoRespondedWithinSameDay(chatUserId) { fmt.Printf("Already responded to user %s, skipping\n", chatUserId)