Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SW-3858 Added validation of sendPoll parameters #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "")
}
Loading