forked from eFishery/NeMo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (76 loc) · 2.22 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
package main
import (
"os/signal"
"fmt"
"log"
"os"
"time"
"syscall"
whatsapp "github.com/Rhymen/go-whatsapp"
cron "github.com/robfig/cron/v3"
"github.com/eFishery/NeMo/utils"
"github.com/eFishery/NeMo/handlers"
)
var waHandler []handlers.WaHandler
func main() {
if len(os.Args) < 2 {
log.Fatal("You need to specify the phone number/ file session")
}
sender := os.Args[1]
jadwal := cron.New()
wac, err := whatsapp.NewConn(5 * time.Second)
wac.SetClientVersion(utils.Settings.WhatsAppClientVersionMajor, utils.Settings.WhatsAppClientVersionMinor, utils.Settings.WhatsAppClientVersionHotfix)
if err != nil {
log.Fatal("error creating connection: %v\n", err)
}
handler := &handlers.WaHandler{wac, uint64(time.Now().Unix()), make(map[string]struct{})}
wac.AddHandler(handler)
if err := utils.Login(wac, sender); 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)
}
isLoaded, Schedules := utils.ReadScheduleFiles()
if !isLoaded {
log.Println("Can't read Schedule Files")
return
}
for index := range(Schedules) {
phone_numbers := Schedules[index].ExpectedUsers
process_name := Schedules[index].ProcessName
log.Println("Read the schedule to run with cron formula " + Schedules[index].Rule)
jadwal.AddFunc(Schedules[index].Rule, func(){
log.Println("Run Schedule " + process_name)
for pIndex := range(phone_numbers) {
go utils.SendMessage(wac, Schedules[index].Message, phone_numbers[pIndex])
}
})
}
jadwal.Start()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
fmt.Println("Shutting down now.")
jadwal.Stop()
session, err := wac.Disconnect()
if err != nil {
log.Fatalf("error disconnecting: %v\n", err)
}
if err := utils.WriteSession(session, sender); err != nil {
log.Fatalf("error saving session: %v", err)
}
os.Exit(1)
}
func exit(wac *whatsapp.Conn, phone_number string) {
fmt.Println("Shutting down now.")
session, err := wac.Disconnect()
if err != nil {
log.Fatalf("error disconnecting: %v\n", err)
}
if err := utils.WriteSession(session, phone_number); err != nil {
log.Fatalf("error saving session: %v", err)
}
os.Exit(1)
}