-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
191 lines (156 loc) · 4.18 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"context"
"fmt"
"inc/lib"
"inc/lib/helpers"
"os"
"os/signal"
"path"
"syscall"
"net/http"
"text/template"
_ "inc/plugins"
_ "inc/plugins/convert"
_ "inc/plugins/ai"
_ "inc/plugins/download"
_ "inc/plugins/group"
_ "inc/plugins/info"
_ "inc/plugins/owner"
_ "inc/plugins/tools"
_ "inc/plugins/random"
_ "inc/plugins/search"
_ "github.com/mattn/go-sqlite3"
"github.com/mdp/qrterminal"
"github.com/subosito/gotenv"
"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"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
// "golang.org/x/net/webdav"
)
type Template struct {
Nama string
Status bool
}
func init() {
gotenv.Load()
//store.DeviceProps.PlatformType = waProto.DeviceProps_SAFARI.Enum()
//store.DeviceProps.PlatformType = waProto.DeviceProps_FIREFOX.Enum()
store.DeviceProps.PlatformType = waProto.DeviceProps_EDGE.Enum()
store.DeviceProps.Os = proto.String(os.Getenv("BOTNAME"))
}
var log helpers.Logger
func main() {
dbLog := waLog.Stdout("Database", "ERROR", true)
container, err := sqlstore.New("sqlite3", "file:mao.db?_foreign_keys=on", dbLog)
if err != nil {
fmt.Println("Kesalahan (error)\n"+fmt.Sprintf("%s",err));
return
}
deviceStore, err := container.GetFirstDevice()
if err != nil {
fmt.Println("Kesalahan (error)\n"+fmt.Sprintf("%s",err));
return
}
clientLog := waLog.Stdout("Client", "ERROR", true)
client := whatsmeow.NewClient(deviceStore, clientLog)
eventHandler := lib.RegisterHandler(client)
client.AddEventHandler(eventHandler)
//handler := lib.NewHandler(container)
//log.Info("Connecting Socket")
//fmt.Println("Connecting Socket")
//client := handler.Client()
client.PrePairCallback = func(jid types.JID, platform, businessName string) bool {
return true
}
if client.Store.ID == nil {
// No ID stored, new login
// Switch Mode
switch int(questLogin()) {
case 1:
fmt.Print("Masukan Nomor (628xx) : ")
var nomor string
_, err := fmt.Scanln(&nomor)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if err := client.Connect(); err != nil {
panic(err)
}
code, err := client.PairPhone(nomor, true, whatsmeow.PairClientChrome, "Chrome (Linux)")
if err != nil {
panic(err)
}
fmt.Println("Code Kamu : " + code)
break
case 2:
qrChan, _ := client.GetQRChannel(context.Background())
if err := client.Connect(); err != nil {
panic(err)
}
for evt := range qrChan {
switch string(evt.Event) {
case "code":
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
log.Info("Qr Required")
break
}
}
break
default:
panic("Pilih apa?")
}
} else {
// Already logged in, just connect
err := client.Connect();
if err != nil {
fmt.Println(err)
}
fmt.Println("Connected Sockett")
}
go HttpStatic(client)
// 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 questLogin() int {
fmt.Println("Silahlan Pilih Opsi Login :")
fmt.Println("1. Pairing Code")
fmt.Println("2. Qr")
fmt.Print("Pilih : ")
var input int
_, err := fmt.Scanln(&input)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 0
}
return input
}
func HttpStatic(client *whatsmeow.Client) {
var port string = os.Getenv("PORT")
if port == "" {
port = "8080"
}
var filepath = path.Join("lib", "index.html")
var tmpl, _ = template.ParseFiles(filepath)
http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
data := Template{
Nama: client.Store.PushName,
Status: client.IsConnected(),
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/index", http.StatusTemporaryRedirect)
})
//fmt.Println("server started at localhost:" + port)
http.ListenAndServe(":"+port, nil)
}