forked from hanzoai/gochimp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automations.go
430 lines (341 loc) · 12.9 KB
/
automations.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
package gochimp3
import (
"errors"
"fmt"
)
const (
automations_path = "/automations"
single_automation_path = automations_path + "/%s"
pause_all_emails_path = single_automation_path + "/actions/pause-all-emails"
start_all_emails_path = single_automation_path + "/actions/start-all-emails"
automation_email_path = single_automation_path + "/emails"
single_automation_email_path = automation_email_path + "/%s"
pause_single_email_path = single_automation_email_path + "/actions/pause"
start_single_email_path = single_automation_email_path + "/actions/start"
automation_queues_path = single_automation_email_path + "/queue"
single_automation_queue_path = automation_queues_path + "/%s"
removed_subscribers_automation_path = single_automation_path + "/removed-subscribers"
)
type ListOfAutomations struct {
baseList
Automations []Automation `json:"automations"`
}
type Automation struct {
ID string `json:"id"`
CreateTime string `json:"create_time"`
StartTime string `json:"start_time"`
Status string `json:"status"`
EmailsSent int `json:"emails_sent"`
Recipients AutomationRecipient `json:"recipients"`
Settings AutomationSettingsShort `json:"settings"`
Tracking AutomationTracking `json:"tracking"`
TriggerSettings WorkflowType `json:"trigger_settings"`
ReportSummary ReportSummary `json:"report_summary"`
withLinks
api *API
}
type AutomationRecipient struct {
ListID string `json:"list_id"`
SegmentOptions AutomationOptions `json:"segment_options"`
}
type AutomationOptions struct {
SavedSegmentID int `json:"saved_segment_id"`
Match string `json:"match"`
Conditions []SegmentConditional `json:"conditions"`
}
type AutomationSettingsShort struct {
UseConversation bool `json:"use_conversation"`
ToName string `json:"to_name"`
Title string `json:"title"`
FromName string `json:"from_name"`
ReplyTo string `json:"reply_to"`
Authenticate bool `json:"authenticate"`
AutoFooter bool `json:"auto_footer"`
InlineCSS bool `json:"inline_css"`
}
type AutomationSettingsLong struct {
Title string `json:"title"`
FromName string `json:"from_name"`
ReplyTo string `json:"reply_to"`
Authenticate bool `json:"authenticate"`
AutoFooter bool `json:"auto_footer"`
InlineCSS bool `json:"inline_css"`
SubjectLine string `json:"subject_line"`
AutoTweet bool `json:"auto_tweet"`
AutoFBPost []string `json:"auto_fb_post"`
FBComments bool `json:"fb_comments"`
TemplateID int `json:"template_id"`
DragAndDrop bool `json:"drag_and_drop"`
}
type AutomationTracking struct {
Opens bool `json:"opens"`
HTMLClicks bool `json:"html_clicks"`
TextClicks bool `json:"text_clicks"`
GoalTracking bool `json:"goal_tracking"`
Ecomm360 bool `json:"ecomm360"`
GoogleAnalytics string `json:"google_analytics"`
Clicktale string `json:"clicktale"`
Salesforce Salesforce `json:"salesforce"`
Highrise Highrise `json:"highrise"`
Capsule Capsule `json:"capsule"`
}
type Salesforce struct {
Campaign bool `json:"campaign"`
Notes bool `json:"notes"`
}
type Highrise struct {
Campaign bool `json:"campaign"`
Notes bool `json:"notes"`
}
type Capsule struct {
Notes bool `json:"notes"`
}
type ReportSummary struct {
Opens int `json:"opens"`
UniqueOpens int `json:"unique_opens"`
OpenRate float64 `json:"open_rate"`
Clicks int `json:"clicks"`
SubscriberClicks int `json:"subscriber_clicks"`
ClickRate float64 `json:"click_rate"`
}
func (auto Automation) CanMakeRequest() error {
if auto.ID == "" {
return errors.New("No ID provided")
}
return nil
}
func (api API) GetAutomations(params *BasicQueryParams) (*ListOfAutomations, error) {
response := new(ListOfAutomations)
err := api.Request("GET", automations_path, params, nil, response)
if err != nil {
return nil, err
}
for _, l := range response.Automations {
l.api = &api
}
return response, nil
}
// TODO query params?
func (api API) GetAutomation(id string) (*Automation, error) {
endpoint := fmt.Sprintf(single_automation_path, id)
response := new(Automation)
response.api = &api
return response, api.Request("GET", endpoint, nil, nil, response)
}
// ------------------------------------------------------------------------------------------------
// Actions for Sending Emails
// ------------------------------------------------------------------------------------------------
func (auto Automation) PauseSendingAll() (bool, error) {
if err := auto.CanMakeRequest(); err != nil {
return false, err
}
return auto.api.PauseSendingAll(auto.ID)
}
func (api API) PauseSendingAll(id string) (bool, error) {
endpoint := fmt.Sprintf(pause_all_emails_path, id)
return api.RequestOk("POST", endpoint)
}
func (auto Automation) StartSendingAll() (bool, error) {
if err := auto.CanMakeRequest(); err != nil {
return false, err
}
return auto.api.StartSendingAll(auto.ID)
}
func (api API) StartSendingAll(id string) (bool, error) {
endpoint := fmt.Sprintf(start_all_emails_path, id)
return api.RequestOk("POST", endpoint)
}
func (email AutomationEmail) PauseSending() (bool, error) {
return email.api.PauseSending(email.WorkflowID, email.ID)
}
func (api API) PauseSending(workflowID, emailID string) (bool, error) {
endpoint := fmt.Sprintf(pause_single_email_path, workflowID, emailID)
return api.RequestOk("POST", endpoint)
}
func (email AutomationEmail) StartSending() (bool, error) {
return email.api.StartSending(email.WorkflowID, email.ID)
}
func (api API) StartSending(workflowID, emailID string) (bool, error) {
endpoint := fmt.Sprintf(start_single_email_path, workflowID, emailID)
return api.RequestOk("POST", endpoint)
}
// ------------------------------------------------------------------------------------------------
// Automation Emails
// ------------------------------------------------------------------------------------------------
type ListOfEmails struct {
baseList
Emails []AutomationEmail `json:"emails"`
}
type AutomationEmail struct {
ID string `json:"id"`
WorkflowID string `json:"workflow_id"`
Position int `json:"position"`
Delay AutomationDelay `json:"delay"`
CreateTime string `json:"create_time"`
StartTime string `json:"start_time"`
ArchiveURL string `json:"archive_url"`
Status string `json:"status"`
EmailsSent int `json:"emails_sent"`
SendTime string `json:"send_time"`
ContentType string `json:"content_type"`
Recipients AutomationRecipient `json:"recipients"`
Settings AutomationSettingsLong `json:"settings"`
Tracking AutomationTracking `json:"tracking"`
SocialCard SocialCard `json:"social_card"`
TriggerSettings WorkflowType `json:"trigger_settings"`
ReportSummary ReportSummary `json:"report_summary"`
withLinks
api *API
}
type SocialCard struct {
ImageURL string `json:"image_url"`
Description string `json:"description"`
Title string `json:"title"`
}
type AutomationDelay struct {
Amount int `json:"amount"`
Type string `json:"type"`
Direction string `json:"direction"`
Action string `json:"action"`
}
func (email AutomationEmail) CanMakeRequest() error {
if email.ID == "" {
return errors.New("No ID provided")
}
return nil
}
func (auto Automation) GetEmails() (*ListOfEmails, error) {
if err := auto.CanMakeRequest(); err != nil {
return nil, err
}
return auto.api.GetAutomationEmails(auto.ID)
}
func (api API) GetAutomationEmails(automationID string) (*ListOfEmails, error) {
endpoint := fmt.Sprintf(automation_email_path, automationID)
response := new(ListOfEmails)
for _, l := range response.Emails {
l.api = &api
}
return response, api.Request("GET", endpoint, nil, nil, response)
}
func (auto Automation) GetEmail(id string) (*AutomationEmail, error) {
if err := auto.CanMakeRequest(); err != nil {
return nil, err
}
return auto.api.GetAutomationEmail(auto.ID, id)
}
func (api API) GetAutomationEmail(automationID, emailID string) (*AutomationEmail, error) {
endpoint := fmt.Sprintf(single_automation_email_path, automationID, emailID)
response := new(AutomationEmail)
response.api = &api
return response, api.Request("GET", endpoint, nil, nil, response)
}
// ------------------------------------------------------------------------------------------------
// Queues
// ------------------------------------------------------------------------------------------------
type AutomationQueueRequest struct {
EmailAddress string `json:"email_address"`
}
type ListOfAutomationQueues struct {
baseList
WorkflowID string `json:"workflow_id"`
EmailID string `json:"email_id"`
Queues []AutomationQueue `json:"queue"`
}
type AutomationQueue struct {
ID string `json:"id"`
WorkflowID string `json:"workflow_id"`
EmailID string `json:"email_id"`
ListID string `json:"list_id"`
EmailAddress string `json:"email_address"`
NextSend string `json:"next_send"`
withLinks
api *API
}
func (email AutomationEmail) GetQueues() (*ListOfAutomationQueues, error) {
if err := email.CanMakeRequest(); err != nil {
return nil, err
}
return email.api.GetAutomationQueues(email.WorkflowID, email.ID)
}
func (api API) GetAutomationQueues(workflowID, emailID string) (*ListOfAutomationQueues, error) {
endpoint := fmt.Sprintf(automation_queues_path, workflowID, emailID)
response := new(ListOfAutomationQueues)
for _, l := range response.Queues {
l.api = &api
}
return response, api.Request("GET", endpoint, nil, nil, response)
}
func (email AutomationEmail) GetQueue(id string) (*AutomationQueue, error) {
if err := email.CanMakeRequest(); err != nil {
return nil, err
}
return email.api.GetAutomationQueue(email.WorkflowID, email.ID, id)
}
func (api API) GetAutomationQueue(workflowID, emailID, subsID string) (*AutomationQueue, error) {
endpoint := fmt.Sprintf(single_automation_queue_path, workflowID, emailID, subsID)
response := new(AutomationQueue)
response.api = &api
return response, api.Request("GET", endpoint, nil, nil, response)
}
func (email AutomationEmail) CreateQueue(emailAddress string) (*AutomationQueue, error) {
if err := email.CanMakeRequest(); err != nil {
return nil, err
}
return email.api.CreateAutomationEmailQueue(email.WorkflowID, email.ID, emailAddress)
}
func (api API) CreateAutomationEmailQueue(workflowID, emailID, emailAddress string) (*AutomationQueue, error) {
endpoint := fmt.Sprintf(automation_queues_path, workflowID, emailID)
response := new(AutomationQueue)
body := &AutomationQueueRequest{
EmailAddress: emailAddress,
}
err := api.Request("POST", endpoint, nil, body, response)
if err != nil {
return nil, err
}
return response, nil
}
// ------------------------------------------------------------------------------------------------
// Removed Subscribers
// ------------------------------------------------------------------------------------------------
type RemovedSubscriberRequest struct {
EmailAddress string `json:"email_address"`
}
type ListOfRemovedSubscribers struct {
baseList
WorkflowID string `json:"workflow_id"`
Subscribers []RemovedSubscriber `json:"subscribers"`
}
type RemovedSubscriber struct {
ID string `json:"id"`
WorkflowID string `json:"workflow_id"`
ListID string `json:"list_id"`
EmailAddress string `json:"email_address"`
withLinks
}
func (auto Automation) GetRemovedSubscribers() (*ListOfRemovedSubscribers, error) {
if err := auto.CanMakeRequest(); err != nil {
return nil, err
}
return auto.api.GetAutomationRemovedSubscribers(auto.ID)
}
func (api API) GetAutomationRemovedSubscribers(workflowID string) (*ListOfRemovedSubscribers, error) {
endpoint := fmt.Sprintf(removed_subscribers_automation_path, workflowID)
response := new(ListOfRemovedSubscribers)
return response, api.Request("GET", endpoint, nil, nil, response)
}
func (auto Automation) CreateRemovedSubscribers(emailAddress string) (*RemovedSubscriber, error) {
if err := auto.CanMakeRequest(); err != nil {
return nil, err
}
return auto.api.CreateAutomationRemovedSubscribers(auto.ID, emailAddress)
}
func (api API) CreateAutomationRemovedSubscribers(workflowID, emailAddress string) (*RemovedSubscriber, error) {
endpoint := fmt.Sprintf(removed_subscribers_automation_path, workflowID)
response := new(RemovedSubscriber)
body := &RemovedSubscriberRequest{
EmailAddress: emailAddress,
}
return response, api.Request("POST", endpoint, nil, body, response)
}