-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
161 lines (138 loc) · 3.62 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"encoding/gob"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
whatsapp "github.com/Rhymen/go-whatsapp"
"github.com/joho/godotenv"
qrcodeTerminal "github.com/mdp/qrterminal/v3"
)
type waHandler struct {
c *whatsapp.Conn
startTime uint64
}
type param struct {
phoneNumber string
dirOutput string
}
var params param
func main() {
mydir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
err = godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
sessionOutput := flag.String("o", mydir+"/sessions", "an output dir")
pN := flag.String("p", "6288", "phone number")
flag.Parse()
params.dirOutput = *sessionOutput
params.phoneNumber = *pN
if params.dirOutput == mydir && params.phoneNumber == "6288" {
fmt.Println("example usage " + os.Args[0] + " -o ~/sessions -p 62872123123")
os.Exit(1)
}
wac, err := whatsapp.NewConn(1 * time.Second)
if err != nil {
log.Fatalf("error creating connection: %v\n", err)
}
var major, minor, patch int = 2, 2123, 7
if clientVersion := os.Getenv("CLIENT_VERSION"); clientVersion != "" {
cv := strings.Split(clientVersion, ".")
// Major, minor, patch all there
major, err = strconv.Atoi(cv[0])
if err != nil {
fmt.Println("Client version format wrong, major section is not int", err)
major = 2
}
minor, err = strconv.Atoi(cv[1])
if err != nil {
fmt.Println("Client version format wrong, minor section is not int", err)
minor = 2123
}
patch, err = strconv.Atoi(cv[2])
if err != nil {
fmt.Println("Client version format wrong, patch section is not int", err)
patch = 7
}
}
wac.SetClientVersion(major, minor, patch)
wac.AddHandler(&waHandler{wac, uint64(time.Now().Unix())})
if err := login(wac); err != nil {
log.Fatalf("error logging in: %v\n", err)
}
pong, err := wac.AdminTest()
if !pong || err != nil {
log.Fatalf("error pinging in: %v\n", err)
}
session, err := wac.Disconnect()
if err != nil {
log.Fatalf("error disconnecting: %v\n", err)
}
if err := writeSession(session, params.phoneNumber); err != nil {
log.Fatalf("error saving session: %v", err)
}
}
func login(wac *whatsapp.Conn) error {
qr := make(chan string)
go func() {
config := qrcodeTerminal.Config{
Level: qrcodeTerminal.L,
Writer: os.Stdout,
BlackChar: qrcodeTerminal.BLACK,
WhiteChar: qrcodeTerminal.WHITE,
QuietZone: 1,
}
qrcodeTerminal.GenerateWithConfig(<-qr, config)
}()
session, err := wac.Login(qr)
if err != nil {
return fmt.Errorf("error during login: %v\n", err)
}
err = writeSession(session, params.phoneNumber)
if err != nil {
return fmt.Errorf("error saving session: %v\n", err)
}
return nil
}
func writeSession(session whatsapp.Session, phoneNumber string) error {
file, err := os.Create(getSessionName(phoneNumber))
if err != nil {
return err
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(session)
if err != nil {
return err
}
return nil
}
func getSessionName(phoneNumber string) string {
if _, err := os.Stat(params.dirOutput); os.IsNotExist(err) {
os.MkdirAll(params.dirOutput, os.ModePerm)
}
return params.dirOutput + "/" + phoneNumber + ".gob"
}
//HandleError needs to be implemented to be a valid WhatsApp handler
func (h *waHandler) HandleError(err error) {
if e, ok := err.(*whatsapp.ErrConnectionFailed); ok {
log.Printf("Connection failed, underlying error: %v", e.Err)
log.Println("Waiting 30sec...")
<-time.After(30 * time.Second)
log.Println("Reconnecting...")
err := h.c.Restore()
if err != nil {
log.Fatalf("Restore failed: %v", err)
}
} else {
log.Printf("error occoured: %v\n", err)
}
}