|
1 | 1 | package methods
|
2 | 2 |
|
| 3 | +import "fmt" |
| 4 | + |
3 | 5 | type SendingCategory struct {
|
4 | 6 | GreenAPI GreenAPIInterface
|
5 | 7 | }
|
@@ -82,5 +84,41 @@ func (c SendingCategory) UploadFile(filePath string) (map[string]interface{}, er
|
82 | 84 | // to a private or group chat.
|
83 | 85 | // https://green-api.com/en/docs/api/sending/SendPoll/
|
84 | 86 | 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 | + |
85 | 123 | return c.GreenAPI.Request("POST", "sendPoll", parameters, "")
|
86 | 124 | }
|
0 commit comments