From 2245e8b235463a3ee738b2a9e1245869ae506c16 Mon Sep 17 00:00:00 2001 From: Dimas Restu H Date: Mon, 18 Mar 2024 23:04:15 +0700 Subject: [PATCH] add feature send poll --- docs/docs.go | 55 ++++++++++++++++++++++++++ docs/swagger.json | 55 ++++++++++++++++++++++++++ docs/swagger.yaml | 36 +++++++++++++++++ internal/route.go | 1 + internal/whatsapp/types/request.go | 8 ++-- internal/whatsapp/whatsapp.go | 62 ++++++++++++++++++++++++++++++ pkg/whatsapp/whatsapp.go | 6 +-- 7 files changed, 216 insertions(+), 7 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 92aa698..d5d3296 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -629,6 +629,61 @@ const docTemplate = `{ } } }, + "/send/poll": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Send Poll to Spesific WhatsApp Personal ID or Group ID", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "WhatsApp Send Message" + ], + "summary": "Send Poll", + "parameters": [ + { + "type": "string", + "description": "Destination WhatsApp Personal ID or Group ID", + "name": "msisdn", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "Poll Question", + "name": "question", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "Poll Options (Comma Seperated for New Options)", + "name": "options", + "in": "formData", + "required": true + }, + { + "type": "boolean", + "default": false, + "description": "Is Multiple Answer", + "name": "multianswer", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "" + } + } + } + }, "/send/sticker": { "post": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index cf9ea31..1673ae9 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -620,6 +620,61 @@ } } }, + "/send/poll": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Send Poll to Spesific WhatsApp Personal ID or Group ID", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "WhatsApp Send Message" + ], + "summary": "Send Poll", + "parameters": [ + { + "type": "string", + "description": "Destination WhatsApp Personal ID or Group ID", + "name": "msisdn", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "Poll Question", + "name": "question", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "Poll Options (Comma Seperated for New Options)", + "name": "options", + "in": "formData", + "required": true + }, + { + "type": "boolean", + "default": false, + "description": "Is Multiple Answer", + "name": "multianswer", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "" + } + } + } + }, "/send/sticker": { "post": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index fd8f891..886d5fa 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -394,6 +394,42 @@ paths: summary: Send Location Message tags: - WhatsApp Send Message + /send/poll: + post: + consumes: + - multipart/form-data + description: Send Poll to Spesific WhatsApp Personal ID or Group ID + parameters: + - description: Destination WhatsApp Personal ID or Group ID + in: formData + name: msisdn + required: true + type: string + - description: Poll Question + in: formData + name: question + required: true + type: string + - description: Poll Options (Comma Seperated for New Options) + in: formData + name: options + required: true + type: string + - default: false + description: Is Multiple Answer + in: formData + name: multianswer + type: boolean + produces: + - application/json + responses: + "200": + description: "" + security: + - BearerAuth: [] + summary: Send Poll + tags: + - WhatsApp Send Message /send/sticker: post: consumes: diff --git a/internal/route.go b/internal/route.go index a0d09a7..996bc9b 100644 --- a/internal/route.go +++ b/internal/route.go @@ -60,6 +60,7 @@ func Routes(e *echo.Echo) { e.POST(router.BaseURL+"/send/audio", ctlWhatsApp.SendAudio, 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/poll", ctlWhatsApp.SendPoll, 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/types/request.go b/internal/whatsapp/types/request.go index a20cec7..5bf6218 100644 --- a/internal/whatsapp/types/request.go +++ b/internal/whatsapp/types/request.go @@ -29,10 +29,10 @@ type RequestSendLink struct { } type RequestSendPoll struct { - RJID string - Question string - Options []string - MultipleAnswer bool + RJID string + Question string + Options string + MultiAnswer bool } type RequestMessage struct { diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index d6a82c3..6621f1e 100644 --- a/internal/whatsapp/whatsapp.go +++ b/internal/whatsapp/whatsapp.go @@ -597,6 +597,68 @@ func sendMedia(c echo.Context, mediaType string) error { return router.ResponseSuccessWithData(c, "Successfully Send Media Message", resSendMessage) } +// SendPoll +// @Summary Send Poll +// @Description Send Poll to Spesific WhatsApp Personal ID or Group ID +// @Tags WhatsApp Send Message +// @Accept multipart/form-data +// @Produce json +// @Param msisdn formData string true "Destination WhatsApp Personal ID or Group ID" +// @Param question formData string true "Poll Question" +// @Param options formData string true "Poll Options (Comma Seperated for New Options)" +// @Param multianswer formData bool false "Is Multiple Answer" default(false) +// @Success 200 +// @Security BearerAuth +// @Router /send/poll [post] +func SendPoll(c echo.Context) error { + var err error + jid := jwtPayload(c).JID + + var reqSendPoll typWhatsApp.RequestSendPoll + reqSendPoll.RJID = strings.TrimSpace(c.FormValue("msisdn")) + reqSendPoll.Question = strings.TrimSpace(c.FormValue("question")) + reqSendPoll.Options = strings.TrimSpace(c.FormValue("options")) + + if len(reqSendPoll.RJID) == 0 { + return router.ResponseBadRequest(c, "Missing Form Value MSISDN") + } + + if len(reqSendPoll.Question) == 0 { + return router.ResponseBadRequest(c, "Missing Form Value Question") + } + + if len(reqSendPoll.Options) == 0 { + return router.ResponseBadRequest(c, "Missing Form Value Options") + } + + isMultiAnswer := strings.TrimSpace(c.FormValue("multianswer")) + if len(isMultiAnswer) == 0 { + // If MultiAnswer Parameter Doesn't Exist or Empty String + // Then Set it Default to False + reqSendPoll.MultiAnswer = false + } else { + // If MultiAnswer Parameter is not Empty + // Then Parse it to Bool + reqSendPoll.MultiAnswer, err = strconv.ParseBool(isMultiAnswer) + if err != nil { + return router.ResponseBadRequest(c, err.Error()) + } + } + + pollOptions := strings.Split(reqSendPoll.Options, ",") + for i, str := range pollOptions { + pollOptions[i] = strings.TrimSpace(str) + } + + var resSendMessage typWhatsApp.ResponseSendMessage + resSendMessage.MsgID, err = pkgWhatsApp.WhatsAppSendPoll(c.Request().Context(), jid, reqSendPoll.RJID, reqSendPoll.Question, pollOptions, reqSendPoll.MultiAnswer) + if err != nil { + return router.ResponseInternalError(c, err.Error()) + } + + return router.ResponseSuccessWithData(c, "Successfully Send Poll Message", resSendMessage) +} + // MessageEdit // @Summary Update Message // @Description Update Message to Spesific WhatsApp Personal ID or Group ID diff --git a/pkg/whatsapp/whatsapp.go b/pkg/whatsapp/whatsapp.go index 67b1820..8adaf94 100644 --- a/pkg/whatsapp/whatsapp.go +++ b/pkg/whatsapp/whatsapp.go @@ -1070,7 +1070,7 @@ func WhatsAppSendSticker(ctx context.Context, jid string, rjid string, stickerBy return "", errors.New("WhatsApp Client is not Valid") } -func WhatsAppSendPoll(ctx context.Context, jid string, rjid string, question string, options []string, isMultipleAnswer bool) (string, error) { +func WhatsAppSendPoll(ctx context.Context, jid string, rjid string, question string, options []string, isMultiAnswer bool) (string, error) { if WhatsAppClient[jid] != nil { var err error @@ -1096,7 +1096,7 @@ func WhatsAppSendPoll(ctx context.Context, jid string, rjid string, question str // Check if Poll Allow Multiple Answer pollAnswerMax := 1 - if isMultipleAnswer { + if isMultiAnswer { pollAnswerMax = len(options) } @@ -1106,7 +1106,7 @@ func WhatsAppSendPoll(ctx context.Context, jid string, rjid string, question str } // Send WhatsApp Message Proto - _, err = WhatsAppClient[jid].SendMessage(ctx, remoteJID, WhatsAppClient[jid].BuildPollCreation(question, options, pollAnswerMax)) + _, err = WhatsAppClient[jid].SendMessage(ctx, remoteJID, WhatsAppClient[jid].BuildPollCreation(question, options, pollAnswerMax), msgExtra) if err != nil { return "", err }