Add allowlist
This commit is contained in:
parent
9e593b2b01
commit
6258f1537b
@ -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,20 +145,22 @@ 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
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.Info.IsFromMe {
|
if v.Info.IsFromMe {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore business chats
|
if !slices.Contains(client.allowlist, chatUserId) {
|
||||||
if v.Info.VerifiedName != nil {
|
if v.Info.IsGroup {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chatUserId := v.Info.Chat.User
|
// Ignore business chats
|
||||||
|
if v.Info.VerifiedName != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user