Skip to content

Commit

Permalink
SW-3858 Added validation of sendPoll parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dephea committed Jun 17, 2024
1 parent c9dbc22 commit 3c83cb5
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/categories/methods/sending.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package methods

import "fmt"

type SendingCategory struct {
GreenAPI GreenAPIInterface
}
Expand Down Expand Up @@ -82,5 +84,41 @@ func (c SendingCategory) UploadFile(filePath string) (map[string]interface{}, er
// to a private or group chat.
// https://green-api.com/en/docs/api/sending/SendPoll/
func (c SendingCategory) SendPoll(parameters map[string]interface{}) (map[string]interface{}, error) {
message, ok := parameters["message"].(string)
if !ok {
return nil, fmt.Errorf("cannot find message paramater")
}

if len(message) > 255 {
return nil, fmt.Errorf("number of characters in message exceeded (more than 255)")
}

options, ok := parameters["options"].([]map[string]interface{})
if !ok {
return nil, fmt.Errorf("options is not of type []map[string]interface{}")
}

if len(options) < 2 {
return nil, fmt.Errorf("cannot create less than 2 poll options")
} else if len(options) > 12 {
return nil, fmt.Errorf("cannot create more than 12 poll options")
}

seen := make(map[string]bool)

for _, option := range options {
optionValue, ok := option["optionName"].(string)
if len(optionValue) > 100 {
return nil, fmt.Errorf("number of characters in optionName exceeded (more than 100)")
}
if !ok {
return nil, fmt.Errorf("option does not have a valid 'optionName'")
}
if seen[optionValue] {
return nil, fmt.Errorf("poll options cannot have duplicates: %s", optionValue)
}
seen[optionValue] = true
}

return c.GreenAPI.Request("POST", "sendPoll", parameters, "")
}

0 comments on commit 3c83cb5

Please sign in to comment.