-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
95 lines (86 loc) · 2.36 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"go.vnia.dev/message"
_ "github.com/mattn/go-sqlite3"
"github.com/mdp/qrterminal"
"github.com/probandula/figlet4go"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
)
func main() {
dbLog := waLog.Stdout("Database", "ERROR", true)
// Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite
container, err := sqlstore.New("sqlite3", "file:sessions.db?_foreign_keys=on", dbLog)
if err != nil {
panic(err)
}
// If you want multiple sessions, remember their JIDs and use .GetDevice(jid) or .GetAllDevices() instead.
deviceStore, err := container.GetFirstDevice()
if err != nil {
panic(err)
}
clientLog := waLog.Stdout("Client", "ERROR", true)
// Client
client := whatsmeow.NewClient(deviceStore, clientLog)
eventHandler := registerHandler(client)
client.AddEventHandler(eventHandler)
if client.Store.ID == nil {
// No ID stored, new login
qrChan, _ := client.GetQRChannel(context.Background())
err = client.Connect()
if err != nil {
panic(err)
}
for evt := range qrChan {
if evt.Event == "code" {
// Render the QR code here
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
// or just manually `echo 2@... | qrencode -t ansiutf8` in a terminal
log.Println("Please Scan")
} else {
log.Println("Succes Login")
}
}
} else {
// Already logged in, just connect
err = client.Connect()
log.Println("Succes Login")
if err != nil {
panic(err)
}
}
// Listen to Ctrl+C (you can also do something else that prevents the program from exiting)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
client.Disconnect()
}
func init() {
ascii := figlet4go.NewAsciiRender()
renderStr, _ := ascii.Render("VNIA BOT")
// Set Browser
store.DeviceProps.PlatformType = waProto.DeviceProps_SAFARI.Enum()
store.DeviceProps.Os = proto.String("VNIA BOT")
// Print Banner
fmt.Print(renderStr)
}
func registerHandler(client *whatsmeow.Client) func(evt interface{}) {
return func(evt interface{}) {
switch v := evt.(type) {
case *events.Message:
go message.Msg(client, v)
break
}
}
}