From 0e01602981fb96cd018c9d9c9ba86fe7b3da4ac1 Mon Sep 17 00:00:00 2001 From: Dimas Restu H Date: Wed, 18 May 2022 11:23:33 +0700 Subject: [PATCH] added view once parameter to send image or video --- docs/docs.go | 14 ++++++++++++++ docs/swagger.json | 14 ++++++++++++++ docs/swagger.yaml | 10 ++++++++++ internal/whatsapp/types/request.go | 5 +++-- internal/whatsapp/whatsapp.go | 25 +++++++++++++++++++++++-- pkg/whatsapp/whatsapp.go | 6 ++++-- 6 files changed, 68 insertions(+), 6 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 611eecc..03639f4 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -289,6 +289,13 @@ const docTemplate = `{ "name": "image", "in": "formData", "required": true + }, + { + "type": "boolean", + "default": false, + "description": "Is View Once", + "name": "viewonce", + "in": "formData" } ], "responses": { @@ -473,6 +480,13 @@ const docTemplate = `{ "name": "video", "in": "formData", "required": true + }, + { + "type": "boolean", + "default": false, + "description": "Is View Once", + "name": "viewonce", + "in": "formData" } ], "responses": { diff --git a/docs/swagger.json b/docs/swagger.json index 0b5793c..f821794 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -284,6 +284,13 @@ "name": "image", "in": "formData", "required": true + }, + { + "type": "boolean", + "default": false, + "description": "Is View Once", + "name": "viewonce", + "in": "formData" } ], "responses": { @@ -468,6 +475,13 @@ "name": "video", "in": "formData", "required": true + }, + { + "type": "boolean", + "default": false, + "description": "Is View Once", + "name": "viewonce", + "in": "formData" } ], "responses": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 621147c..fec9e54 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -174,6 +174,11 @@ paths: name: image required: true type: file + - default: false + description: Is View Once + in: formData + name: viewonce + type: boolean produces: - application/json responses: @@ -292,6 +297,11 @@ paths: name: video required: true type: file + - default: false + description: Is View Once + in: formData + name: viewonce + type: boolean produces: - application/json responses: diff --git a/internal/whatsapp/types/request.go b/internal/whatsapp/types/request.go index 7586965..0b1004f 100644 --- a/internal/whatsapp/types/request.go +++ b/internal/whatsapp/types/request.go @@ -5,8 +5,9 @@ type RequestLogin struct { } type RequestSendMessage struct { - RJID string - Message string + RJID string + Message string + ViewOnce bool } type RequestSendLocation struct { diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index 1b65c0f..edd633a 100644 --- a/internal/whatsapp/whatsapp.go +++ b/internal/whatsapp/whatsapp.go @@ -306,6 +306,7 @@ func SendDocument(c echo.Context) error { // @Param msisdn formData string true "Destination Phone Number" // @Param caption formData string true "Caption Image Message" // @Param image formData file true "Image File" +// @Param viewonce formData bool false "Is View Once" default(false) // @Success 200 // @Security BearerAuth // @Router /api/v1/whatsapp/send/image [post] @@ -337,6 +338,7 @@ func SendAudio(c echo.Context) error { // @Param msisdn formData string true "Destination Phone Number" // @Param caption formData string true "Caption Video Message" // @Param video formData file true "Video File" +// @Param viewonce formData bool false "Is View Once" default(false) // @Success 200 // @Security BearerAuth // @Router /api/v1/whatsapp/send/video [post] @@ -372,6 +374,25 @@ func sendMedia(c echo.Context, mediaType string) error { reqSendMessage.Message = strings.TrimSpace(c.FormValue("caption")) } + // Check if Media Type is "image" or "video" + // Then Parse ViewOnce Parameter + if mediaType == "image" || mediaType == "video" { + isViewOnce := strings.TrimSpace(c.FormValue("viewonce")) + + if len(isViewOnce) == 0 { + // If ViewOnce Parameter Doesn't Exist or Empty String + // Then Set it Default to False + reqSendMessage.ViewOnce = false + } else { + // If ViewOnce Parameter is not Empty + // Then Parse it to Bool + reqSendMessage.ViewOnce, err = strconv.ParseBool(isViewOnce) + if err != nil { + return router.ResponseBadRequest(c, err.Error()) + } + } + } + // Don't Forget to Close The File Stream defer fileStream.Close() @@ -433,13 +454,13 @@ func sendMedia(c echo.Context, mediaType string) error { resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendDocument(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message) case "image": - resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendImage(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message) + resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendImage(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message, reqSendMessage.ViewOnce) case "audio": resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendAudio(jid, reqSendMessage.RJID, fileBytes, fileType) case "video": - resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendVideo(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message) + resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendVideo(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message, reqSendMessage.ViewOnce) } // Return Internal Server Error diff --git a/pkg/whatsapp/whatsapp.go b/pkg/whatsapp/whatsapp.go index d3099da..558caab 100644 --- a/pkg/whatsapp/whatsapp.go +++ b/pkg/whatsapp/whatsapp.go @@ -406,7 +406,7 @@ func WhatsAppSendDocument(jid string, rjid string, fileBytes []byte, fileType st return "", errors.New("WhatsApp Client is not Valid") } -func WhatsAppSendImage(jid string, rjid string, imageBytes []byte, imageType string, imageCaption string) (string, error) { +func WhatsAppSendImage(jid string, rjid string, imageBytes []byte, imageType string, imageCaption string, isViewOnce bool) (string, error) { if WhatsAppClient[jid] != nil { var err error @@ -441,6 +441,7 @@ func WhatsAppSendImage(jid string, rjid string, imageBytes []byte, imageType str FileSha256: imageUploaded.FileSHA256, FileEncSha256: imageUploaded.FileEncSHA256, MediaKey: imageUploaded.MediaKey, + ViewOnce: proto.Bool(isViewOnce), }, } @@ -507,7 +508,7 @@ func WhatsAppSendAudio(jid string, rjid string, audioBytes []byte, audioType str return "", errors.New("WhatsApp Client is not Valid") } -func WhatsAppSendVideo(jid string, rjid string, videoBytes []byte, videoType string, videoCaption string) (string, error) { +func WhatsAppSendVideo(jid string, rjid string, videoBytes []byte, videoType string, videoCaption string, isViewOnce bool) (string, error) { if WhatsAppClient[jid] != nil { var err error @@ -542,6 +543,7 @@ func WhatsAppSendVideo(jid string, rjid string, videoBytes []byte, videoType str FileSha256: videoUploaded.FileSHA256, FileEncSha256: videoUploaded.FileEncSHA256, MediaKey: videoUploaded.MediaKey, + ViewOnce: proto.Bool(isViewOnce), }, }