-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcreate-schedule-broadcast.js
132 lines (107 loc) · 4.51 KB
/
create-schedule-broadcast.js
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
const fetch = require("node-fetch");
const credentials = require("./credentials.json");
const BASE_URL = 'https://api.aweber.com/1.0/';
const client_id = credentials['clientId'];
const client_secret = credentials['clientSecret'];
const accessToken = credentials['accessToken'];
const qs = require("querystring");
async function request(url,options) {
if (options == null) options = {};
if (options.credentials == null) options.credentials = 'same-origin';
return fetch(url, options).then(function(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
var error = new Error(response.statusText || response.status);
error.response = response;
return Promise.reject(error);
}
});
}
async function getCollection(accessToken,url) {
let res;
const collection = [];
while(url) {
res = await request(url,{
headers:{
"Authorization":`Bearer ${accessToken}`
}
});
let page = await res.json();
console.log({page});
collection.push(...page.entries);
url = page.next_collection_link;
}
return collection;
}
const data = {
// the broadcast html, this can be provided without body_text
'body_html' : '<html><body>An html version of my email</body></html>',
// if provided, this will be the plain text version of the email
// if this is not provided, it will be auto-generated based on the body_html
'body_text' : 'A plain text version of my email',
// this is the broadcast subject line
'subject' : 'This is an email created by the api!',
// if there are links in this broadcast, track them
'click_tracking_enabled' : 'true',
// include or exclude subscribers on other lists in this broadcast
// these are lists of URI's or list links
'exclude_lists' : [],
'include_lists' : [],
// this means the broadcast will be available via a url after it's sent
'is_archived' : 'true',
// if notifications are enabled and notify_on_send is True, send an email
// to the AWeber account holder when this broadcast stats' are available
'notify_on_send' : 'true',
};
(async () => {
// Get an account to search on
const accounts = await getCollection(accessToken, BASE_URL + 'accounts');
// Get a list to find broadcasts on
const lists = await getCollection( accessToken, accounts[0]['lists_collection_link']);
// if enabled, get the facebook url to share the broadcast to facebook
const integrations = await getCollection( accessToken, accounts[0]['integrations_collection_link']);
for (let integration of integrations) {
if (integration['service_name'].toLowerCase() == 'facebook') {
// there could be multiple, so pick the first one we find and break
data['facebook_integration'] = integration['self_link'];
break;
}
}
// make the broadcast on the first list
const broadcastResponse = await request(`${lists[0]['self_link']}/broadcasts`,{
// this may need to be form encoded.
method:"POST",
body:qs.stringify(data),
'headers' : {
'Authorization' : 'Bearer ' + accessToken,
"Content-Type":"application/x-www-form-urlencoded"
}
});
const broadcast = await broadcastResponse.json();
console.log({broadcast})
// schedule the broadcast we made
const timestamp = new Date();
console.log({timestamp});
const scheduledFor = timestamp.toISOString(); // must be iso8601 compliant
console.log({scheduledFor});
console.log("self link",broadcast['self_link']);
const scheduleResponse = await request(`${broadcast['self_link']}/schedule`, {
method:"POST",
body:qs.stringify({'scheduled_for' : scheduledFor}),
'headers' :{
'Authorization' : 'Bearer ' + accessToken,
"Content-Type":"application/x-www-form-urlencoded"
}
});
// retrieve the scheduled broadcast to see the updated scheduled_for
const scheduledResponse = await request(broadcast['self_link'], {
headers:{
'Authorization' : 'Bearer ' + accessToken
}
});
const scheduledBroadcast = await scheduledResponse.json();
console.log({scheduledBroadcast});
console.log( `Scheduled broadcast subject: ${scheduledBroadcast['subject']} on list: ${lists[0]['name']}`);
console.log (` to be sent at: ${scheduledBroadcast['scheduled_for']}`);
})();