-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (75 loc) · 2.07 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
package main
import (
"TuruBot/configs"
"TuruBot/handler"
"context"
"fmt"
"os"
"os/signal"
"syscall"
// _ "github.com/jackc/pgx/v4/stdlib" // <= pgsql driver pake ini
"github.com/mdp/qrterminal"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
//_ "modernc.org/sqlite"
_ "github.com/mattn/go-sqlite3"
)
func main() {
dbLog := waLog.Stdout("Database", "INFO", true)
// testing config
// jangan lupa ubah pake pgsql kalo udah production
// sessionName := fmt.Sprintf("postgresql://%s:%s@%s/%s", configs.GetEnv("DB_USER"), configs.GetEnv("DB_PASS"), configs.GetEnv("DB_HOST"), configs.GetEnv("DB_NAME"))
sessionName := fmt.Sprintf("%s.sqlite?_pragma=foreign_keys=1&_journal_mode=WAL", configs.GetEnv("SESSION_NAME"))
container, err := sqlstore.New("sqlite3", sessionName, dbLog)
if err != nil {
panic(err)
}
deviceStore, err := container.GetFirstDevice()
if err != nil {
panic(err)
}
container.Upgrade()
clientLog := waLog.Stdout("Client", "INFO", true)
client := whatsmeow.NewClient(deviceStore, clientLog)
eventHandler := registerHandler(client)
client.AddEventHandler(eventHandler)
if client.Store.ID == nil {
// login
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)
fmt.Println("QR code received, please scan the qr code!")
} else {
fmt.Println("Login event:", evt.Event)
}
}
} else {
// reconnect, existing session
err = client.Connect()
if err != nil {
panic(err)
}
}
// channel untuk exit terima sinyal exit
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
client.Disconnect()
}
func registerHandler(client *whatsmeow.Client) func(evt interface {}) {
return func(evt interface {}) {
switch v := evt.(type) {
case *events.Message:
go handler.EventHandler(client, v)
default:
return
}
}
}