-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (100 loc) · 2.75 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
package main
import (
"encoding/gob"
"fmt"
"os"
"strings"
"time"
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
"github.com/Rhymen/go-whatsapp"
)
type waHandler struct {
wac *whatsapp.Conn
startTime uint64
}
func (wh *waHandler) HandleError(err error) {
if strings.Contains(err.Error(), "invalid string with tag 174") {
return
}
fmt.Fprintf(os.Stderr, "error caught in handler: %v\n", err)
}
// HandleTextMessage receives whatsapp text messages and checks if the message was send by the current
// user, if it does not contain the keyword '@echo' or if it is from before the program start and then returns.
// Otherwise the message is echoed back to the original author.
func (wh *waHandler) HandleTextMessage(message whatsapp.TextMessage) {
if message.Info.FromMe || !strings.Contains(strings.ToLower(message.Text), "@echo") || message.Info.Timestamp < wh.startTime {
return
}
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: message.Info.RemoteJid,
},
Text: message.Text,
}
if _, err := wh.wac.Send(msg); err != nil {
fmt.Fprintf(os.Stderr, "error sending message: %v\n", err)
}
fmt.Printf("echoed message '%v' to user %v\n", message.Text, message.Info.RemoteJid)
}
func login(wac *whatsapp.Conn) error {
session, err := readSession()
if err == nil {
session, err = wac.RestoreWithSession(session)
if err != nil {
return fmt.Errorf("restoring session failed: %v", err)
}
} else {
qr := make(chan string)
go func() {
terminal := qrcodeTerminal.New()
terminal.Get(<-qr).Print()
}()
session, err = wac.Login(qr)
if err != nil {
return fmt.Errorf("error during login: %v", err)
}
}
if err = writeSession(session); err != nil {
return fmt.Errorf("error saving session: %v", err)
}
return nil
}
func readSession() (whatsapp.Session, error) {
session := whatsapp.Session{}
file, err := os.Open(os.TempDir() + "/whatsappSession.gob")
if err != nil {
return session, err
}
defer file.Close()
decoder := gob.NewDecoder(file)
if err = decoder.Decode(&session); err != nil {
return session, err
}
return session, nil
}
func writeSession(session whatsapp.Session) error {
fmt.Printf("%#v", session)
file, err := os.Create(os.TempDir() + "/whatsappSession.gob")
if err != nil {
return err
}
defer file.Close()
encoder := gob.NewEncoder(file)
if err = encoder.Encode(session); err != nil {
return err
}
return nil
}
func main() {
wac, err := whatsapp.NewConn(5 * time.Second)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating connection: %v\n", err)
return
}
wac.AddHandler(&waHandler{wac, uint64(time.Now().Unix())})
if err = login(wac); err != nil {
fmt.Fprintf(os.Stderr, "error logging in: %v\n", err)
return
}
<-time.After(60 * time.Minute)
}