Add allowlist

This commit is contained in:
Sangeeth Sudheer 2025-04-01 17:27:08 +05:30
parent 9e593b2b01
commit 6258f1537b
Signed by: x
GPG Key ID: F6D06ECE734C57D1

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math/rand/v2" "math/rand/v2"
"os" "os"
"slices"
"strconv" "strconv"
"time" "time"
@ -19,23 +20,22 @@ import (
) )
const autoResponseTimeMapJsonFileName = "auto-response-time-map.json" const autoResponseTimeMapJsonFileName = "auto-response-time-map.json"
const allowlistFileName = "allowlist.json"
type Client struct { type Client struct {
WAClient *whatsmeow.Client WAClient *whatsmeow.Client
message string message string
autoResponseTimeMap map[string]string autoResponseTimeMap map[string]string
allowlist []string
} }
func NewClient(waClient *whatsmeow.Client) *Client { func NewClient(waClient *whatsmeow.Client) *Client {
autoResponseTimeMap := map[string]string{} autoResponseTimeMap := map[string]string{}
allowlist := []string{}
fileInfo, _ := os.Stat(autoResponseTimeMapJsonFileName) fileInfo, _ := os.Stat(autoResponseTimeMapJsonFileName)
if fileInfo != nil { if fileInfo != nil {
if fileInfo.IsDir() {
panic(fmt.Errorf("expected %s to be a file, but found dir", autoResponseTimeMapJsonFileName))
}
bytes, err := os.ReadFile(autoResponseTimeMapJsonFileName) bytes, err := os.ReadFile(autoResponseTimeMapJsonFileName)
if err != nil { 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{ return &Client{
WAClient: waClient, WAClient: waClient,
message: waautoresponder.AutoResponderMessage, message: waautoresponder.AutoResponderMessage,
autoResponseTimeMap: autoResponseTimeMap, autoResponseTimeMap: autoResponseTimeMap,
allowlist: allowlist,
} }
} }
@ -128,11 +145,14 @@ func (client *Client) updateAutoResponseTime(userId string) {
func (client *Client) eventHandler(evt interface{}) { func (client *Client) eventHandler(evt interface{}) {
switch v := evt.(type) { switch v := evt.(type) {
case *events.Message: case *events.Message:
if v.Info.IsGroup { chatUserId := v.Info.Chat.User
if v.Info.IsFromMe {
return return
} }
if v.Info.IsFromMe { if !slices.Contains(client.allowlist, chatUserId) {
if v.Info.IsGroup {
return return
} }
@ -140,8 +160,7 @@ func (client *Client) eventHandler(evt interface{}) {
if v.Info.VerifiedName != nil { if v.Info.VerifiedName != nil {
return return
} }
}
chatUserId := v.Info.Chat.User
if client.hasAutoRespondedWithinSameDay(chatUserId) { if client.hasAutoRespondedWithinSameDay(chatUserId) {
fmt.Printf("Already responded to user %s, skipping\n", chatUserId) fmt.Printf("Already responded to user %s, skipping\n", chatUserId)