|
| 1 | +package greenapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | +) |
| 6 | + |
| 7 | +type AccountCategory struct { |
| 8 | + GreenAPI GreenAPIInterface |
| 9 | +} |
| 10 | + |
| 11 | +// ------------------------------------------------------------------ GetSettings |
| 12 | + |
| 13 | +// Getting settings of an instance. |
| 14 | +// |
| 15 | +// https://green-api.com/v3/docs/api/account/GetSettings/ |
| 16 | +func (c AccountCategory) GetSettings() (*APIResponse, error) { |
| 17 | + return c.GreenAPI.Request("GET", "getSettings", nil) |
| 18 | +} |
| 19 | + |
| 20 | +// ------------------------------------------------------------------ SetSettings |
| 21 | + |
| 22 | +type RequestSetSettings struct { |
| 23 | + WebhookUrl *string `json:"webhookUrl,omitempty"` |
| 24 | + WebhookUrlToken *string `json:"webhookUrlToken,omitempty"` |
| 25 | + DelaySendMessagesMilliseconds *uint `json:"delaySendMessagesMilliseconds,omitempty"` |
| 26 | + MarkIncomingMessagesReaded string `json:"markIncomingMessagesReaded,omitempty"` |
| 27 | + MarkIncomingMessagesReadedOnReply string `json:"markIncomingMessagesReadedOnReply,omitempty"` |
| 28 | + OutgoingWebhook string `json:"outgoingWebhook,omitempty"` |
| 29 | + OutgoingMessageWebhook string `json:"outgoingMessageWebhook,omitempty"` |
| 30 | + OutgoingAPIMessageWebhook string `json:"outgoingAPIMessageWebhook,omitempty"` |
| 31 | + StateWebhook string `json:"stateWebhook,omitempty"` |
| 32 | + IncomingWebhook string `json:"incomingWebhook,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +type SetSettingsOption func(*RequestSetSettings) error |
| 36 | + |
| 37 | +// URL for sending notifications. |
| 38 | +func OptionalWebhookUrl(webhookUrl string) SetSettingsOption { |
| 39 | + return func(r *RequestSetSettings) error { |
| 40 | + err := ValidateURL(webhookUrl) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + r.WebhookUrl = &webhookUrl |
| 45 | + return nil |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Token to access your notification server. |
| 50 | +func OptionalWebhookUrlToken(webhookUrlToken string) SetSettingsOption { |
| 51 | + return func(r *RequestSetSettings) error { |
| 52 | + r.WebhookUrlToken = &webhookUrlToken |
| 53 | + return nil |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Message sending delay. |
| 58 | +func OptionalDelaySendMessages(delaySendMessagesMilliseconds uint) SetSettingsOption { |
| 59 | + return func(r *RequestSetSettings) error { |
| 60 | + r.DelaySendMessagesMilliseconds = &delaySendMessagesMilliseconds |
| 61 | + return nil |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Mark incoming messages as read or not. |
| 66 | +func OptionalMarkIncomingMessagesRead(markIncomingMessagesReaded bool) SetSettingsOption { |
| 67 | + return func(r *RequestSetSettings) error { |
| 68 | + if markIncomingMessagesReaded { |
| 69 | + r.MarkIncomingMessagesReaded = "yes" |
| 70 | + } else { |
| 71 | + r.MarkIncomingMessagesReaded = "no" |
| 72 | + } |
| 73 | + return nil |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Mark incoming messages as read when posting a message to the chat via API. |
| 78 | +func OptionalMarkIncomingMessagesReadOnReply(markIncomingMessagesReadedOnReply bool) SetSettingsOption { |
| 79 | + return func(r *RequestSetSettings) error { |
| 80 | + if markIncomingMessagesReadedOnReply { |
| 81 | + r.MarkIncomingMessagesReadedOnReply = "yes" |
| 82 | + } else { |
| 83 | + r.MarkIncomingMessagesReadedOnReply = "no" |
| 84 | + } |
| 85 | + return nil |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Get notifications about outgoing messages sending/delivering/reading statuses |
| 90 | +func OptionalOutgoingWebhook(outgoingWebhook bool) SetSettingsOption { |
| 91 | + return func(r *RequestSetSettings) error { |
| 92 | + if outgoingWebhook { |
| 93 | + r.OutgoingWebhook = "yes" |
| 94 | + } else { |
| 95 | + r.OutgoingWebhook = "no" |
| 96 | + } |
| 97 | + return nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Get notifications about messages sent from the phone. |
| 102 | +func OptionalOutgoingMessageWebhook(outgoingMessageWebhook bool) SetSettingsOption { |
| 103 | + return func(r *RequestSetSettings) error { |
| 104 | + if outgoingMessageWebhook { |
| 105 | + r.OutgoingMessageWebhook = "yes" |
| 106 | + } else { |
| 107 | + r.OutgoingMessageWebhook = "no" |
| 108 | + } |
| 109 | + return nil |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Get notifications about messages sent from API. |
| 114 | +func OptionalOutgoingAPIMessageWebhook(outgoingAPIMessageWebhook bool) SetSettingsOption { |
| 115 | + return func(r *RequestSetSettings) error { |
| 116 | + if outgoingAPIMessageWebhook { |
| 117 | + r.OutgoingAPIMessageWebhook = "yes" |
| 118 | + } else { |
| 119 | + r.OutgoingAPIMessageWebhook = "no" |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Get notifications about the instance authorization state change. |
| 126 | +func OptionalStateWebhook(stateWebhook bool) SetSettingsOption { |
| 127 | + return func(r *RequestSetSettings) error { |
| 128 | + if stateWebhook { |
| 129 | + r.StateWebhook = "yes" |
| 130 | + } else { |
| 131 | + r.StateWebhook = "no" |
| 132 | + } |
| 133 | + return nil |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Get notifications about incoming messages and files. |
| 138 | +func OptionalIncomingWebhook(incomingWebhook bool) SetSettingsOption { |
| 139 | + return func(r *RequestSetSettings) error { |
| 140 | + if incomingWebhook { |
| 141 | + r.IncomingWebhook = "yes" |
| 142 | + } else { |
| 143 | + r.IncomingWebhook = "no" |
| 144 | + } |
| 145 | + return nil |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// Applying settings for an instance. |
| 150 | +// |
| 151 | +// https://green-api.com/v3/docs/api/account/SetSettings/ |
| 152 | +// |
| 153 | +// Add optional arguments by passing these functions: |
| 154 | +// |
| 155 | +// OptionalWebhookUrl(webhookUrl string) <- URL for sending notifications. |
| 156 | +// OptionalWebhookUrlToken(webhookUrlToken string) <- Token to access your notification server. |
| 157 | +// OptionalDelaySendMesssages(delaySendMessagesMilliseconds int) <- Message sending delay. |
| 158 | +// OptionalMarkIncomingMessagesRead(markIncomingMessagesReaded bool) <- Mark incoming messages as read or not. |
| 159 | +// OptionalMarkIncomingMessagesReadOnReply(markIncomingMessagesReadedOnReply bool) <- Mark incoming messages as read when posting a message to the chat via API. |
| 160 | +// OptionalOutgoingWessebhook(outgoingWebhook bool) <- Get notifications about outgoing messages sending/delivering/reading statuses. |
| 161 | +// OptionalOutgoingMageWebhook(outgoingMessageWebhook bool) <- Get notifications about messages sent from the phone. |
| 162 | +// OptionalOutgoingAPIMessageWebhook(outgoingAPIMessageWebhook bool) <- Get notifications about messages sent from API. |
| 163 | +// OptionalStateWebhook(stateWebhook bool) <- Get notifications about the instance authorization state change. |
| 164 | +// OptionalIncomingWebhook(incomingWebhook bool) <- Get notifications about incoming messages and files. |
| 165 | +func (c AccountCategory) SetSettings(options ...SetSettingsOption) (*APIResponse, error) { |
| 166 | + |
| 167 | + r := &RequestSetSettings{} |
| 168 | + for _, o := range options { |
| 169 | + err := o(r) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + jsonData, err := json.Marshal(r) |
| 176 | + if err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + |
| 180 | + return c.GreenAPI.Request("POST", "setSettings", jsonData) |
| 181 | +} |
| 182 | + |
| 183 | +// ------------------------------------------------------------------ GetStateInstance |
| 184 | + |
| 185 | +// Getting state of an instance. |
| 186 | +// |
| 187 | +// https://green-api.com/v3/docs/api/account/GetStateInstance/ |
| 188 | +func (c AccountCategory) GetStateInstance() (*APIResponse, error) { |
| 189 | + return c.GreenAPI.Request("GET", "getStateInstance", nil) |
| 190 | +} |
| 191 | + |
| 192 | +// ------------------------------------------------------------------ GetStatusInstance |
| 193 | + |
| 194 | +// Getting the status of an instance socket connection with MAX. |
| 195 | +// |
| 196 | +// https://green-api.com/v3/docs/api/account/GetStatusInstance/ |
| 197 | +func (c AccountCategory) GetStatusInstance() (*APIResponse, error) { |
| 198 | + return c.GreenAPI.Request("GET", "getStatusInstance", nil) |
| 199 | +} |
| 200 | + |
| 201 | +// ------------------------------------------------------------------ Reboot |
| 202 | + |
| 203 | +// Rebooting an instance. |
| 204 | +// |
| 205 | +// https://green-api.com/v3/docs/api/account/Reboot/ |
| 206 | +func (c AccountCategory) Reboot() (*APIResponse, error) { |
| 207 | + return c.GreenAPI.Request("GET", "reboot", nil) |
| 208 | +} |
| 209 | + |
| 210 | +// ------------------------------------------------------------------ Logout |
| 211 | + |
| 212 | +// Logging out an instance. |
| 213 | +// |
| 214 | +// https://green-api.com/v3/docs/api/account/Logout/ |
| 215 | +func (c AccountCategory) Logout() (*APIResponse, error) { |
| 216 | + return c.GreenAPI.Request("GET", "logout", nil) |
| 217 | +} |
| 218 | + |
| 219 | +// ------------------------------------------------------------------ StartAuthorization |
| 220 | + |
| 221 | +type RequestStartAuthorization struct { |
| 222 | + PhoneNumber int `json:"phoneNumber"` |
| 223 | +} |
| 224 | + |
| 225 | +// Start instance authorization |
| 226 | +// |
| 227 | +// https://green-api.com/v3/en/docs/api/account/StartAuthorization/ |
| 228 | +func (c AccountCategory) StartAuthorization(phoneNumber int) (*APIResponse, error) { |
| 229 | + r := &RequestStartAuthorization{ |
| 230 | + PhoneNumber: phoneNumber, |
| 231 | + } |
| 232 | + |
| 233 | + jsonData, err := json.Marshal(r) |
| 234 | + if err != nil { |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + |
| 238 | + return c.GreenAPI.Request("POST", "startAuthorization", jsonData) |
| 239 | +} |
| 240 | + |
| 241 | +// ------------------------------------------------------------------ SendAuthorizationCode |
| 242 | + |
| 243 | +type RequestSendAuthorizationCode struct { |
| 244 | + Code string `json:"code"` |
| 245 | +} |
| 246 | + |
| 247 | +// Start instance authorization |
| 248 | +// |
| 249 | +// https://green-api.com/v3/en/docs/api/account/StartAuthorization/ |
| 250 | +func (c AccountCategory) SendAuthorizationCode(code string) (*APIResponse, error) { |
| 251 | + r := &RequestSendAuthorizationCode{ |
| 252 | + Code: code, |
| 253 | + } |
| 254 | + |
| 255 | + jsonData, err := json.Marshal(r) |
| 256 | + if err != nil { |
| 257 | + return nil, err |
| 258 | + } |
| 259 | + |
| 260 | + return c.GreenAPI.Request("POST", "sendAuthorizationCode", jsonData) |
| 261 | +} |
| 262 | + |
| 263 | +// ------------------------------------------------------------------ SetProfilePicture |
| 264 | + |
| 265 | +type RequestSetProfilePicture struct { |
| 266 | + File string `json:"file"` |
| 267 | +} |
| 268 | + |
| 269 | +// Setting a profile picture. |
| 270 | +// |
| 271 | +// https://green-api.com/v3/docs/api/account/SetProfilePicture/ |
| 272 | +func (c AccountCategory) SetProfilePicture(filepath string) (*APIResponse, error) { |
| 273 | + r := &RequestSetProfilePicture{ |
| 274 | + File: filepath, |
| 275 | + } |
| 276 | + |
| 277 | + jsonData, err := json.Marshal(r) |
| 278 | + if err != nil { |
| 279 | + return nil, err |
| 280 | + } |
| 281 | + |
| 282 | + return c.GreenAPI.Request("POST", "setProfilePicture", jsonData, WithFormData(true)) |
| 283 | +} |
| 284 | + |
| 285 | +// ------------------------------------------------------------------ GetAccountSettings |
| 286 | + |
| 287 | +// Getting information about the MAX account |
| 288 | +// |
| 289 | +// https://green-api.com/v3/docs/api/account/GetAccountSettings/ |
| 290 | +func (c AccountCategory) GetAccountSettings() (*APIResponse, error) { |
| 291 | + return c.GreenAPI.Request("GET", "getAccountSettings", nil) |
| 292 | +} |
0 commit comments