/
debug.go
52 lines (44 loc) · 1.12 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package handler
import (
"context"
"github.com/macedo/whatsappbot/pkg/slice"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
)
type DebugHandler struct {
cli *whatsmeow.Client
jids []string
log waLog.Logger
}
type DebugAttributes struct {
Enabled bool `mapstructure:"enabled"`
JIDs []string `mapstructure:"jids"`
}
func NewDebugHandler(cli *whatsmeow.Client, attrs DebugAttributes) *DebugHandler {
return &DebugHandler{
cli: cli,
jids: attrs.JIDs,
log: waLog.Stdout("debug", "INFO", true),
}
}
func (h *DebugHandler) EventHandler() func(any) {
return func(rawEvt any) {
switch evt := rawEvt.(type) {
case *events.Message:
sender := evt.Info.Chat
if slice.Contains[string](h.jids, sender.String()) {
_, err := h.cli.SendMessage(context.Background(), sender, &waProto.Message{
Conversation: proto.String("OK"),
})
if err != nil {
h.log.Errorf("Failed to send message back.\n"+
"Here's what happened: %v", err)
return
}
}
}
}
}