Browse Source

add feature send poll

pull/61/head
Dimas Restu H 2 years ago
parent
commit
2245e8b235
  1. 55
      docs/docs.go
  2. 55
      docs/swagger.json
  3. 36
      docs/swagger.yaml
  4. 1
      internal/route.go
  5. 8
      internal/whatsapp/types/request.go
  6. 62
      internal/whatsapp/whatsapp.go
  7. 6
      pkg/whatsapp/whatsapp.go

55
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": [

55
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": [

36
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:

1
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))

8
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 {

62
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

6
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
}

Loading…
Cancel
Save