-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (97 loc) · 2.39 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
116
package main
import (
"encoding/gob"
"fmt"
qrT "github.com/Baozisoftware/qrcode-terminal-go"
"github.com/Rhymen/go-whatsapp"
"os"
"os/signal"
"syscall"
"time"
"twentemilieu-whatsapp-bot/config"
"twentemilieu-whatsapp-bot/handlers"
"twentemilieu-whatsapp-bot/scheduler"
)
func main() {
config := config.Config{}
if err := config.SetFromBytes(); err != nil {
fmt.Println(err)
return
}
connect(&config)
}
func connect(conf *config.Config) {
wac, err := whatsapp.NewConn(60 * time.Second)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create a connection to WhatsApp: %v\n", err)
return
}
wac.SetClientVersion(2, 2021, 4)
wac.AddHandler(&handlers.TwenteHandler{Conn: wac, StartTime: uint64(time.Now().Unix())})
if err := login(wac); err != nil {
fmt.Fprintf(os.Stderr, "Could not login to WhatsApp: %v\n", err)
return
}
pong, err := wac.AdminTest()
if !pong || err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to phone: %v.\n", err)
return
}
scheduler := scheduler.Scheduler{Wac: wac, Conf: conf}
go scheduler.Watch(time.Hour * 24)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
fmt.Println("Shutting down...")
session, err := wac.Disconnect()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to disconnect from WhatsApp: %v\n", err)
return
}
if err := writeSession(session); err != nil {
fmt.Fprintf(os.Stderr, "Could not store session: %v\n", err)
return
}
}
func login(conn *whatsapp.Conn) error {
session, err := readSession()
if err != nil {
qr := make(chan string)
go func() {
term := qrT.New()
term.Get(<-qr).Print()
}()
session, err = conn.Login(qr)
if err != nil {
return fmt.Errorf("cannot login: %v", err)
}
} else {
session, err = conn.RestoreWithSession(session)
if err != nil {
return fmt.Errorf("cannot restore: %v", err)
}
}
err = writeSession(session)
return err
}
func readSession() (whatsapp.Session, error) {
session := whatsapp.Session{}
file, err := os.Open(".session.gob")
defer file.Close()
if err != nil {
return session, err
}
decoder := gob.NewDecoder(file)
err = decoder.Decode(&session)
return session, err
}
func writeSession(session whatsapp.Session) error {
file, err := os.OpenFile(".session.gob", os.O_CREATE|os.O_RDWR, 0600)
defer file.Close()
if err != nil {
return err
}
encoder := gob.NewEncoder(file)
err = encoder.Encode(session)
return err
}