-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathactivecampaign.js
More file actions
executable file
·435 lines (415 loc) · 15.4 KB
/
Copy pathactivecampaign.js
File metadata and controls
executable file
·435 lines (415 loc) · 15.4 KB
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
#!/usr/bin/env node
const API_KEY = process.env.ACTIVECAMPAIGN_API_KEY
const API_URL = process.env.ACTIVECAMPAIGN_API_URL
if (!API_KEY) {
console.error(JSON.stringify({ error: 'ACTIVECAMPAIGN_API_KEY environment variable required' }))
process.exit(1)
}
if (!API_URL) {
console.error(JSON.stringify({ error: 'ACTIVECAMPAIGN_API_URL environment variable required (e.g. https://yourname.api-us1.com)' }))
process.exit(1)
}
const BASE_URL = `${API_URL.replace(/\/$/, '')}/api/3`
async function api(method, path, body) {
if (args['dry-run']) {
return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { 'Api-Token': '***', 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body || undefined }
}
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers: {
'Api-Token': API_KEY,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
})
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
function parseArgs(args) {
const result = { _: [] }
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const next = args[i + 1]
if (next && !next.startsWith('--')) {
result[key] = next
i++
} else {
result[key] = true
}
} else {
result._.push(arg)
}
}
return result
}
const args = parseArgs(process.argv.slice(2))
const [cmd, sub, ...rest] = args._
async function main() {
let result
const limit = args.limit ? Number(args.limit) : 20
const offset = args.offset ? Number(args.offset) : 0
switch (cmd) {
case 'contacts':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
if (args.email) params.set('email', args.email)
if (args.search) params.set('search', args.search)
if (args['list-id']) params.set('listid', args['list-id'])
if (args.status) params.set('status', args.status)
result = await api('GET', `/contacts?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/contacts/${id}`)
break
}
case 'create': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
const contact = { email }
if (args['first-name']) contact.firstName = args['first-name']
if (args['last-name']) contact.lastName = args['last-name']
if (args.phone) contact.phone = args.phone
result = await api('POST', '/contacts', { contact })
break
}
case 'update': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const contact = {}
if (args.email) contact.email = args.email
if (args['first-name']) contact.firstName = args['first-name']
if (args['last-name']) contact.lastName = args['last-name']
if (args.phone) contact.phone = args.phone
result = await api('PUT', `/contacts/${id}`, { contact })
break
}
case 'delete': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('DELETE', `/contacts/${id}`)
break
}
case 'sync': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
const contact = { email }
if (args['first-name']) contact.firstName = args['first-name']
if (args['last-name']) contact.lastName = args['last-name']
if (args.phone) contact.phone = args.phone
result = await api('POST', '/contact/sync', { contact })
break
}
default:
result = { error: 'Unknown contacts subcommand. Use: list, get, create, update, delete, sync' }
}
break
case 'lists':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
result = await api('GET', `/lists?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/lists/${id}`)
break
}
case 'create': {
const name = args.name
if (!name) { result = { error: '--name required' }; break }
const list = { name }
if (args['string-id']) list.stringid = args['string-id']
if (args['sender-url']) list.sender_url = args['sender-url']
if (args['sender-reminder']) list.sender_reminder = args['sender-reminder']
result = await api('POST', '/lists', { list })
break
}
case 'delete': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('DELETE', `/lists/${id}`)
break
}
case 'subscribe': {
const listId = args['list-id'] || args.id
const contactId = args['contact-id']
if (!listId) { result = { error: '--list-id required' }; break }
if (!contactId) { result = { error: '--contact-id required' }; break }
result = await api('POST', '/contactLists', {
contactList: { list: listId, contact: contactId, status: 1 }
})
break
}
case 'unsubscribe': {
const listId = args['list-id'] || args.id
const contactId = args['contact-id']
if (!listId) { result = { error: '--list-id required' }; break }
if (!contactId) { result = { error: '--contact-id required' }; break }
result = await api('POST', '/contactLists', {
contactList: { list: listId, contact: contactId, status: 2 }
})
break
}
default:
result = { error: 'Unknown lists subcommand. Use: list, get, create, delete, subscribe, unsubscribe' }
}
break
case 'campaigns':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
result = await api('GET', `/campaigns?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/campaigns/${id}`)
break
}
default:
result = { error: 'Unknown campaigns subcommand. Use: list, get' }
}
break
case 'deals':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
if (args.search) params.set('search', args.search)
if (args.stage) params.set('filters[stage]', args.stage)
if (args.owner) params.set('filters[owner]', args.owner)
result = await api('GET', `/deals?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/deals/${id}`)
break
}
case 'create': {
const title = args.title
if (!title) { result = { error: '--title required' }; break }
const deal = { title }
if (args.value) deal.value = Number(args.value)
if (args.currency) deal.currency = args.currency
if (args.pipeline) deal.group = args.pipeline
if (args.stage) deal.stage = args.stage
if (args.owner) deal.owner = args.owner
if (args['contact-id']) deal.contact = args['contact-id']
result = await api('POST', '/deals', { deal })
break
}
case 'update': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const deal = {}
if (args.title) deal.title = args.title
if (args.value) deal.value = Number(args.value)
if (args.stage) deal.stage = args.stage
if (args.owner) deal.owner = args.owner
if (args.status) deal.status = Number(args.status)
result = await api('PUT', `/deals/${id}`, { deal })
break
}
case 'delete': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('DELETE', `/deals/${id}`)
break
}
default:
result = { error: 'Unknown deals subcommand. Use: list, get, create, update, delete' }
}
break
case 'automations':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
result = await api('GET', `/automations?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/automations/${id}`)
break
}
case 'add-contact': {
const automationId = args.id
const contactId = args['contact-id']
if (!automationId) { result = { error: '--id required (automation ID)' }; break }
if (!contactId) { result = { error: '--contact-id required (contact ID, not email)' }; break }
result = await api('POST', '/contactAutomations', {
contactAutomation: { contact: contactId, automation: automationId }
})
break
}
default:
result = { error: 'Unknown automations subcommand. Use: list, get, add-contact' }
}
break
case 'tags':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
if (args.search) params.set('search', args.search)
result = await api('GET', `/tags?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/tags/${id}`)
break
}
case 'create': {
const name = args.name
if (!name) { result = { error: '--name required' }; break }
result = await api('POST', '/tags', {
tag: { tag: name, tagType: args.type || 'contact' }
})
break
}
case 'delete': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('DELETE', `/tags/${id}`)
break
}
case 'add-to-contact': {
const tagId = args['tag-id']
const contactId = args['contact-id']
if (!tagId) { result = { error: '--tag-id required' }; break }
if (!contactId) { result = { error: '--contact-id required' }; break }
result = await api('POST', '/contactTags', {
contactTag: { contact: contactId, tag: tagId }
})
break
}
case 'remove-from-contact': {
const contactTagId = args.id
if (!contactTagId) { result = { error: '--id required (contactTag ID)' }; break }
result = await api('DELETE', `/contactTags/${contactTagId}`)
break
}
default:
result = { error: 'Unknown tags subcommand. Use: list, get, create, delete, add-to-contact, remove-from-contact' }
}
break
case 'pipelines':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
result = await api('GET', `/dealGroups?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/dealGroups/${id}`)
break
}
default:
result = { error: 'Unknown pipelines subcommand. Use: list, get' }
}
break
case 'webhooks':
switch (sub) {
case 'list': {
const params = new URLSearchParams()
params.set('limit', String(limit))
params.set('offset', String(offset))
result = await api('GET', `/webhooks?${params.toString()}`)
break
}
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/webhooks/${id}`)
break
}
case 'create': {
const name = args.name
const url = args.url
if (!name) { result = { error: '--name required' }; break }
if (!url) { result = { error: '--url required' }; break }
const events = args.events?.split(',') || ['subscribe']
const sources = args.sources?.split(',') || ['public', 'admin', 'api', 'system']
result = await api('POST', '/webhooks', {
webhook: { name, url, events, sources }
})
break
}
case 'delete': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('DELETE', `/webhooks/${id}`)
break
}
default:
result = { error: 'Unknown webhooks subcommand. Use: list, get, create, delete' }
}
break
case 'users':
switch (sub) {
case 'me':
result = await api('GET', '/users/me')
break
case 'list':
result = await api('GET', '/users')
break
default:
result = { error: 'Unknown users subcommand. Use: me, list' }
}
break
default:
result = {
error: 'Unknown command',
usage: {
contacts: 'contacts [list | get --id <id> | create --email <email> | update --id <id> | delete --id <id> | sync --email <email>]',
lists: 'lists [list | get --id <id> | create --name <name> | delete --id <id> | subscribe --list-id <lid> --contact-id <cid> | unsubscribe --list-id <lid> --contact-id <cid>]',
campaigns: 'campaigns [list | get --id <id>]',
deals: 'deals [list | get --id <id> | create --title <title> | update --id <id> | delete --id <id>]',
automations: 'automations [list | get --id <id> | add-contact --id <aid> --email <email>]',
tags: 'tags [list | get --id <id> | create --name <name> | delete --id <id> | add-to-contact --tag-id <tid> --contact-id <cid>]',
pipelines: 'pipelines [list | get --id <id>]',
webhooks: 'webhooks [list | get --id <id> | create --name <name> --url <url> | delete --id <id>]',
users: 'users [me | list]',
options: '--limit <n> --offset <n> --search <query> --email <email>',
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})