-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
115 lines (104 loc) · 2.58 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"context"
"errors"
"flag"
"os"
"os/signal"
"strings"
"syscall"
_ "github.com/mattn/go-sqlite3"
qrterminal "github.com/mdp/qrterminal/v3"
"go.mau.fi/whatsmeow"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/store/sqlstore"
waLog "go.mau.fi/whatsmeow/util/log"
)
var (
cli *whatsmeow.Client
log waLog.Logger
)
func main() {
waBinary.IndentXML = true
debugLogs := flag.Bool("debug", false, "Enable debug logs?")
dbDialect := flag.String("db-dialect", "sqlite3", "Database dialect (sqlite3 or postgres)")
dbAddress := flag.String("db-address", "file:db/examplestore.db?_foreign_keys=on", "Database address")
flag.Parse()
logLevel := "INFO"
if *debugLogs {
logLevel = "DEBUG"
}
log = waLog.Stdout("Main", logLevel, true)
dbLog := waLog.Stdout("Database", logLevel, true)
storeContainer, err := sqlstore.New(*dbDialect, *dbAddress, dbLog)
if err != nil {
log.Errorf("Failed to connect to database: %v", err)
return
}
device, err := storeContainer.GetFirstDevice()
if err != nil {
log.Errorf("Failed to get device: %v", err)
return
}
cli = whatsmeow.NewClient(device, waLog.Stdout("Client", logLevel, true))
ch, err := cli.GetQRChannel(context.Background())
if err != nil {
// This error means that we're already logged in, so ignore it.
if !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
log.Errorf("Failed to get QR channel: %v", err)
}
} else {
go func() {
for evt := range ch {
if evt.Event == "code" {
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
} else {
log.Infof("QR channel result: %s", evt.Event)
}
}
}()
}
cli.AddEventHandler(handler)
err = cli.Connect()
if err != nil {
log.Errorf("Failed to connect: %v", err)
return
}
c := make(chan os.Signal, 1)
input := make(chan string, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
defer close(input)
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
line := strings.TrimSpace(scan.Text())
if len(line) > 0 {
input <- line
}
}
}()
for {
log.Infof(`Send Text -> send <jid> <text>
Send Image -> sendimg <jid> <image path> [caption]
Send Bulk Text -> sendbulk <csv file>
Send Bulk Image -> sendbulkimg <csv file>
Exit -> Crtl+C`)
select {
case <-c:
log.Infof("Interrupt received, exiting")
cli.Disconnect()
return
case cmd := <-input:
if len(cmd) == 0 {
log.Infof("Stdin closed, exiting")
cli.Disconnect()
return
}
args := strings.Fields(cmd)
cmd = args[0]
args = args[1:]
go handleCmd(strings.ToLower(cmd), args)
}
}
}