-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (101 loc) · 2.61 KB
/
main.go
File metadata and controls
121 lines (101 loc) · 2.61 KB
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
117
118
119
120
121
package main
import (
whatsapp "github.com/Rhymen/go-whatsapp"
qrT "github.com/Baozisoftware/qrcode-terminal-go"
"fmt"
"time"
"os"
"os/signal"
"syscall"
"encoding/gob"
"./handlers"
)
func main() {
// Create a connection to WhatsApp.
wac, err := whatsapp.NewConn(20 * time.Second);
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create a connection to WhatsApp: %v\n", err)
return
}
wac.AddHandler(&handlers.WaaierHandler{Conn: wac, StartTime: uint64(time.Now().Unix())})
// Login to WhatsApp.
if err = login(wac); err != nil {
fmt.Fprintf(os.Stderr, "Could not login to WhatsApp: %v\n", err)
return
}
// Verify phone connectivity.
pong, err := wac.AdminTest()
if !pong || err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to phone: %v.\n", err)
return
}
// Wait for a SIGTERM.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
// Disconnect safely.
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 {
// Try to load previous session.
session, err := readSession()
if err != nil {
// No previous session, or error loading. Make a new one.
qr := make(chan string)
go func() {
term := qrT.New()
term.Get(<-qr).Print()
}()
// Create a new session and store it.
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
}
// Read the session from the session file
// and try to reconnect to WhatsApp.
func readSession() (whatsapp.Session, error) {
// Create a dummy session object.
session := whatsapp.Session{}
// Try to read the previous session file.
file, err := os.Open(".session.gob")
defer file.Close()
if err != nil {
return session, err
}
// Decode and return the session.
decoder := gob.NewDecoder(file)
err = decoder.Decode(&session)
return session, err
}
// Store the session to a file for later
// restoring.
func writeSession(session whatsapp.Session) error {
// Open the file and write.
file, err := os.OpenFile(".session.gob", os.O_CREATE|os.O_RDWR, 0600)
defer file.Close()
if err != nil {
return err
}
// Encode the session and store it in the file.
encoder := gob.NewEncoder(file)
err = encoder.Encode(session)
return err
}