-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
299 lines (245 loc) · 7.14 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"context"
"os/exec"
"time"
"net/http"
"runtime"
"go.wasender.api/helper"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"log"
"path/filepath"
"os"
_ "github.com/mattn/go-sqlite3"
"github.com/mdp/qrterminal"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/store/sqlstore"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
// "go.mau.fi/whatsmeow/types"
)
func init() {
store.DeviceProps.PlatformType = waProto.DeviceProps_ANDROID_PHONE.Enum() // set props to android device
store.DeviceProps.Os = proto.String("WaSender-API")
}
func clearScreen() {
if runtime.GOOS == "windows" {
c := exec.Command("cls")
c.Stdout = os.Stdout
c.Run()
} else {
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
}
}
func AutoDeleteFiles() {
log.Println("Starting Temporary cleaner goroutine")
interval := 5 * time.Minute
timer := time.NewTimer(interval)
for {
select {
case <-timer.C:
err := deleteTempFiles()
if err != nil {
log.Println("Failed to delete temp files:", err)
}
timer.Reset(interval)
}
}
}
func deleteTempFiles() error {
dir, err := filepath.Abs("temp/")
if err != nil {
return err
}
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
err := os.Remove(filepath.Join(dir, file.Name()))
if err != nil {
log.Println("Failed to delete file:", err)
}
}
return nil
}
func main() {
// Set gin to release
gin.SetMode(gin.ReleaseMode)
// register new gin container
rgin := gin.New()
// register sqlite container
dbLog := waLog.Stdout("Database", "ERROR", true)
container, err := sqlstore.New("sqlite3", "file:secret/session.db", dbLog)
if err != nil {
log.Println("Failed to create sqlite3 container:", err)
return
}
deviceStore, err := container.GetFirstDevice()
if err != nil {
log.Println("Failed to retrieve stored data from container", err)
return
}
clientLog := waLog.Stdout("Client", "ERROR", true)
client := whatsmeow.NewClient(deviceStore, clientLog)
if client.Store.ID == nil {
qr_code, _ := client.GetQRChannel(context.Background())
err = client.Connect()
if err != nil {
log.Println("Failed to generate qrcode", err)
return
}
for evt := range qr_code {
log.Println(evt.Event)
if evt.Event == "code" {
clearScreen()
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
log.Println("Scan QRCode to Login !")
} else if evt.Event == "err-client-outdated" {
clearScreen()
log.Println("Client outdated, please upgrade the whatsmeow modules")
return // return because error in outdated modules
} else {
clearScreen()
log.Println("Connected to whatsapp")
// Starting goroutine for autodelete temporary folder
go AutoDeleteFiles()
}
}
} else {
err = client.Connect()
if err != nil {
log.Println("Cannot open connection to whatsapp", err)
return
}
// start go routines for auto delete temp files
clearScreen()
log.Println("Connected to whatsapp")
// Starting goroutine for autodelete temporary folder
go AutoDeleteFiles()
}
// register helper as hexec
hexec := helper.Register(client)
// Gin default route and authentication
rgin.Use(func(c *gin.Context) {
token := c.GetHeader("Authorization")
if token != "Bearer your_secret_token" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
c.Abort()
return
}
})
// handle for /sendtext
rgin.POST("/sendtext", func(c *gin.Context) {
var req struct {
To string `json:"to"`
Text string `json:"text"`
}
err := c.BindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return
}
clientIP := c.ClientIP()
log.Println("Requests received from", clientIP)
err = hexec.SendMessage(req.To, req.Text)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send message"})
log.Println("Failed to process request from", clientIP, "to sending a message to", req.To)
return
}
log.Println("Success process request from", clientIP, "to sending a message to", req.To)
c.JSON(http.StatusOK, gin.H{"message": "message sent successfully"})
})
// handle for /senddoc
rgin.POST("/senddoc", func(c *gin.Context) {
var req struct {
To string `json:"to"`
Caption string `json:"caption"`
Filename string `json:"filename"`
Document []byte `json:"document"`
}
err := c.BindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return
}
// Create temporary files
uuid := uuid.New().String()
tempFile, err := os.CreateTemp("temp/", uuid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create temporary file"})
return
}
defer tempFile.Close()
// Write the document data to the temporary file
_, err = tempFile.Write(req.Document)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to write document to file"})
return
}
clientIP := c.ClientIP()
log.Println("Requests received from", clientIP)
// Send the document
filedir := filepath.Join(tempFile.Name())
err = hexec.SendDocument(req.To, filedir, req.Caption, req.Filename)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send document"})
log.Println("Failed to process request from", clientIP, "to sending a document to", req.To)
return
}
log.Println("Success process request from", clientIP, "to sending a document to", req.To)
c.JSON(http.StatusOK, gin.H{"message": "document sent successfully"})
})
// handle for /sendimg
rgin.POST("/sendimg", func(c *gin.Context) {
var req struct {
To string `json:"to"`
Caption string `json:"caption"`
Image []byte `json:"image"`
}
err := c.BindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return
}
// Create temporary files
uuid := uuid.New().String()
tempFile, err := os.CreateTemp("temp/", uuid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create temporary file"})
return
}
defer tempFile.Close()
// Write the document data to the temporary file
_, err = tempFile.Write(req.Image)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to write document to file"})
return
}
clientIP := c.ClientIP()
log.Println("Requests received from", clientIP)
// Send the image
filedir := filepath.Join(tempFile.Name())
err = hexec.SendImage(req.To, filedir, req.Caption)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send image"})
log.Println("Failed to process request from", clientIP, "to sending a images to", req.To)
return
}
log.Println("Success process request from", clientIP, "to sending a image to", req.To)
c.JSON(http.StatusOK, gin.H{"message": "image sent successfully"})
})
// listen at 8080
rgin.Run(":8080")
// set the c make chain to 1 for fixing should bufferd
c := make(chan os.Signal, 1)
<-c
// disconnecting whatsmeow client
client.Disconnect()
}