Skip to content

Commit

Permalink
feat: add send poll message
Browse files Browse the repository at this point in the history
  • Loading branch information
aldinokemal committed Mar 10, 2024
1 parent 62f334f commit aa2f10d
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
![release linux](https://github.com/aldinokemal/go-whatsapp-web-multidevice/actions/workflows/release-linux.yml/badge.svg)
![release macos](https://github.com/aldinokemal/go-whatsapp-web-multidevice/actions/workflows/release-mac.yml/badge.svg)

### Support `ARM` Architecture
Now that we support ARM64 for Linux:
- [Release](https://github.com/aldinokemal/go-whatsapp-web-multidevice/releases/latest) for ARM64
- [Docker Image](https://hub.docker.com/r/aldinokemal2104/go-whatsapp-web-multidevice/tags) for ARM64.
### Support `ARM` Architecture

Now that we support ARM64 for Linux:

- [Release](https://github.com/aldinokemal/go-whatsapp-web-multidevice/releases/latest) for ARM64
- [Docker Image](https://hub.docker.com/r/aldinokemal2104/go-whatsapp-web-multidevice/tags) for ARM64.

### Feature

- Send whatsapp via http API, [docs/openapi.yml](./docs/openapi.yaml) for more details
- Compress image before send
- Compress video before send
Expand Down Expand Up @@ -84,6 +86,7 @@ Now that we support ARM64 for Linux:
7. open `http://localhost:3000` in browser

### Production Mode (docker)

```
docker run --detach --publish=3000:3000 --name=whatsapp --restart=always --volume=$(docker volume create --name=whatsapp):/app/storages aldinokemal2104/go-whatsapp-web-multidevice --autoreply="Dont't reply this message please"
```
Expand Down Expand Up @@ -116,6 +119,7 @@ API using [openapi-generator](https://openapi-generator.tech/#try)
|| Send Contact | POST | /send/contact |
|| Send Link | POST | /send/link |
|| Send Location | POST | /send/location |
|| Send Poll / Vote | POST | /send/poll |
|| Revoke Message | POST | /message/:message_id/revoke |
|| React Message | POST | /message/:message_id/react |
|| Join Group With Link | POST | /group/join-with-link |
Expand Down
2 changes: 1 addition & 1 deletion src/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
AppVersion = "v4.10.0"
AppVersion = "v4.11.0"
AppPort = "3000"
AppDebug = false
AppOs = fmt.Sprintf("AldinoKemal")
Expand Down
8 changes: 8 additions & 0 deletions src/domains/send/poll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package send

type PollRequest struct {
Phone string `json:"phone" form:"phone"`
Question string `json:"question" form:"question"`
Options []string `json:"options[]" form:"options[]"`
MaxAnswer int `json:"max_answer" form:"max_answer"`
}
1 change: 1 addition & 0 deletions src/domains/send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ISendService interface {
SendLink(ctx context.Context, request LinkRequest) (response GenericResponse, err error)
SendLocation(ctx context.Context, request LocationRequest) (response GenericResponse, err error)
SendAudio(ctx context.Context, request AudioRequest) (response GenericResponse, err error)
SendPoll(ctx context.Context, request PollRequest) (response GenericResponse, err error)
}

type GenericResponse struct {
Expand Down
19 changes: 19 additions & 0 deletions src/internal/rest/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func InitRestSend(app *fiber.App, service domainSend.ISendService) Send {
app.Post("/send/link", rest.SendLink)
app.Post("/send/location", rest.SendLocation)
app.Post("/send/audio", rest.SendAudio)
app.Post("/send/poll", rest.SendPoll)
return rest
}

Expand Down Expand Up @@ -185,3 +186,21 @@ func (controller *Send) SendAudio(c *fiber.Ctx) error {
Results: response,
})
}

func (controller *Send) SendPoll(c *fiber.Ctx) error {
var request domainSend.PollRequest
err := c.BodyParser(&request)
utils.PanicIfNeeded(err)

whatsapp.SanitizePhone(&request.Phone)

response, err := controller.Service.SendPoll(c.UserContext(), request)
utils.PanicIfNeeded(err)

return c.JSON(utils.ResponseData{
Status: 200,
Code: "SUCCESS",
Message: response.Status,
Results: response,
})
}
20 changes: 20 additions & 0 deletions src/services/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,23 @@ func (service serviceSend) SendAudio(ctx context.Context, request domainSend.Aud
response.Status = fmt.Sprintf("Send audio success %s (server timestamp: %s)", request.Phone, ts.Timestamp.String())
return response, nil
}

func (service serviceSend) SendPoll(ctx context.Context, request domainSend.PollRequest) (response domainSend.GenericResponse, err error) {
err = validations.ValidateSendPoll(ctx, request)
if err != nil {
return response, err
}
dataWaRecipient, err := whatsapp.ValidateJidWithLogin(service.WaCli, request.Phone)
if err != nil {
return response, err
}

ts, err := service.WaCli.SendMessage(ctx, dataWaRecipient, service.WaCli.BuildPollCreation(request.Question, request.Options, request.MaxAnswer))
if err != nil {
return response, err
}

response.MessageID = ts.ID
response.Status = fmt.Sprintf("Send poll success %s (server timestamp: %s)", request.Phone, ts.Timestamp.String())
return response, nil
}
30 changes: 30 additions & 0 deletions src/validations/send_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,33 @@ func ValidateSendAudio(ctx context.Context, request domainSend.AudioRequest) err

return nil
}

func ValidateSendPoll(ctx context.Context, request domainSend.PollRequest) error {
err := validation.ValidateStructWithContext(ctx, &request,
validation.Field(&request.Phone, validation.Required),
validation.Field(&request.Question, validation.Required),

validation.Field(&request.Options, validation.Required),
validation.Field(&request.Options, validation.Each(validation.Required)),

validation.Field(&request.MaxAnswer, validation.Required),
validation.Field(&request.MaxAnswer, validation.Min(1)),
validation.Field(&request.MaxAnswer, validation.Max(len(request.Options))),
)

if err != nil {
return pkgError.ValidationError(err.Error())
}

// validate options should be unique each other
uniqueOptions := make(map[string]bool)
for _, option := range request.Options {
if _, ok := uniqueOptions[option]; ok {
return pkgError.ValidationError("options should be unique")
}
uniqueOptions[option] = true
}

return nil

}

0 comments on commit aa2f10d

Please sign in to comment.