Browse Source

update and improvements:

- fix whatsapp jid registered checking avoid error when group jid is entered
- optimize message send, update, delete that required jid to be checked first
pull/61/head
Dimas Restu H 2 years ago
parent
commit
16a14bfe79
  1. 87
      docs/docs.go
  2. 82
      docs/swagger.json
  3. 79
      docs/swagger.yaml
  4. 8
      internal/route.go
  5. 30
      internal/whatsapp/whatsapp.go
  6. 183
      pkg/whatsapp/whatsapp.go

87
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 package docs
import "github.com/swaggo/swag" import "github.com/swaggo/swag"
@ -31,7 +32,7 @@ const docTemplate = `{
"summary": "Show The Status of The Server", "summary": "Show The Status of The Server",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -53,7 +54,7 @@ const docTemplate = `{
"summary": "Generate Authentication Token", "summary": "Generate Authentication Token",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -75,10 +76,12 @@ const docTemplate = `{
"summary": "Get Joined Groups Information", "summary": "Get Joined Groups Information",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
},
}
},
"/group/join": {
"post": { "post": {
"security": [ "security": [
{ {
@ -104,11 +107,13 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
},
"delete": {
}
},
"/group/leave": {
"post": {
"security": [ "security": [
{ {
"BearerAuth": [] "BearerAuth": []
@ -126,14 +131,14 @@ const docTemplate = `{
{ {
"type": "string", "type": "string",
"description": "Group ID", "description": "Group ID",
"name": "gid",
"name": "groupid",
"in": "formData", "in": "formData",
"required": true "required": true
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -172,7 +177,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -197,7 +202,7 @@ const docTemplate = `{
"summary": "Pair Phone for WhatsApp Multi-Device Login", "summary": "Pair Phone for WhatsApp Multi-Device Login",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -219,19 +224,19 @@ const docTemplate = `{
"summary": "Logout Device from WhatsApp Multi-Device", "summary": "Logout Device from WhatsApp Multi-Device",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
}, },
"/message": {
"/message/delete": {
"post": { "post": {
"security": [ "security": [
{ {
"BearerAuth": [] "BearerAuth": []
} }
], ],
"description": "Update Message to Spesific WhatsApp Personal ID or Group ID",
"description": "Delete Message to Spesific WhatsApp Personal ID or Group ID",
"consumes": [ "consumes": [
"multipart/form-data" "multipart/form-data"
], ],
@ -241,7 +246,7 @@ const docTemplate = `{
"tags": [ "tags": [
"WhatsApp Message" "WhatsApp Message"
], ],
"summary": "Update Message",
"summary": "Delete Message",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -256,28 +261,23 @@ const docTemplate = `{
"name": "messageid", "name": "messageid",
"in": "formData", "in": "formData",
"required": true "required": true
},
{
"type": "string",
"description": "Text Message",
"name": "message",
"in": "formData",
"required": true
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
},
"delete": {
}
},
"/message/edit": {
"post": {
"security": [ "security": [
{ {
"BearerAuth": [] "BearerAuth": []
} }
], ],
"description": "Delete Message to Spesific WhatsApp Personal ID or Group ID",
"description": "Update Message to Spesific WhatsApp Personal ID or Group ID",
"consumes": [ "consumes": [
"multipart/form-data" "multipart/form-data"
], ],
@ -287,7 +287,7 @@ const docTemplate = `{
"tags": [ "tags": [
"WhatsApp Message" "WhatsApp Message"
], ],
"summary": "Delete Message",
"summary": "Update Message",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -302,11 +302,18 @@ const docTemplate = `{
"name": "messageid", "name": "messageid",
"in": "formData", "in": "formData",
"required": true "required": true
},
{
"type": "string",
"description": "Text Message",
"name": "message",
"in": "formData",
"required": true
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -337,7 +344,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -378,7 +385,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -426,7 +433,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -467,7 +474,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -522,7 +529,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -569,7 +576,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -617,7 +624,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -658,7 +665,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -699,7 +706,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -754,7 +761,7 @@ const docTemplate = `{
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -782,8 +789,6 @@ var SwaggerInfo = &swag.Spec{
Description: "This is WhatsApp Multi-Device Implementation in Go REST API", Description: "This is WhatsApp Multi-Device Implementation in Go REST API",
InfoInstanceName: "swagger", InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate, SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
} }
func init() { func init() {

82
docs/swagger.json

@ -23,7 +23,7 @@
"summary": "Show The Status of The Server", "summary": "Show The Status of The Server",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -45,7 +45,7 @@
"summary": "Generate Authentication Token", "summary": "Generate Authentication Token",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -67,10 +67,12 @@
"summary": "Get Joined Groups Information", "summary": "Get Joined Groups Information",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
},
}
},
"/group/join": {
"post": { "post": {
"security": [ "security": [
{ {
@ -96,11 +98,13 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
},
"delete": {
}
},
"/group/leave": {
"post": {
"security": [ "security": [
{ {
"BearerAuth": [] "BearerAuth": []
@ -118,14 +122,14 @@
{ {
"type": "string", "type": "string",
"description": "Group ID", "description": "Group ID",
"name": "gid",
"name": "groupid",
"in": "formData", "in": "formData",
"required": true "required": true
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -164,7 +168,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -189,7 +193,7 @@
"summary": "Pair Phone for WhatsApp Multi-Device Login", "summary": "Pair Phone for WhatsApp Multi-Device Login",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -211,19 +215,19 @@
"summary": "Logout Device from WhatsApp Multi-Device", "summary": "Logout Device from WhatsApp Multi-Device",
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
}, },
"/message": {
"/message/delete": {
"post": { "post": {
"security": [ "security": [
{ {
"BearerAuth": [] "BearerAuth": []
} }
], ],
"description": "Update Message to Spesific WhatsApp Personal ID or Group ID",
"description": "Delete Message to Spesific WhatsApp Personal ID or Group ID",
"consumes": [ "consumes": [
"multipart/form-data" "multipart/form-data"
], ],
@ -233,7 +237,7 @@
"tags": [ "tags": [
"WhatsApp Message" "WhatsApp Message"
], ],
"summary": "Update Message",
"summary": "Delete Message",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -248,28 +252,23 @@
"name": "messageid", "name": "messageid",
"in": "formData", "in": "formData",
"required": true "required": true
},
{
"type": "string",
"description": "Text Message",
"name": "message",
"in": "formData",
"required": true
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
},
"delete": {
}
},
"/message/edit": {
"post": {
"security": [ "security": [
{ {
"BearerAuth": [] "BearerAuth": []
} }
], ],
"description": "Delete Message to Spesific WhatsApp Personal ID or Group ID",
"description": "Update Message to Spesific WhatsApp Personal ID or Group ID",
"consumes": [ "consumes": [
"multipart/form-data" "multipart/form-data"
], ],
@ -279,7 +278,7 @@
"tags": [ "tags": [
"WhatsApp Message" "WhatsApp Message"
], ],
"summary": "Delete Message",
"summary": "Update Message",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -294,11 +293,18 @@
"name": "messageid", "name": "messageid",
"in": "formData", "in": "formData",
"required": true "required": true
},
{
"type": "string",
"description": "Text Message",
"name": "message",
"in": "formData",
"required": true
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -329,7 +335,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -370,7 +376,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -418,7 +424,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -459,7 +465,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -514,7 +520,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -561,7 +567,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -609,7 +615,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -650,7 +656,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -691,7 +697,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }
@ -746,7 +752,7 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "OK"
"description": ""
} }
} }
} }

79
docs/swagger.yaml

@ -14,7 +14,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
summary: Show The Status of The Server summary: Show The Status of The Server
tags: tags:
- Root - Root
@ -25,59 +25,61 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BasicAuth: [] - BasicAuth: []
summary: Generate Authentication Token summary: Generate Authentication Token
tags: tags:
- Root - Root
/group: /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: produces:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Leave Group By Group ID
summary: Get Joined Groups Information
tags: tags:
- WhatsApp Group - 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: produces:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Get Joined Groups Information
summary: Join Group From Invitation Link
tags: tags:
- WhatsApp Group - WhatsApp Group
/group/leave:
post: post:
description: Joining to Group From Invitation Link from WhatsApp
description: Leaving Group By Group ID from WhatsApp
parameters: parameters:
- description: Group Invitation Link
- description: Group ID
in: formData in: formData
name: link
name: groupid
required: true required: true
type: string type: string
produces: produces:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Join Group From Invitation Link
summary: Leave Group By Group ID
tags: tags:
- WhatsApp Group - WhatsApp Group
/login: /login:
@ -99,7 +101,7 @@ paths:
- text/html - text/html
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Generate QR Code for WhatsApp Multi-Device Login summary: Generate QR Code for WhatsApp Multi-Device Login
@ -114,7 +116,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Pair Phone for WhatsApp Multi-Device Login summary: Pair Phone for WhatsApp Multi-Device Login
@ -127,14 +129,14 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Logout Device from WhatsApp Multi-Device summary: Logout Device from WhatsApp Multi-Device
tags: tags:
- WhatsApp Authentication - WhatsApp Authentication
/message:
delete:
/message/delete:
post:
consumes: consumes:
- multipart/form-data - multipart/form-data
description: Delete Message to Spesific WhatsApp Personal ID or Group ID description: Delete Message to Spesific WhatsApp Personal ID or Group ID
@ -153,12 +155,13 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Delete Message summary: Delete Message
tags: tags:
- WhatsApp Message - WhatsApp Message
/message/edit:
post: post:
consumes: consumes:
- multipart/form-data - multipart/form-data
@ -183,7 +186,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Update Message summary: Update Message
@ -202,7 +205,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Check If WhatsApp Personal ID is Registered summary: Check If WhatsApp Personal ID is Registered
@ -228,7 +231,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Audio Message summary: Send Audio Message
@ -260,7 +263,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Contact Message summary: Send Contact Message
@ -287,7 +290,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Document Message summary: Send Document Message
@ -323,7 +326,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Image Message summary: Send Image Message
@ -353,7 +356,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Link Message summary: Send Link Message
@ -385,7 +388,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Location Message summary: Send Location Message
@ -412,7 +415,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Sticker Message summary: Send Sticker Message
@ -438,7 +441,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Text Message summary: Send Text Message
@ -474,7 +477,7 @@ paths:
- application/json - application/json
responses: responses:
"200": "200":
description: OK
description: ""
security: security:
- BearerAuth: [] - BearerAuth: []
summary: Send Video Message summary: Send Video Message

8
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+"/registered", ctlWhatsApp.Registered, middleware.JWTWithConfig(authJWTConfig))
e.GET(router.BaseURL+"/group", ctlWhatsApp.GetGroup, 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/text", ctlWhatsApp.SendText, middleware.JWTWithConfig(authJWTConfig))
e.POST(router.BaseURL+"/send/location", ctlWhatsApp.SendLocation, 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/video", ctlWhatsApp.SendVideo, middleware.JWTWithConfig(authJWTConfig))
e.POST(router.BaseURL+"/send/sticker", ctlWhatsApp.SendSticker, 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))
} }

30
internal/whatsapp/whatsapp.go

@ -173,9 +173,9 @@ func Registered(c echo.Context) error {
return router.ResponseInternalError(c, "Missing Query Value MSISDN") 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") 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" // @Param link formData string true "Group Invitation Link"
// @Success 200 // @Success 200
// @Security BearerAuth // @Security BearerAuth
// @Router /group [post]
// @Router /group/join [post]
func JoinGroup(c echo.Context) error { func JoinGroup(c echo.Context) error {
var err error var err error
jid := jwtPayload(c).JID jid := jwtPayload(c).JID
@ -234,19 +234,19 @@ func JoinGroup(c echo.Context) error {
// @Description Leaving Group By Group ID from WhatsApp // @Description Leaving Group By Group ID from WhatsApp
// @Tags WhatsApp Group // @Tags WhatsApp Group
// @Produce json // @Produce json
// @Param gid formData string true "Group ID"
// @Param groupid formData string true "Group ID"
// @Success 200 // @Success 200
// @Security BearerAuth // @Security BearerAuth
// @Router /group [delete]
// @Router /group/leave [post]
func LeaveGroup(c echo.Context) error { func LeaveGroup(c echo.Context) error {
var err error var err error
jid := jwtPayload(c).JID jid := jwtPayload(c).JID
var reqGroupLeave typWhatsApp.RequestGroupLeave var reqGroupLeave typWhatsApp.RequestGroupLeave
reqGroupLeave.GID = strings.TrimSpace(c.FormValue("gid"))
reqGroupLeave.GID = strings.TrimSpace(c.FormValue("groupid"))
if len(reqGroupLeave.GID) == 0 { 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) 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) return router.ResponseSuccessWithData(c, "Successfully Send Media Message", resSendMessage)
} }
// MessageUpdate
// MessageEdit
// @Summary Update Message // @Summary Update Message
// @Description Update Message to Spesific WhatsApp Personal ID or Group ID // @Description Update Message to Spesific WhatsApp Personal ID or Group ID
// @Tags WhatsApp Message // @Tags WhatsApp Message
@ -608,8 +608,8 @@ func sendMedia(c echo.Context, mediaType string) error {
// @Param message formData string true "Text Message" // @Param message formData string true "Text Message"
// @Success 200 // @Success 200
// @Security BearerAuth // @Security BearerAuth
// @Router /message [post]
func MessageUpdate(c echo.Context) error {
// @Router /message/edit [post]
func MessageEdit(c echo.Context) error {
var err error var err error
jid := jwtPayload(c).JID jid := jwtPayload(c).JID
@ -623,7 +623,7 @@ func MessageUpdate(c echo.Context) error {
} }
if len(reqMessageUpdate.MSGID) == 0 { 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 { if len(reqMessageUpdate.Message) == 0 {
@ -631,7 +631,7 @@ func MessageUpdate(c echo.Context) error {
} }
var resSendMessage typWhatsApp.ResponseSendMessage 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 { if err != nil {
return router.ResponseInternalError(c, err.Error()) return router.ResponseInternalError(c, err.Error())
} }
@ -649,7 +649,7 @@ func MessageUpdate(c echo.Context) error {
// @Param messageid formData string true "Message ID" // @Param messageid formData string true "Message ID"
// @Success 200 // @Success 200
// @Security BearerAuth // @Security BearerAuth
// @Router /message [delete]
// @Router /message/delete [post]
func MessageDelete(c echo.Context) error { func MessageDelete(c echo.Context) error {
var err error var err error
jid := jwtPayload(c).JID jid := jwtPayload(c).JID
@ -663,7 +663,7 @@ func MessageDelete(c echo.Context) error {
} }
if len(reqMessageUpdate.MSGID) == 0 { 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) err = pkgWhatsApp.WhatsAppMessageDelete(c.Request().Context(), jid, reqMessageUpdate.RJID, reqMessageUpdate.MSGID)

183
pkg/whatsapp/whatsapp.go

@ -346,6 +346,25 @@ func WhatsAppGetJID(jid string, id string) types.JID {
return types.EmptyJID 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 { func WhatsAppComposeJID(id string) types.JID {
// Decompose WhatsApp ID First Before Recomposing // Decompose WhatsApp ID First Before Recomposing
id = WhatsAppDecomposeJID(id) id = WhatsAppDecomposeJID(id)
@ -408,6 +427,32 @@ func WhatsAppComposeStatus(jid string, rjid types.JID, isComposing bool, isAudio
_ = WhatsAppClient[jid].SendChatPresence(rjid, typeCompose, typeComposeMedia) _ = 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) { func WhatsAppSendText(ctx context.Context, jid string, rjid string, message string) (string, error) {
if WhatsAppClient[jid] != nil { if WhatsAppClient[jid] != nil {
var err error var err error
@ -418,13 +463,10 @@ func WhatsAppSendText(ctx context.Context, jid string, rjid string, message stri
return "", err 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 // Set Chat Presence
@ -466,13 +508,10 @@ func WhatsAppSendLocation(ctx context.Context, jid string, rjid string, latitude
return "", err 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 // Set Chat Presence
@ -517,13 +556,10 @@ func WhatsAppSendDocument(ctx context.Context, jid string, rjid string, fileByte
return "", err 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 // Set Chat Presence
@ -581,13 +617,10 @@ func WhatsAppSendImage(ctx context.Context, jid string, rjid string, imageBytes
return "", err 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 // Set Chat Presence
@ -722,13 +755,10 @@ func WhatsAppSendAudio(ctx context.Context, jid string, rjid string, audioBytes
return "", err 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 // Set Chat Presence
@ -780,13 +810,10 @@ func WhatsAppSendVideo(ctx context.Context, jid string, rjid string, videoBytes
return "", err 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 // Set Chat Presence
@ -844,13 +871,10 @@ func WhatsAppSendContact(ctx context.Context, jid string, rjid string, contactNa
return "", err 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 // Set Chat Presence
@ -898,13 +922,10 @@ func WhatsAppSendLink(ctx context.Context, jid string, rjid string, linkCaption
return "", err 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 // Set Chat Presence
@ -983,13 +1004,10 @@ func WhatsAppSendSticker(ctx context.Context, jid string, rjid string, stickerBy
return "", err 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 // 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") 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 { if WhatsAppClient[jid] != nil {
var err error var err error
@ -1060,13 +1078,10 @@ func WhatsAppMessageUpdate(ctx context.Context, jid string, rjid string, msgid s
return "", err 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 // Set Chat Presence
@ -1105,13 +1120,10 @@ func WhatsAppMessageDelete(ctx context.Context, jid string, rjid string, msgid s
return err 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 // Set Chat Presence
@ -1189,7 +1201,7 @@ func WhatsAppGroupJoin(jid string, link string) (string, error) {
return "", errors.New("WhatsApp Client is not Valid") 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 { if WhatsAppClient[jid] != nil {
var err error var err error
@ -1199,8 +1211,19 @@ func WhatsAppGroupLeave(jid string, gid string) error {
return err 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 // Leave Group By Group ID
return WhatsAppClient[jid].LeaveGroup(WhatsAppComposeJID(gid))
return WhatsAppClient[jid].LeaveGroup(groupJID)
} }
// Return Error WhatsApp Client is not Valid // Return Error WhatsApp Client is not Valid

Loading…
Cancel
Save