diff --git a/docs/docs.go b/docs/docs.go index 22eddfb..92aa698 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,4 +1,5 @@ -// Package docs Code generated by swaggo/swag. DO NOT EDIT +// Package docs GENERATED BY SWAG; DO NOT EDIT +// This file was generated by swaggo/swag package docs import "github.com/swaggo/swag" @@ -31,7 +32,7 @@ const docTemplate = `{ "summary": "Show The Status of The Server", "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -53,7 +54,7 @@ const docTemplate = `{ "summary": "Generate Authentication Token", "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -75,10 +76,12 @@ const docTemplate = `{ "summary": "Get Joined Groups Information", "responses": { "200": { - "description": "OK" + "description": "" } } - }, + } + }, + "/group/join": { "post": { "security": [ { @@ -104,11 +107,13 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } - }, - "delete": { + } + }, + "/group/leave": { + "post": { "security": [ { "BearerAuth": [] @@ -126,14 +131,14 @@ const docTemplate = `{ { "type": "string", "description": "Group ID", - "name": "gid", + "name": "groupid", "in": "formData", "required": true } ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -172,7 +177,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -197,7 +202,7 @@ const docTemplate = `{ "summary": "Pair Phone for WhatsApp Multi-Device Login", "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -219,19 +224,19 @@ const docTemplate = `{ "summary": "Logout Device from WhatsApp Multi-Device", "responses": { "200": { - "description": "OK" + "description": "" } } } }, - "/message": { + "/message/delete": { "post": { "security": [ { "BearerAuth": [] } ], - "description": "Update Message to Spesific WhatsApp Personal ID or Group ID", + "description": "Delete Message to Spesific WhatsApp Personal ID or Group ID", "consumes": [ "multipart/form-data" ], @@ -241,7 +246,7 @@ const docTemplate = `{ "tags": [ "WhatsApp Message" ], - "summary": "Update Message", + "summary": "Delete Message", "parameters": [ { "type": "string", @@ -256,28 +261,23 @@ const docTemplate = `{ "name": "messageid", "in": "formData", "required": true - }, - { - "type": "string", - "description": "Text Message", - "name": "message", - "in": "formData", - "required": true } ], "responses": { "200": { - "description": "OK" + "description": "" } } - }, - "delete": { + } + }, + "/message/edit": { + "post": { "security": [ { "BearerAuth": [] } ], - "description": "Delete Message to Spesific WhatsApp Personal ID or Group ID", + "description": "Update Message to Spesific WhatsApp Personal ID or Group ID", "consumes": [ "multipart/form-data" ], @@ -287,7 +287,7 @@ const docTemplate = `{ "tags": [ "WhatsApp Message" ], - "summary": "Delete Message", + "summary": "Update Message", "parameters": [ { "type": "string", @@ -302,11 +302,18 @@ const docTemplate = `{ "name": "messageid", "in": "formData", "required": true + }, + { + "type": "string", + "description": "Text Message", + "name": "message", + "in": "formData", + "required": true } ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -337,7 +344,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -378,7 +385,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -426,7 +433,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -467,7 +474,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -522,7 +529,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -569,7 +576,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -617,7 +624,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -658,7 +665,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -699,7 +706,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -754,7 +761,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -782,8 +789,6 @@ var SwaggerInfo = &swag.Spec{ Description: "This is WhatsApp Multi-Device Implementation in Go REST API", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, - LeftDelim: "{{", - RightDelim: "}}", } func init() { diff --git a/docs/swagger.json b/docs/swagger.json index c85d8be..cf9ea31 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -23,7 +23,7 @@ "summary": "Show The Status of The Server", "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -45,7 +45,7 @@ "summary": "Generate Authentication Token", "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -67,10 +67,12 @@ "summary": "Get Joined Groups Information", "responses": { "200": { - "description": "OK" + "description": "" } } - }, + } + }, + "/group/join": { "post": { "security": [ { @@ -96,11 +98,13 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } - }, - "delete": { + } + }, + "/group/leave": { + "post": { "security": [ { "BearerAuth": [] @@ -118,14 +122,14 @@ { "type": "string", "description": "Group ID", - "name": "gid", + "name": "groupid", "in": "formData", "required": true } ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -164,7 +168,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -189,7 +193,7 @@ "summary": "Pair Phone for WhatsApp Multi-Device Login", "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -211,19 +215,19 @@ "summary": "Logout Device from WhatsApp Multi-Device", "responses": { "200": { - "description": "OK" + "description": "" } } } }, - "/message": { + "/message/delete": { "post": { "security": [ { "BearerAuth": [] } ], - "description": "Update Message to Spesific WhatsApp Personal ID or Group ID", + "description": "Delete Message to Spesific WhatsApp Personal ID or Group ID", "consumes": [ "multipart/form-data" ], @@ -233,7 +237,7 @@ "tags": [ "WhatsApp Message" ], - "summary": "Update Message", + "summary": "Delete Message", "parameters": [ { "type": "string", @@ -248,28 +252,23 @@ "name": "messageid", "in": "formData", "required": true - }, - { - "type": "string", - "description": "Text Message", - "name": "message", - "in": "formData", - "required": true } ], "responses": { "200": { - "description": "OK" + "description": "" } } - }, - "delete": { + } + }, + "/message/edit": { + "post": { "security": [ { "BearerAuth": [] } ], - "description": "Delete Message to Spesific WhatsApp Personal ID or Group ID", + "description": "Update Message to Spesific WhatsApp Personal ID or Group ID", "consumes": [ "multipart/form-data" ], @@ -279,7 +278,7 @@ "tags": [ "WhatsApp Message" ], - "summary": "Delete Message", + "summary": "Update Message", "parameters": [ { "type": "string", @@ -294,11 +293,18 @@ "name": "messageid", "in": "formData", "required": true + }, + { + "type": "string", + "description": "Text Message", + "name": "message", + "in": "formData", + "required": true } ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -329,7 +335,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -370,7 +376,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -418,7 +424,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -459,7 +465,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -514,7 +520,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -561,7 +567,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -609,7 +615,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -650,7 +656,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -691,7 +697,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } @@ -746,7 +752,7 @@ ], "responses": { "200": { - "description": "OK" + "description": "" } } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 413799b..fd8f891 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -14,7 +14,7 @@ paths: - application/json responses: "200": - description: OK + description: "" summary: Show The Status of The Server tags: - Root @@ -25,59 +25,61 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BasicAuth: [] summary: Generate Authentication Token tags: - Root /group: - delete: - description: Leaving Group By Group ID from WhatsApp - parameters: - - description: Group ID - in: formData - name: gid - required: true - type: string + get: + description: Get Joined Groups Information from WhatsApp produces: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] - summary: Leave Group By Group ID + summary: Get Joined Groups Information tags: - WhatsApp Group - get: - description: Get Joined Groups Information from WhatsApp + /group/join: + post: + description: Joining to Group From Invitation Link from WhatsApp + parameters: + - description: Group Invitation Link + in: formData + name: link + required: true + type: string produces: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] - summary: Get Joined Groups Information + summary: Join Group From Invitation Link tags: - WhatsApp Group + /group/leave: post: - description: Joining to Group From Invitation Link from WhatsApp + description: Leaving Group By Group ID from WhatsApp parameters: - - description: Group Invitation Link + - description: Group ID in: formData - name: link + name: groupid required: true type: string produces: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] - summary: Join Group From Invitation Link + summary: Leave Group By Group ID tags: - WhatsApp Group /login: @@ -99,7 +101,7 @@ paths: - text/html responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Generate QR Code for WhatsApp Multi-Device Login @@ -114,7 +116,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Pair Phone for WhatsApp Multi-Device Login @@ -127,14 +129,14 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Logout Device from WhatsApp Multi-Device tags: - WhatsApp Authentication - /message: - delete: + /message/delete: + post: consumes: - multipart/form-data description: Delete Message to Spesific WhatsApp Personal ID or Group ID @@ -153,12 +155,13 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Delete Message tags: - WhatsApp Message + /message/edit: post: consumes: - multipart/form-data @@ -183,7 +186,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Update Message @@ -202,7 +205,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Check If WhatsApp Personal ID is Registered @@ -228,7 +231,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Audio Message @@ -260,7 +263,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Contact Message @@ -287,7 +290,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Document Message @@ -323,7 +326,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Image Message @@ -353,7 +356,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Link Message @@ -385,7 +388,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Location Message @@ -412,7 +415,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Sticker Message @@ -438,7 +441,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Text Message @@ -474,7 +477,7 @@ paths: - application/json responses: "200": - description: OK + description: "" security: - BearerAuth: [] summary: Send Video Message diff --git a/internal/route.go b/internal/route.go index acdcd7a..a0d09a7 100644 --- a/internal/route.go +++ b/internal/route.go @@ -48,8 +48,8 @@ func Routes(e *echo.Echo) { e.GET(router.BaseURL+"/registered", ctlWhatsApp.Registered, middleware.JWTWithConfig(authJWTConfig)) e.GET(router.BaseURL+"/group", ctlWhatsApp.GetGroup, middleware.JWTWithConfig(authJWTConfig)) - e.POST(router.BaseURL+"/group", ctlWhatsApp.JoinGroup, middleware.JWTWithConfig(authJWTConfig)) - e.DELETE(router.BaseURL+"/group", ctlWhatsApp.LeaveGroup, middleware.JWTWithConfig(authJWTConfig)) + e.POST(router.BaseURL+"/group/join", ctlWhatsApp.JoinGroup, middleware.JWTWithConfig(authJWTConfig)) + e.POST(router.BaseURL+"/group/leave", ctlWhatsApp.LeaveGroup, middleware.JWTWithConfig(authJWTConfig)) e.POST(router.BaseURL+"/send/text", ctlWhatsApp.SendText, middleware.JWTWithConfig(authJWTConfig)) e.POST(router.BaseURL+"/send/location", ctlWhatsApp.SendLocation, middleware.JWTWithConfig(authJWTConfig)) @@ -61,6 +61,6 @@ func Routes(e *echo.Echo) { e.POST(router.BaseURL+"/send/video", ctlWhatsApp.SendVideo, middleware.JWTWithConfig(authJWTConfig)) e.POST(router.BaseURL+"/send/sticker", ctlWhatsApp.SendSticker, middleware.JWTWithConfig(authJWTConfig)) - e.POST(router.BaseURL+"/message", ctlWhatsApp.MessageUpdate, middleware.JWTWithConfig(authJWTConfig)) - e.DELETE(router.BaseURL+"/message", ctlWhatsApp.MessageDelete, middleware.JWTWithConfig(authJWTConfig)) + e.POST(router.BaseURL+"/message/edit", ctlWhatsApp.MessageEdit, middleware.JWTWithConfig(authJWTConfig)) + e.POST(router.BaseURL+"/message/delete", ctlWhatsApp.MessageDelete, middleware.JWTWithConfig(authJWTConfig)) } diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index 49230d2..d6a82c3 100644 --- a/internal/whatsapp/whatsapp.go +++ b/internal/whatsapp/whatsapp.go @@ -173,9 +173,9 @@ func Registered(c echo.Context) error { return router.ResponseInternalError(c, "Missing Query Value MSISDN") } - jidInfo := pkgWhatsApp.WhatsAppGetJID(jid, remoteJID) - if jidInfo.IsEmpty() { - return router.ResponseNotFound(c, "WhatsApp Personal ID is Not Registered") + err := pkgWhatsApp.WhatsAppCheckRegistered(jid, remoteJID) + if err != nil { + return router.ResponseInternalError(c, err.Error()) } return router.ResponseSuccess(c, "WhatsApp Personal ID is Registered") @@ -209,7 +209,7 @@ func GetGroup(c echo.Context) error { // @Param link formData string true "Group Invitation Link" // @Success 200 // @Security BearerAuth -// @Router /group [post] +// @Router /group/join [post] func JoinGroup(c echo.Context) error { var err error jid := jwtPayload(c).JID @@ -234,19 +234,19 @@ func JoinGroup(c echo.Context) error { // @Description Leaving Group By Group ID from WhatsApp // @Tags WhatsApp Group // @Produce json -// @Param gid formData string true "Group ID" +// @Param groupid formData string true "Group ID" // @Success 200 // @Security BearerAuth -// @Router /group [delete] +// @Router /group/leave [post] func LeaveGroup(c echo.Context) error { var err error jid := jwtPayload(c).JID var reqGroupLeave typWhatsApp.RequestGroupLeave - reqGroupLeave.GID = strings.TrimSpace(c.FormValue("gid")) + reqGroupLeave.GID = strings.TrimSpace(c.FormValue("groupid")) if len(reqGroupLeave.GID) == 0 { - return router.ResponseBadRequest(c, "Missing Form Value GID") + return router.ResponseBadRequest(c, "Missing Form Value Group ID") } err = pkgWhatsApp.WhatsAppGroupLeave(jid, reqGroupLeave.GID) @@ -597,7 +597,7 @@ func sendMedia(c echo.Context, mediaType string) error { return router.ResponseSuccessWithData(c, "Successfully Send Media Message", resSendMessage) } -// MessageUpdate +// MessageEdit // @Summary Update Message // @Description Update Message to Spesific WhatsApp Personal ID or Group ID // @Tags WhatsApp Message @@ -608,8 +608,8 @@ func sendMedia(c echo.Context, mediaType string) error { // @Param message formData string true "Text Message" // @Success 200 // @Security BearerAuth -// @Router /message [post] -func MessageUpdate(c echo.Context) error { +// @Router /message/edit [post] +func MessageEdit(c echo.Context) error { var err error jid := jwtPayload(c).JID @@ -623,7 +623,7 @@ func MessageUpdate(c echo.Context) error { } if len(reqMessageUpdate.MSGID) == 0 { - return router.ResponseBadRequest(c, "Missing Form Value MessageID") + return router.ResponseBadRequest(c, "Missing Form Value Message ID") } if len(reqMessageUpdate.Message) == 0 { @@ -631,7 +631,7 @@ func MessageUpdate(c echo.Context) error { } var resSendMessage typWhatsApp.ResponseSendMessage - resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppMessageUpdate(c.Request().Context(), jid, reqMessageUpdate.RJID, reqMessageUpdate.MSGID, reqMessageUpdate.Message) + resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppMessageEdit(c.Request().Context(), jid, reqMessageUpdate.RJID, reqMessageUpdate.MSGID, reqMessageUpdate.Message) if err != nil { return router.ResponseInternalError(c, err.Error()) } @@ -649,7 +649,7 @@ func MessageUpdate(c echo.Context) error { // @Param messageid formData string true "Message ID" // @Success 200 // @Security BearerAuth -// @Router /message [delete] +// @Router /message/delete [post] func MessageDelete(c echo.Context) error { var err error jid := jwtPayload(c).JID @@ -663,7 +663,7 @@ func MessageDelete(c echo.Context) error { } if len(reqMessageUpdate.MSGID) == 0 { - return router.ResponseBadRequest(c, "Missing Form Value MessageID") + return router.ResponseBadRequest(c, "Missing Form Value Message ID") } err = pkgWhatsApp.WhatsAppMessageDelete(c.Request().Context(), jid, reqMessageUpdate.RJID, reqMessageUpdate.MSGID) diff --git a/pkg/whatsapp/whatsapp.go b/pkg/whatsapp/whatsapp.go index 5c76cd5..b0270f3 100644 --- a/pkg/whatsapp/whatsapp.go +++ b/pkg/whatsapp/whatsapp.go @@ -346,6 +346,25 @@ func WhatsAppGetJID(jid string, id string) types.JID { return types.EmptyJID } +func WhatsAppCheckJID(jid string, id string) (types.JID, error) { + if WhatsAppClient[jid] != nil { + // Compose New Remote JID + remoteJID := WhatsAppComposeJID(id) + if remoteJID.Server != types.GroupServer { + // Validate JID if Remote JID is not Group JID + if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { + return types.EmptyJID, errors.New("WhatsApp Personal ID is Not Registered") + } + } + + // Return Remote ID Information + return remoteJID, nil + } + + // Return Empty ID Information + return types.EmptyJID, nil +} + func WhatsAppComposeJID(id string) types.JID { // Decompose WhatsApp ID First Before Recomposing id = WhatsAppDecomposeJID(id) @@ -408,6 +427,32 @@ func WhatsAppComposeStatus(jid string, rjid types.JID, isComposing bool, isAudio _ = WhatsAppClient[jid].SendChatPresence(rjid, typeCompose, typeComposeMedia) } +func WhatsAppCheckRegistered(jid string, id string) error { + if WhatsAppClient[jid] != nil { + var err error + + // Make Sure WhatsApp Client is OK + err = WhatsAppIsClientOK(jid) + if err != nil { + return err + } + + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, id) + if err != nil { + return err + } + + // Make Sure WhatsApp ID is Not Empty or It is Not Group ID + if remoteJID.IsEmpty() || remoteJID.Server == types.GroupServer { + return errors.New("WhatsApp Personal ID is Not Registered") + } + } + + // Return Error WhatsApp Client is not Valid + return errors.New("WhatsApp Client is not Valid") +} + func WhatsAppSendText(ctx context.Context, jid string, rjid string, message string) (string, error) { if WhatsAppClient[jid] != nil { var err error @@ -418,13 +463,10 @@ func WhatsAppSendText(ctx context.Context, jid string, rjid string, message stri return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -466,13 +508,10 @@ func WhatsAppSendLocation(ctx context.Context, jid string, rjid string, latitude return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -517,13 +556,10 @@ func WhatsAppSendDocument(ctx context.Context, jid string, rjid string, fileByte return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -581,13 +617,10 @@ func WhatsAppSendImage(ctx context.Context, jid string, rjid string, imageBytes return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -722,13 +755,10 @@ func WhatsAppSendAudio(ctx context.Context, jid string, rjid string, audioBytes return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -780,13 +810,10 @@ func WhatsAppSendVideo(ctx context.Context, jid string, rjid string, videoBytes return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -844,13 +871,10 @@ func WhatsAppSendContact(ctx context.Context, jid string, rjid string, contactNa return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -898,13 +922,10 @@ func WhatsAppSendLink(ctx context.Context, jid string, rjid string, linkCaption return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -983,13 +1004,10 @@ func WhatsAppSendSticker(ctx context.Context, jid string, rjid string, stickerBy return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -1050,7 +1068,7 @@ func WhatsAppSendSticker(ctx context.Context, jid string, rjid string, stickerBy return "", errors.New("WhatsApp Client is not Valid") } -func WhatsAppMessageUpdate(ctx context.Context, jid string, rjid string, msgid string, message string) (string, error) { +func WhatsAppMessageEdit(ctx context.Context, jid string, rjid string, msgid string, message string) (string, error) { if WhatsAppClient[jid] != nil { var err error @@ -1060,13 +1078,10 @@ func WhatsAppMessageUpdate(ctx context.Context, jid string, rjid string, msgid s return "", err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return "", errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err } // Set Chat Presence @@ -1105,13 +1120,10 @@ func WhatsAppMessageDelete(ctx context.Context, jid string, rjid string, msgid s return err } - // Compose New Remote JID - remoteJID := WhatsAppComposeJID(rjid) - if remoteJID.Server != types.GroupServer { - // Validate JID if Remote JID is not Gorup JID - if WhatsAppGetJID(jid, remoteJID.String()).IsEmpty() { - return errors.New("WhatsApp Personal ID is Not Registered") - } + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return err } // Set Chat Presence @@ -1189,7 +1201,7 @@ func WhatsAppGroupJoin(jid string, link string) (string, error) { return "", errors.New("WhatsApp Client is not Valid") } -func WhatsAppGroupLeave(jid string, gid string) error { +func WhatsAppGroupLeave(jid string, gjid string) error { if WhatsAppClient[jid] != nil { var err error @@ -1199,8 +1211,19 @@ func WhatsAppGroupLeave(jid string, gid string) error { return err } + // Make Sure WhatsApp ID is Registered + groupJID, err := WhatsAppCheckJID(jid, gjid) + if err != nil { + return err + } + + // Make Sure WhatsApp ID is Group Server + if groupJID.Server != types.GroupServer { + return errors.New("WhatsApp Group ID is Not Group Server") + } + // Leave Group By Group ID - return WhatsAppClient[jid].LeaveGroup(WhatsAppComposeJID(gid)) + return WhatsAppClient[jid].LeaveGroup(groupJID) } // Return Error WhatsApp Client is not Valid