From 3c83cb5da70c482996a00ced650277986d157c44 Mon Sep 17 00:00:00 2001 From: dephea Date: Mon, 17 Jun 2024 10:23:04 +0300 Subject: [PATCH] SW-3858 Added validation of sendPoll parameters --- pkg/categories/methods/sending.go | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/categories/methods/sending.go b/pkg/categories/methods/sending.go index df26587..060d7df 100644 --- a/pkg/categories/methods/sending.go +++ b/pkg/categories/methods/sending.go @@ -1,5 +1,7 @@ package methods +import "fmt" + type SendingCategory struct { GreenAPI GreenAPIInterface } @@ -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, "") }