-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.go
639 lines (587 loc) · 21 KB
/
contacts.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package activecampaign
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
)
type Contacts struct {
Contacts []Contact `json:"contacts"`
Meta FieldValuesMeta `json:"meta"`
}
type Contact struct {
CreateDate string `json:"cdate"`
Email string `json:"email"`
Phone string `json:"phone"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
ID string `json:"id"`
UpdateDate string `json:"udate"`
Links ContactLinks `json:"links"`
}
type ContactLinks struct {
BounceLogs string `json:"bounceLogs"`
ContactAutomations string `json:"contactAutomations"`
ContactData string `json:"contactData"`
ContactGoals string `json:"contactGoals"`
ContactLists string `json:"contactLists"`
ContactLogs string `json:"contactLogs"`
ContactTags string `json:"contactTags"`
ContactDeals string `json:"contactDeals"`
Deals string `json:"deals"`
FieldValues string `json:"fieldValues"`
GeoIps string `json:"geoIps"`
Notes string `json:"notes"`
Organization string `json:"organization"`
PlusAppend string `json:"plusAppend"`
TrackingLogs string `json:"trackingLogs"`
ScoreValues string `json:"scoreValues"`
AccountContacts string `json:"accountContacts"`
AutomationEntryCounts string `json:"automationEntryCounts"`
}
func (a *ActiveCampaign) Contacts(ctx context.Context, pof *POF) (*Contacts, error) {
res, err := a.send(ctx, http.MethodGet, "contacts", pof, nil)
if err != nil {
return nil, &Error{Op: "contacts", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
var contacts Contacts
err = json.NewDecoder(res.Body).Decode(&contacts)
if err != nil {
return nil, &Error{Op: "contacts", Err: err}
}
return &contacts, nil
}
func (a *ActiveCampaign) ListContacts(ctx context.Context, listID string) (*Contacts, error) {
res, err := a.send(ctx, http.MethodGet, "contacts?listid="+listID+"&status=1", nil, nil)
if err != nil {
return nil, &Error{Op: "list contacts", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
var contacts Contacts
err = json.NewDecoder(res.Body).Decode(&contacts)
if err != nil {
return nil, &Error{Op: "list contacts", Err: err}
}
return &contacts, nil
}
func (a *ActiveCampaign) ContactFieldValues(ctx context.Context, pof *POF, id string) (*FieldValues, error) {
return a.fieldValues(ctx, pof, "contacts/"+id+"/fieldValues")
}
type ContactCreate struct {
Contact Contact `json:"contact"`
}
type ContactCreated struct {
Contact struct {
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
EmailEmpty bool `json:"email_empty"`
Cdate string `json:"cdate"`
Udate string `json:"udate"`
Orgid string `json:"orgid"`
Orgname string `json:"orgname"`
Links struct {
BounceLogs string `json:"bounceLogs"`
ContactAutomations string `json:"contactAutomations"`
ContactData string `json:"contactData"`
ContactGoals string `json:"contactGoals"`
ContactLists string `json:"contactLists"`
ContactLogs string `json:"contactLogs"`
ContactTags string `json:"contactTags"`
ContactDeals string `json:"contactDeals"`
Deals string `json:"deals"`
FieldValues string `json:"fieldValues"`
GeoIps string `json:"geoIps"`
Notes string `json:"notes"`
Organization string `json:"organization"`
PlusAppend string `json:"plusAppend"`
TrackingLogs string `json:"trackingLogs"`
ScoreValues string `json:"scoreValues"`
AccountContacts string `json:"accountContacts"`
AutomationEntryCounts string `json:"automationEntryCounts"`
} `json:"links"`
Hash string `json:"hash"`
ID string `json:"id"`
Organization string `json:"organization"`
} `json:"contact"`
}
type Links struct {
BounceLogs string `json:"bounceLogs"`
ContactAutomations string `json:"contactAutomations"`
ContactData string `json:"contactData"`
ContactGoals string `json:"contactGoals"`
ContactLists string `json:"contactLists"`
ContactLogs string `json:"contactLogs"`
ContactTags string `json:"contactTags"`
ContactDeals string `json:"contactDeals"`
Deals string `json:"deals"`
FieldValues string `json:"fieldValues"`
GeoIps string `json:"geoIps"`
Notes string `json:"notes"`
Organization string `json:"organization"`
PlusAppend string `json:"plusAppend"`
TrackingLogs string `json:"trackingLogs"`
ScoreValues string `json:"scoreValues"`
}
func (a *ActiveCampaign) ContactCreate(ctx context.Context, contact ContactCreate) (*ContactCreated, error) {
//Decode the Struct into JSON
jsonStr, err := json.Marshal(contact)
if err != nil {
return nil, &Error{Op: "contact create", Err: err}
}
res, err := a.send(ctx, http.MethodPost, "contacts", nil, bytes.NewReader(jsonStr))
if err != nil {
return nil, &Error{Op: "contact create", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode != http.StatusCreated {
return nil, errors.New("contact create: " + res.Status)
}
var contactCreated ContactCreated
err = json.NewDecoder(res.Body).Decode(&contactCreated)
if err != nil {
return nil, err
}
return &contactCreated, nil
}
func (a *ActiveCampaign) ContactDelete(ctx context.Context, id string) error {
res, err := a.send(ctx, http.MethodDelete, "contacts/"+id, nil, nil)
if err != nil {
return &Error{Op: "contact delete", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode == http.StatusOK {
return nil
}
var message struct {
Message string `json:"message"`
}
err = json.NewDecoder(res.Body).Decode(&message)
if err != nil {
return &Error{Op: "contact delete", Err: err}
}
return errors.New("contact delete: " + message.Message)
}
type ContactUpdate struct {
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
func (a *ActiveCampaign) ContactUpdate(ctx context.Context, id string, contact ContactUpdate) error {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
Contact ContactUpdate `json:"contact"`
}{
Contact: contact,
})
if err != nil {
return &Error{Op: "contact update", Err: err}
}
res, err := a.send(ctx, http.MethodPut, "contacts/"+id, nil, b)
if err != nil {
return &Error{Op: "contact update", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode == http.StatusOK {
return nil
}
var message struct {
Message string `json:"message"`
}
err = json.NewDecoder(res.Body).Decode(&message)
if err != nil {
return &Error{Op: "contact update", Err: err}
}
return errors.New("contact update: " + message.Message)
}
type GetContactResponse struct {
ContactAutomations []struct {
Contact string `json:"contact"`
Seriesid string `json:"seriesid"`
Startid string `json:"startid"`
Status string `json:"status"`
Adddate string `json:"adddate"`
Remdate interface{} `json:"remdate"`
Timespan interface{} `json:"timespan"`
Lastblock string `json:"lastblock"`
Lastdate string `json:"lastdate"`
CompletedElements string `json:"completedElements"`
TotalElements string `json:"totalElements"`
Completed int `json:"completed"`
CompleteValue int `json:"completeValue"`
Links struct {
Automation string `json:"automation"`
Contact string `json:"contact"`
ContactGoals string `json:"contactGoals"`
} `json:"links"`
ID string `json:"id"`
Automation string `json:"automation"`
} `json:"contactAutomations"`
ContactLists []struct {
Contact string `json:"contact"`
List string `json:"list"`
Form interface{} `json:"form"`
Seriesid string `json:"seriesid"`
Sdate interface{} `json:"sdate"`
Udate interface{} `json:"udate"`
Status string `json:"status"`
Responder string `json:"responder"`
Sync string `json:"sync"`
Unsubreason interface{} `json:"unsubreason"`
Campaign interface{} `json:"campaign"`
Message interface{} `json:"message"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
IP4Sub string `json:"ip4Sub"`
Sourceid string `json:"sourceid"`
AutosyncLog interface{} `json:"autosyncLog"`
IP4Last string `json:"ip4_last"`
IP4Unsub string `json:"ip4Unsub"`
UnsubscribeAutomation interface{} `json:"unsubscribeAutomation"`
Links struct {
Automation string `json:"automation"`
List string `json:"list"`
Contact string `json:"contact"`
Form string `json:"form"`
AutosyncLog string `json:"autosyncLog"`
Campaign string `json:"campaign"`
UnsubscribeAutomation string `json:"unsubscribeAutomation"`
Message string `json:"message"`
} `json:"links"`
ID string `json:"id"`
Automation interface{} `json:"automation"`
} `json:"contactLists"`
Deals []struct {
Owner string `json:"owner"`
Contact string `json:"contact"`
Organization interface{} `json:"organization"`
Group interface{} `json:"group"`
Title string `json:"title"`
Nexttaskid string `json:"nexttaskid"`
Currency string `json:"currency"`
Status string `json:"status"`
Links struct {
Activities string `json:"activities"`
Contact string `json:"contact"`
ContactDeals string `json:"contactDeals"`
Group string `json:"group"`
NextTask string `json:"nextTask"`
Notes string `json:"notes"`
Organization string `json:"organization"`
Owner string `json:"owner"`
ScoreValues string `json:"scoreValues"`
Stage string `json:"stage"`
Tasks string `json:"tasks"`
} `json:"links"`
ID string `json:"id"`
NextTask interface{} `json:"nextTask"`
} `json:"deals"`
FieldValues []struct {
Contact string `json:"contact"`
Field string `json:"field"`
Value interface{} `json:"value"`
Cdate string `json:"cdate"`
Udate string `json:"udate"`
Links struct {
Owner string `json:"owner"`
Field string `json:"field"`
} `json:"links"`
ID string `json:"id"`
Owner string `json:"owner"`
} `json:"fieldValues"`
GeoAddresses []struct {
IP4 string `json:"ip4"`
Country2 string `json:"country2"`
Country string `json:"country"`
State string `json:"state"`
City string `json:"city"`
Zip string `json:"zip"`
Area string `json:"area"`
Lat string `json:"lat"`
Lon string `json:"lon"`
Tz string `json:"tz"`
Tstamp string `json:"tstamp"`
Links []interface{} `json:"links"`
ID string `json:"id"`
} `json:"geoAddresses"`
GeoIps []struct {
Contact string `json:"contact"`
Campaignid string `json:"campaignid"`
Messageid string `json:"messageid"`
Geoaddrid string `json:"geoaddrid"`
IP4 string `json:"ip4"`
Tstamp string `json:"tstamp"`
GeoAddress string `json:"geoAddress"`
Links struct {
GeoAddress string `json:"geoAddress"`
} `json:"links"`
ID string `json:"id"`
} `json:"geoIps"`
Contact struct {
Cdate string `json:"cdate"`
Email string `json:"email"`
Phone string `json:"phone"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Orgid string `json:"orgid"`
SegmentioID string `json:"segmentio_id"`
BouncedHard string `json:"bounced_hard"`
BouncedSoft string `json:"bounced_soft"`
BouncedDate interface{} `json:"bounced_date"`
IP string `json:"ip"`
Ua interface{} `json:"ua"`
Hash string `json:"hash"`
SocialdataLastcheck interface{} `json:"socialdata_lastcheck"`
EmailLocal string `json:"email_local"`
EmailDomain string `json:"email_domain"`
Sentcnt string `json:"sentcnt"`
RatingTstamp interface{} `json:"rating_tstamp"`
Gravatar string `json:"gravatar"`
Deleted string `json:"deleted"`
Adate interface{} `json:"adate"`
Udate interface{} `json:"udate"`
Edate interface{} `json:"edate"`
ContactAutomations []string `json:"contactAutomations"`
ContactLists []string `json:"contactLists"`
FieldValues []string `json:"fieldValues"`
GeoIps []string `json:"geoIps"`
Deals []string `json:"deals"`
AccountContacts []string `json:"accountContacts"`
Links struct {
BounceLogs string `json:"bounceLogs"`
ContactAutomations string `json:"contactAutomations"`
ContactData string `json:"contactData"`
ContactGoals string `json:"contactGoals"`
ContactLists string `json:"contactLists"`
ContactLogs string `json:"contactLogs"`
ContactTags string `json:"contactTags"`
ContactDeals string `json:"contactDeals"`
Deals string `json:"deals"`
FieldValues string `json:"fieldValues"`
GeoIps string `json:"geoIps"`
Notes string `json:"notes"`
Organization string `json:"organization"`
PlusAppend string `json:"plusAppend"`
TrackingLogs string `json:"trackingLogs"`
ScoreValues string `json:"scoreValues"`
} `json:"links"`
ID string `json:"id"`
Organization interface{} `json:"organization"`
} `json:"contact"`
}
//Get Contact by ID
func (a *ActiveCampaign) ContactGet(ctx context.Context, id string) (*GetContactResponse, error) {
res, err := a.send(ctx, http.MethodGet, "contacts/"+id, nil, nil)
if err != nil {
return nil, &Error{Op: "contact get", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode == http.StatusOK {
var contact GetContactResponse
err = json.NewDecoder(res.Body).Decode(&contact)
if err != nil {
return nil, err
}
return &contact, nil
}
var message struct {
Message string `json:"message"`
}
err = json.NewDecoder(res.Body).Decode(&message)
if err != nil {
return nil, &Error{Op: "contact get", Err: err}
}
return nil, errors.New("contact get: " + message.Message)
}
type FilteredContactResponse struct {
Contacts []struct {
Cdate string `json:"cdate"`
Email string `json:"email"`
Phone string `json:"phone"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Orgid string `json:"orgid"`
SegmentioID string `json:"segmentio_id"`
BouncedHard string `json:"bounced_hard"`
BouncedSoft string `json:"bounced_soft"`
BouncedDate string `json:"bounced_date"`
IP string `json:"ip"`
Ua string `json:"ua"`
Hash string `json:"hash"`
SocialdataLastcheck string `json:"socialdata_lastcheck"`
EmailLocal string `json:"email_local"`
EmailDomain string `json:"email_domain"`
Sentcnt string `json:"sentcnt"`
RatingTstamp string `json:"rating_tstamp"`
Gravatar string `json:"gravatar"`
Deleted string `json:"deleted"`
Adate string `json:"adate"`
Udate string `json:"udate"`
Edate string `json:"edate"`
ScoreValues []interface{} `json:"scoreValues"`
Links struct {
BounceLogs string `json:"bounceLogs"`
ContactAutomations string `json:"contactAutomations"`
ContactData string `json:"contactData"`
ContactGoals string `json:"contactGoals"`
ContactLists string `json:"contactLists"`
ContactLogs string `json:"contactLogs"`
ContactTags string `json:"contactTags"`
ContactDeals string `json:"contactDeals"`
Deals string `json:"deals"`
FieldValues string `json:"fieldValues"`
GeoIps string `json:"geoIps"`
Notes string `json:"notes"`
Organization string `json:"organization"`
PlusAppend string `json:"plusAppend"`
TrackingLogs string `json:"trackingLogs"`
ScoreValues string `json:"scoreValues"`
} `json:"links"`
ID string `json:"id"`
Organization interface{} `json:"organization"`
AccountContacts []string `json:"accountContacts,omitempty"`
} `json:"contacts"`
Meta struct {
Total string `json:"total"`
PageInput struct {
Segmentid int `json:"segmentid"`
Formid int `json:"formid"`
Listid int `json:"listid"`
Tagid int `json:"tagid"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Search interface{} `json:"search"`
Sort interface{} `json:"sort"`
Seriesid int `json:"seriesid"`
Waitid int `json:"waitid"`
Status int `json:"status"`
ForceQuery int `json:"forceQuery"`
Cacheid string `json:"cacheid"`
} `json:"page_input"`
} `json:"meta"`
}
//Get Contact by Email
func (a *ActiveCampaign) ContactGetByEmail(ctx context.Context, email string) (*FilteredContactResponse, error) {
// url := "https://youraccountname.api-us1.com/api/3/contacts?email=marcelwolf%40me.com&status=-1&orders[email]=ASC"
res, err := a.send(ctx, http.MethodGet, "contacts", &POF{QueryParams: []QueryParams{{
Key: "email",
Value: email,
}, {
Key: "status",
Value: "1",
}, {
Key: "orders[email]",
Value: "ASC",
}}}, nil)
if err != nil {
return nil, &Error{Op: "contact get by email", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode == http.StatusOK {
var contact FilteredContactResponse
err = json.NewDecoder(res.Body).Decode(&contact)
if err != nil {
return nil, err
}
return &contact, nil
}
var message struct {
Message string `json:"message"`
}
err = json.NewDecoder(res.Body).Decode(&message)
if err != nil {
return nil, &Error{Op: "contact get by email", Err: err}
}
return nil, errors.New("contact get by email: " + message.Message)
}
type ContactTag struct {
ContactId string `json:"contact"`
TagId string `json:"tag"`
}
type ContactTagResponse struct {
Contact string `json:"contact"`
Tag string `json:"tag"`
Cdate string `json:"cdate"`
ID string `json:"id"`
Links struct {
Contact string `json:"contact"`
Tag string `json:"tag"`
} `json:"links"`
}
func (a *ActiveCampaign) ContactTag(ctx context.Context, contactTag ContactTag) (*ContactTagResponse, error) {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
ContactTag ContactTag `json:"contactTag"`
}{
ContactTag: contactTag,
})
if err != nil {
return nil, &Error{Op: "contact tag", Err: err}
}
res, err := a.send(ctx, http.MethodPost, "contactTags", nil, b)
if err != nil {
return nil, &Error{Op: "contact tag", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode != http.StatusCreated {
return nil, errors.New("contact tag: " + res.Status)
}
var contactTagResponse struct {
ContactTag ContactTagResponse `json:"contactTag"`
}
err = json.NewDecoder(res.Body).Decode(&contactTagResponse)
if err != nil {
return nil, err
}
return &contactTagResponse.ContactTag, nil
}
func (a *ActiveCampaign) DeleteContactTag(ctx context.Context, contactTagId string) error {
res, err := a.send(ctx, http.MethodDelete, "contactTags/"+contactTagId, nil, nil)
if err != nil {
return &Error{Op: "delete contact tag", Err: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(res.Body)
if res.StatusCode != http.StatusOK {
return errors.New("delete contact tag: " + res.Status)
}
return nil
}