Skip to content

Commit c527a76

Browse files
committed
Everything is DONE
1 parent b4e860e commit c527a76

12 files changed

Lines changed: 377 additions & 17 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/gofiber/fiber/v3 v3.0.0
1717
github.com/golang-jwt/jwt/v5 v5.3.1
1818
github.com/google/uuid v1.6.0
19+
github.com/gorilla/websocket v1.5.3
1920
github.com/joho/godotenv v1.5.1
2021
github.com/mattn/go-sqlite3 v1.14.34
2122
github.com/mdp/qrterminal/v3 v3.2.1
@@ -76,7 +77,6 @@ require (
7677
github.com/google/s2a-go v0.1.9 // indirect
7778
github.com/googleapis/enterprise-certificate-proxy v0.3.12 // indirect
7879
github.com/googleapis/gax-go/v2 v2.17.0 // indirect
79-
github.com/gorilla/websocket v1.5.3 // indirect
8080
github.com/jupiterrider/ffi v0.6.0 // indirect
8181
github.com/klauspost/compress v1.18.4 // indirect
8282
github.com/leodido/go-urn v1.4.0 // indirect

servers/soham/common/communication.interface.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package soham_common_req_keys
33
type MessageType string
44

55
const (
6-
STATUS_MESSAGE MessageType = "STATUS_MESSAGE"
7-
REPSONSE_MESSAGE MessageType = "RESPONSE_MESSAGE"
8-
SEND_TEXT_MESSAGE MessageType = "SEND_TEXT_MESSAGE"
6+
STATUS_MESSAGE MessageType = "STATUS_MESSAGE"
7+
REPSONSE_MESSAGE MessageType = "RESPONSE_MESSAGE"
8+
SEND_TEXT_MESSAGE MessageType = "SEND_TEXT_MESSAGE"
9+
SEND_BASE64_IMAGE MessageType = "SEND_BASE64_IMAGE"
10+
SEND_WEB_MEDIA MessageType = "SEND_WEB_MEDIA"
11+
SEND_FILE_PATH_MEDIA MessageType = "SEND_FILE_PATH_MEDIA"
912
)
1013

1114
type WhatsappClientMessage struct {
@@ -23,3 +26,34 @@ type SendTextMessage struct {
2326
ApiSendRequestBase
2427
Message string `json:"message" validate:"required"`
2528
}
29+
30+
type SendBase64Image struct {
31+
ApiSendRequestBase
32+
Base64 string `json:"base64" validate:"required"`
33+
// Ext string `json:"ext_name" validate:"required,regexp=^.[\\w]*$"`
34+
Media string `json:"media_name" validate:"required"`
35+
ImageDesc string `json:"image_desc"`
36+
}
37+
38+
type SendWebMediaType struct {
39+
ApiSendRequestBase
40+
WebMediaLink string `json:"web_media_link" validate:"required,url"`
41+
MediaName string `json:"media_name" validate:"required"`
42+
ImageDesc string `json:"image_desc"`
43+
}
44+
45+
type SendFilePathMediaType struct {
46+
ApiSendRequestBase
47+
LocalMediaPath string `json:"local_media_path" validate:"required"`
48+
ImageDesc string `json:"image_desc"`
49+
// {
50+
// key: 'local_media_path',
51+
// required: true,
52+
// type: 'string',
53+
// },
54+
// {
55+
// key: 'image_desc',
56+
// required: false,
57+
// type: 'string',
58+
// },
59+
}

servers/soham/common/req.keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ const (
99
ERROR_NUMBER_TOKEN_NOT_PASSED = 204
1010
ERROR_GENERATE_TOKEN_MISMATCH = 205
1111
ERROR_MISMATCH_NUMBER_FROM_TOKEN = 206
12+
ERROR_INVALID_WEB_MEDIA_LINK = 207
1213
)
281 KB
Loading

servers/soham/whatsapp-client/websoceket/index.go

Lines changed: 203 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import (
55
"log"
66
"net/http"
77
"net/url"
8+
"path"
9+
"path/filepath"
810
"strconv"
911
"time"
1012

1113
"github.com/gorilla/websocket"
14+
"github.com/rpsoftech/golang-servers/env"
1215
whatsapp_core "github.com/rpsoftech/golang-servers/functions/whatsapp/core"
1316
"github.com/rpsoftech/golang-servers/interfaces"
1417
soham_common_req_keys "github.com/rpsoftech/golang-servers/servers/soham/common"
18+
utility_functions "github.com/rpsoftech/golang-servers/utility/functions"
1519
)
1620

1721
type WebsocketConnectionObject struct {
@@ -23,7 +27,7 @@ type WebsocketConnectionObject struct {
2327
UUID string
2428
NUMBER string
2529
WhatsappConnectionMap *whatsapp_core.IWhatsappConnectionMap
26-
WhatsappConnectino *whatsapp_core.WhatsappConnection
30+
WhatsappConnection *whatsapp_core.WhatsappConnection
2731
}
2832

2933
// var connectionCalled = false
@@ -61,7 +65,20 @@ func (w *WebsocketConnectionObject) InitalizeWebsocket() {
6165
continue
6266
}
6367
if whatsappClientMessage.Type == soham_common_req_keys.SEND_TEXT_MESSAGE {
64-
w.SendTextMessage(&whatsappClientMessage)
68+
go w.SendTextMessage(&whatsappClientMessage)
69+
continue
70+
}
71+
if whatsappClientMessage.Type == soham_common_req_keys.SEND_BASE64_IMAGE {
72+
go w.SendBase64Image(&whatsappClientMessage)
73+
continue
74+
}
75+
if whatsappClientMessage.Type == soham_common_req_keys.SEND_WEB_MEDIA {
76+
go w.SendWebMedia(&whatsappClientMessage)
77+
continue
78+
}
79+
if whatsappClientMessage.Type == soham_common_req_keys.SEND_FILE_PATH_MEDIA {
80+
go w.SendMediaWithFilePath(&whatsappClientMessage)
81+
continue
6582
}
6683
}
6784
done <- 1
@@ -73,6 +90,7 @@ func (w *WebsocketConnectionObject) InitalizeWebsocket() {
7390
time.Sleep(time.Second * 10)
7491
done <- 1
7592
}()
93+
w.WhatsappConnectionStatus = soham_common_req_keys.DISCONNECTED
7694
<-done
7795
w.InitalizeWebsocket()
7896
}
@@ -98,7 +116,7 @@ func (w *WebsocketConnectionObject) SendTextMessage(wcm *soham_common_req_keys.W
98116
})
99117
return
100118
}
101-
if w.WhatsappConnectino == nil {
119+
if w.WhatsappConnection == nil {
102120
wc, ok := (*w.WhatsappConnectionMap)[w.UUID]
103121
if !ok {
104122
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
@@ -108,9 +126,9 @@ func (w *WebsocketConnectionObject) SendTextMessage(wcm *soham_common_req_keys.W
108126
})
109127
return
110128
}
111-
w.WhatsappConnectino = wc
129+
w.WhatsappConnection = wc
112130
}
113-
wc := w.WhatsappConnectino
131+
wc := w.WhatsappConnection
114132
if wc.ConnectionStatus != 1 {
115133
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
116134
ReqId: reqId,
@@ -132,6 +150,180 @@ func (w *WebsocketConnectionObject) SendTextMessage(wcm *soham_common_req_keys.W
132150
})
133151
}
134152

153+
func (w *WebsocketConnectionObject) SendBase64Image(wcm *soham_common_req_keys.WhatsappClientMessage) {
154+
reqId := wcm.ReqId
155+
jsonData, err := json.Marshal(wcm.Message)
156+
if err != nil {
157+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
158+
ReqId: reqId,
159+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
160+
Message: interfaces.RequestError{Message: "Invalid message type"},
161+
})
162+
return
163+
}
164+
body := new(soham_common_req_keys.SendBase64Image)
165+
err = json.Unmarshal(jsonData, body)
166+
if err != nil {
167+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
168+
ReqId: reqId,
169+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
170+
Message: interfaces.RequestError{Message: "Invalid message type"},
171+
})
172+
return
173+
}
174+
if w.WhatsappConnection == nil {
175+
wc, ok := (*w.WhatsappConnectionMap)[w.UUID]
176+
if !ok {
177+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
178+
ReqId: reqId,
179+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
180+
Message: interfaces.RequestError{Message: "Invalid from number"},
181+
})
182+
return
183+
}
184+
w.WhatsappConnection = wc
185+
}
186+
wc := w.WhatsappConnection
187+
if wc.ConnectionStatus != 1 {
188+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
189+
ReqId: reqId,
190+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
191+
Message: interfaces.RequestError{Message: "Whatsapp not connected"},
192+
})
193+
return
194+
}
195+
toNumber := make([]string, len(body.To))
196+
for i, v := range body.To {
197+
toNumber[i] = strconv.Itoa(v)
198+
}
199+
200+
resp := wc.SendMediaFileBase64(toNumber, body.Base64, body.Media, body.ImageDesc)
201+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
202+
ReqId: reqId,
203+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
204+
Message: resp,
205+
})
206+
}
207+
208+
func (w *WebsocketConnectionObject) SendWebMedia(wcm *soham_common_req_keys.WhatsappClientMessage) {
209+
reqId := wcm.ReqId
210+
jsonData, err := json.Marshal(wcm.Message)
211+
if err != nil {
212+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
213+
ReqId: reqId,
214+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
215+
Message: interfaces.RequestError{Message: "Invalid message type"},
216+
})
217+
return
218+
}
219+
body := new(soham_common_req_keys.SendWebMediaType)
220+
err = json.Unmarshal(jsonData, body)
221+
if err != nil {
222+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
223+
ReqId: reqId,
224+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
225+
Message: interfaces.RequestError{Message: "Invalid message type", Extra: err},
226+
})
227+
return
228+
}
229+
filePath := path.Join(env.FindAndReturnCurrentDir(), "tmp", body.MediaName)
230+
err = utility_functions.DownloadFile(filePath, body.WebMediaLink)
231+
if err != nil {
232+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
233+
ReqId: reqId,
234+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
235+
Message: interfaces.RequestError{Message: "Failed to download media file", Extra: err},
236+
})
237+
return
238+
}
239+
if w.WhatsappConnection == nil {
240+
wc, ok := (*w.WhatsappConnectionMap)[w.UUID]
241+
if !ok {
242+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
243+
ReqId: reqId,
244+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
245+
Message: interfaces.RequestError{Message: "Invalid from number"},
246+
})
247+
return
248+
}
249+
w.WhatsappConnection = wc
250+
}
251+
wc := w.WhatsappConnection
252+
if wc.ConnectionStatus != 1 {
253+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
254+
ReqId: reqId,
255+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
256+
Message: interfaces.RequestError{Message: "Whatsapp not connected"},
257+
})
258+
return
259+
}
260+
toNumber := make([]string, len(body.To))
261+
for i, v := range body.To {
262+
toNumber[i] = strconv.Itoa(v)
263+
}
264+
265+
resp := wc.SendMediaFileWithPath(toNumber, filePath, body.MediaName, body.ImageDesc)
266+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
267+
ReqId: reqId,
268+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
269+
Message: resp,
270+
})
271+
}
272+
func (w *WebsocketConnectionObject) SendMediaWithFilePath(wcm *soham_common_req_keys.WhatsappClientMessage) {
273+
reqId := wcm.ReqId
274+
jsonData, err := json.Marshal(wcm.Message)
275+
if err != nil {
276+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
277+
ReqId: reqId,
278+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
279+
Message: interfaces.RequestError{Message: "Invalid message type"},
280+
})
281+
return
282+
}
283+
body := new(soham_common_req_keys.SendFilePathMediaType)
284+
err = json.Unmarshal(jsonData, body)
285+
if err != nil {
286+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
287+
ReqId: reqId,
288+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
289+
Message: interfaces.RequestError{Message: "Invalid message type"},
290+
})
291+
return
292+
}
293+
if w.WhatsappConnection == nil {
294+
wc, ok := (*w.WhatsappConnectionMap)[w.UUID]
295+
if !ok {
296+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
297+
ReqId: reqId,
298+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
299+
Message: interfaces.RequestError{Message: "Invalid from number"},
300+
})
301+
return
302+
}
303+
w.WhatsappConnection = wc
304+
}
305+
wc := w.WhatsappConnection
306+
if wc.ConnectionStatus != 1 {
307+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
308+
ReqId: reqId,
309+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
310+
Message: interfaces.RequestError{Message: "Whatsapp not connected"},
311+
})
312+
return
313+
}
314+
toNumber := make([]string, len(body.To))
315+
for i, v := range body.To {
316+
toNumber[i] = strconv.Itoa(v)
317+
}
318+
319+
resp := wc.SendMediaFileWithPath(toNumber, body.LocalMediaPath, filepath.Base(body.LocalMediaPath), body.ImageDesc)
320+
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
321+
ReqId: reqId,
322+
Type: soham_common_req_keys.REPSONSE_MESSAGE,
323+
Message: resp,
324+
})
325+
}
326+
135327
func (w *WebsocketConnectionObject) SendResponse(wcm *soham_common_req_keys.WhatsappClientMessage) bool {
136328
if !w.connected {
137329
return false
@@ -142,18 +334,20 @@ func (w *WebsocketConnectionObject) SendResponse(wcm *soham_common_req_keys.What
142334

143335
func (w *WebsocketConnectionObject) CheckStatusAndSendResponse() {
144336
time.Sleep(time.Second * 5)
145-
w.WhatsappConnectionStatus = soham_common_req_keys.NOT_LOGGED_IN
146-
if w.WhatsappConnectino == nil {
337+
if w.WhatsappConnectionStatus == "" {
338+
w.WhatsappConnectionStatus = soham_common_req_keys.NOT_LOGGED_IN
339+
}
340+
if w.WhatsappConnection == nil {
147341
wc, ok := (*w.WhatsappConnectionMap)[w.UUID]
148342
if !ok {
149343
w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
150344
Type: soham_common_req_keys.REPSONSE_MESSAGE,
151345
Message: interfaces.RequestError{Message: "Invalid from number"},
152346
})
153347
}
154-
w.WhatsappConnectino = wc
348+
w.WhatsappConnection = wc
155349
}
156-
wc := w.WhatsappConnectino
350+
wc := w.WhatsappConnection
157351
if wc.ConnectionStatus == 1 && w.WhatsappConnectionStatus != soham_common_req_keys.LOGGED_IN {
158352
if w.SendResponse(&soham_common_req_keys.WhatsappClientMessage{
159353
Type: soham_common_req_keys.STATUS_MESSAGE,

servers/soham/whatsapp-server/api/index.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ func AddApis(router fiber.Router) {
1010
soham_whatsapp_server_middleware.AllowOnlyValidTokenMiddleWare,
1111
soham_whatsapp_server_middleware.AllowOnlyValidLoggedInWhatsapp)
1212
router.Post("/send_message", SendMessageApi)
13+
router.Post("/send_base64_media", SendImageBase64ImageApi)
14+
router.Post("/send_web_media", SendWebMediaApi)
15+
router.Post("/send_local_media", SendWebMediaApi)
1316
}

0 commit comments

Comments
 (0)