Skip to content

Commit 3c83cb5

Browse files
committed
SW-3858 Added validation of sendPoll parameters
1 parent c9dbc22 commit 3c83cb5

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pkg/categories/methods/sending.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package methods
22

3+
import "fmt"
4+
35
type SendingCategory struct {
46
GreenAPI GreenAPIInterface
57
}
@@ -82,5 +84,41 @@ func (c SendingCategory) UploadFile(filePath string) (map[string]interface{}, er
8284
// to a private or group chat.
8385
// https://green-api.com/en/docs/api/sending/SendPoll/
8486
func (c SendingCategory) SendPoll(parameters map[string]interface{}) (map[string]interface{}, error) {
87+
message, ok := parameters["message"].(string)
88+
if !ok {
89+
return nil, fmt.Errorf("cannot find message paramater")
90+
}
91+
92+
if len(message) > 255 {
93+
return nil, fmt.Errorf("number of characters in message exceeded (more than 255)")
94+
}
95+
96+
options, ok := parameters["options"].([]map[string]interface{})
97+
if !ok {
98+
return nil, fmt.Errorf("options is not of type []map[string]interface{}")
99+
}
100+
101+
if len(options) < 2 {
102+
return nil, fmt.Errorf("cannot create less than 2 poll options")
103+
} else if len(options) > 12 {
104+
return nil, fmt.Errorf("cannot create more than 12 poll options")
105+
}
106+
107+
seen := make(map[string]bool)
108+
109+
for _, option := range options {
110+
optionValue, ok := option["optionName"].(string)
111+
if len(optionValue) > 100 {
112+
return nil, fmt.Errorf("number of characters in optionName exceeded (more than 100)")
113+
}
114+
if !ok {
115+
return nil, fmt.Errorf("option does not have a valid 'optionName'")
116+
}
117+
if seen[optionValue] {
118+
return nil, fmt.Errorf("poll options cannot have duplicates: %s", optionValue)
119+
}
120+
seen[optionValue] = true
121+
}
122+
85123
return c.GreenAPI.Request("POST", "sendPoll", parameters, "")
86124
}

0 commit comments

Comments
 (0)