-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsms_service.go
410 lines (347 loc) · 11.7 KB
/
sms_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package sendpulse_sdk_go
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type SmsService struct {
client *Client
}
func newSmsService(cl *Client) *SmsService {
return &SmsService{client: cl}
}
type SmsVariable struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Value any `json:"value"`
}
type AddPhonesCounters struct {
Added int `json:"added"`
Exceptions int `json:"exceptions"`
Exists int `json:"exists"`
}
func (service *SmsService) AddPhones(ctx context.Context, mailingListID int, phones []string) (*AddPhonesCounters, error) {
path := "/sms/numbers"
type paramsFormat struct {
AddressBookID int `json:"addressBookId"`
Phones []string `json:"phones"`
}
data := paramsFormat{AddressBookID: mailingListID, Phones: phones}
var respData struct {
Result bool `json:"result"`
Counters *AddPhonesCounters `json:"counters"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return respData.Counters, err
}
type PhoneWithVariable struct {
Phone string
Variables []SmsVariable
}
func (service *SmsService) AddPhonesWithVariables(ctx context.Context, mailingListID int, phones []*PhoneWithVariable) (*AddPhonesCounters, error) {
path := "/sms/numbers/variables"
type paramsFormat struct {
AddressBookID int `json:"addressBookId"`
Phones map[string][][]SmsVariable `json:"phones"`
}
ph := make(map[string][][]SmsVariable)
for _, item := range phones {
ph[item.Phone] = append(ph[item.Phone], item.Variables)
}
data := paramsFormat{
AddressBookID: mailingListID,
Phones: ph,
}
var respData struct {
Result bool `json:"result"`
Counters *AddPhonesCounters `json:"counters"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return respData.Counters, err
}
func (service *SmsService) UpdateVariablesSingle(ctx context.Context, addressBookID int, phone string, variables []SmsVariable) error {
path := fmt.Sprintf("/addressbooks/%d/phones/variable", addressBookID)
type paramsFormat struct {
Phone string `json:"phone"`
Variables []SmsVariable `json:"variables"`
}
data := paramsFormat{Phone: phone, Variables: variables}
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return err
}
func (service *SmsService) UpdateVariablesMultiple(ctx context.Context, addressBookID int, phones []string, variables []SmsVariable) error {
path := "/sms/numbers"
type paramsFormat struct {
AddressBookID int `json:"addressBookId"`
Phones []string `json:"phones"`
Variables []SmsVariable `json:"variables"`
}
data := paramsFormat{
AddressBookID: addressBookID,
Phones: phones,
Variables: variables,
}
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodPut, path, data, &respData, true)
return err
}
func (service *SmsService) DeletePhones(ctx context.Context, addressBookID int, phones []string) error {
path := "/sms/numbers"
type paramsFormat struct {
AddressBookID int `json:"addressBookId"`
Phones []string `json:"phones"`
}
data := paramsFormat{
AddressBookID: addressBookID,
Phones: phones,
}
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodDelete, path, data, &respData, true)
return err
}
type PhoneInfo struct {
Status int `json:"status"`
Variables map[string]any `json:"variables"`
Added DateTime `json:"added"`
}
func (service *SmsService) GetPhoneInfo(ctx context.Context, addressBookID int, phone string) (*PhoneInfo, error) {
path := fmt.Sprintf("/sms/numbers/info/%d/%s", addressBookID, phone)
var respData struct {
Result bool `json:"result"`
Data *PhoneInfo `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
func (service *SmsService) AddToBlacklist(ctx context.Context, phones []string, description string) error {
path := "/sms/black_list"
type paramsFormat struct {
Description string `json:"description"`
Phones []string `json:"phones"`
}
data := paramsFormat{
Description: description,
Phones: phones,
}
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return err
}
func (service *SmsService) RemoveFromBlacklist(ctx context.Context, phones []string) error {
path := "/sms/black_list"
type paramsFormat struct {
Phones []string `json:"phones"`
}
data := paramsFormat{
Phones: phones,
}
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodDelete, path, data, &respData, true)
return err
}
type BlacklistPhone struct {
Phone string
Description string `json:"description"`
AddDate DateTime `json:"add_date"`
}
func (service *SmsService) GetBlacklistedPhones(ctx context.Context, phones []string) ([]*BlacklistPhone, error) {
path := "/sms/black_list/by_numbers"
urlParams := url.Values{}
urlParams.Add("phones", "["+strings.Join(phones, ",")+"]")
path += "?" + urlParams.Encode()
type BlacklistPhoneInternal struct {
BlacklistPhone
Phone int `json:"phone"`
}
var respData struct {
Result bool `json:"result"`
Data []*BlacklistPhoneInternal `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
if err != nil {
return nil, err
}
data := make([]*BlacklistPhone, len(respData.Data))
for i, item := range respData.Data {
item.BlacklistPhone.Phone = strconv.Itoa(item.Phone)
data[i] = &item.BlacklistPhone
}
return data, nil
}
type CreateSmsCampaignByAddressBookParams struct {
Sender string `json:"sender"`
MailingListID int `json:"addressBookId"`
Body string `json:"body"`
Transliterate int `json:"transliterate"`
Route map[string]string `json:"route,omitempty"`
Date DateTime `json:"date"`
Emulate int `json:"emulate"`
}
func (service *SmsService) CreateCampaignByMailingList(ctx context.Context, params CreateSmsCampaignByAddressBookParams) (int, error) {
path := "/sms/campaigns"
var respData struct {
Result bool `json:"result"`
CampaignID int `json:"campaign_id"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return respData.CampaignID, err
}
type CreateSmsCampaignByPhonesParams struct {
Sender string `json:"sender"`
Phones []string `json:"phones"`
Body string `json:"body"`
Transliterate int `json:"transliterate"`
Route map[string]string `json:"route,omitempty"`
Date DateTime `json:"date"`
Emulate int `json:"emulate"`
}
func (service *SmsService) CreateCampaignByPhones(ctx context.Context, params CreateSmsCampaignByPhonesParams) (int, error) {
path := "/sms/send"
var respData struct {
Result bool `json:"result"`
CampaignID int `json:"campaign_id"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return respData.CampaignID, err
}
type SmsCampaign struct {
ID int `json:"id"`
AddressBookID int `json:"address_book_id"`
CompanyPrice Float32 `json:"company_price"`
CompanyCurrency string `json:"company_currency"`
SendDate DateTime `json:"send_date"`
DateCreated DateTime `json:"date_created"`
SenderMailAddress string `json:"sender_mail_address"`
SenderMailName string `json:"sender_mail_name"`
}
func (service *SmsService) GetCampaigns(ctx context.Context, dateFrom, dateTo time.Time) ([]*SmsCampaign, error) {
dtFormat := "2006-01-02 15:04:05"
path := "/sms/campaigns/list"
urlParams := url.Values{}
urlParams.Add("dateFrom", dateFrom.Format(dtFormat))
urlParams.Add("dateTo", dateTo.Format(dtFormat))
path += "?" + urlParams.Encode()
var respData struct {
Result bool `json:"result"`
Data []*SmsCampaign `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
type SmsCampaignInfo struct {
ID int `json:"id"`
AddressBookID int `json:"address_book_id"`
Currency string `json:"currency"`
CompanyPrice Float32 `json:"company_price"`
SendDate DateTime `json:"send_date"`
DateCreated DateTime `json:"date_created"`
SenderName string `json:"sender_name"`
PhonesInfo []struct {
Phone int `json:"phone"`
Status int `json:"status"`
StatusExplain string `json:"status_explain"`
CountryCode string `json:"сountry_code"`
MoneySpent float32 `json:"money_spent"`
} `json:"task_phones_info"`
}
func (service *SmsService) GetCampaignInfo(ctx context.Context, id int) (*SmsCampaignInfo, error) {
path := fmt.Sprintf("/sms/campaigns/info/%d", id)
var respData struct {
Result bool `json:"result"`
Data *SmsCampaignInfo `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
func (service *SmsService) CancelCampaign(ctx context.Context, id int) error {
path := fmt.Sprintf("/sms/campaigns/cancel/%d", id)
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodPut, path, nil, &respData, true)
return err
}
type SmsCampaignCostParams struct {
AddressBookID int
Phones []string
Body string
Sender string
Route map[string]string
}
type SmsCampaignCampaignCost struct {
Price float32 `json:"price"`
Currency string `json:"currency"`
}
func (service *SmsService) GetCampaignCost(ctx context.Context, params SmsCampaignCostParams) (*SmsCampaignCampaignCost, error) {
path := "/sms/campaigns/cost"
urlParams := url.Values{}
if params.AddressBookID != 0 {
urlParams.Add("addressBookId", strconv.Itoa(params.AddressBookID))
}
if len(params.Phones) != 0 {
urlParams.Add("phones", "["+strings.Join(params.Phones, ",")+"]")
}
if params.Body != "" {
urlParams.Add("body", params.Body)
}
if params.Sender != "" {
urlParams.Add("sender", params.Sender)
}
if len(params.Route) != 0 {
route, _ := json.Marshal(params.Route)
urlParams.Add("route", string(route))
}
if len(urlParams) != 0 {
path += "?" + urlParams.Encode()
}
var respData struct {
Result bool `json:"result"`
Data *SmsCampaignCampaignCost `json:"data"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Data, err
}
type SmsSender struct {
ID int `json:"id"`
Sender string `json:"sender"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
Status int `json:"status"`
StatusExplain string `json:"status_explain"`
}
func (service *SmsService) GetSenders(ctx context.Context) ([]*SmsSender, error) {
path := "/sms/senders"
var respData []*SmsSender
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
func (service *SmsService) DeleteCampaign(ctx context.Context, id int) error {
path := "/sms/campaigns"
type paramsFormat struct {
ID int `json:"id"`
}
data := paramsFormat{
ID: id,
}
var respData struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodDelete, path, data, &respData, true)
return err
}