-
Notifications
You must be signed in to change notification settings - Fork 1
/
whatsapp.go
70 lines (62 loc) · 1.43 KB
/
whatsapp.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
package main
import (
"context"
_ "github.com/mattn/go-sqlite3"
"os"
"time"
"github.com/mdp/qrterminal/v3"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
)
func setupWhatsAppClient() {
dbLog := waLog.Stdout("Database", "ERROR", true)
container, err := sqlstore.New("sqlite3", "file:whatsapp.db?_foreign_keys=on", dbLog)
if err != nil {
panic(err)
}
deviceStore, err := container.GetFirstDevice()
if err != nil {
panic(err)
}
clientLog := waLog.Stdout("Client", "ERROR", true)
client = whatsmeow.NewClient(deviceStore, clientLog)
client.AddEventHandler(eventHandler)
if client.Store.ID == nil {
qrChan, _ := client.GetQRChannel(context.Background())
err = client.Connect()
if err != nil {
panic(err)
}
for evt := range qrChan {
if evt.Event == "code" {
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
}
}
} else {
err = client.Connect()
if err != nil {
panic(err)
}
}
client.SendPresence(types.PresenceAvailable)
}
func eventHandler(evt interface{}) {
switch v := evt.(type) {
case *events.Presence:
jid := v.From.String()
status := "Online"
if v.Unavailable {
status = "Offline"
}
mu.Lock()
userStatus[jid] = status
userStatusLog[jid] = append(userStatusLog[jid], StatusLog{
Time: time.Now().UTC(),
Status: status,
})
mu.Unlock()
}
}