-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathcustomer-io.js
More file actions
executable file
·205 lines (189 loc) · 7.16 KB
/
Copy pathcustomer-io.js
File metadata and controls
executable file
·205 lines (189 loc) · 7.16 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
#!/usr/bin/env node
const APP_KEY = process.env.CUSTOMERIO_APP_KEY
const SITE_ID = process.env.CUSTOMERIO_SITE_ID
const API_KEY = process.env.CUSTOMERIO_API_KEY
const TRACK_URL = 'https://track.customer.io/api/v1'
const APP_URL = 'https://api.customer.io/v1'
const hasTrackAuth = SITE_ID && API_KEY
const hasAppAuth = APP_KEY
if (!hasTrackAuth && !hasAppAuth) {
console.error(JSON.stringify({ error: 'CUSTOMERIO_APP_KEY (for App API) or CUSTOMERIO_SITE_ID + CUSTOMERIO_API_KEY (for Track API) environment variables required' }))
process.exit(1)
}
const basicAuth = hasTrackAuth ? Buffer.from(`${SITE_ID}:${API_KEY}`).toString('base64') : null
async function trackApi(method, path, body) {
if (!hasTrackAuth) {
return { error: 'Track API requires CUSTOMERIO_SITE_ID and CUSTOMERIO_API_KEY environment variables' }
}
if (args['dry-run']) {
return { _dry_run: true, method, url: `${TRACK_URL}${path}`, headers: { Authorization: '***', 'Content-Type': 'application/json' }, body: body || undefined }
}
const res = await fetch(`${TRACK_URL}${path}`, {
method,
headers: {
'Authorization': `Basic ${basicAuth}`,
'Content-Type': '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 }
}
}
async function appApi(method, path, body) {
if (!hasAppAuth) {
return { error: 'App API requires CUSTOMERIO_APP_KEY environment variable' }
}
if (args['dry-run']) {
return { _dry_run: true, method, url: `${APP_URL}${path}`, headers: { Authorization: '***', 'Content-Type': 'application/json' }, body: body || undefined }
}
const res = await fetch(`${APP_URL}${path}`, {
method,
headers: {
'Authorization': `Bearer ${APP_KEY}`,
'Content-Type': '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
switch (cmd) {
case 'customers':
switch (sub) {
case 'identify': {
const customerId = rest[0] || args.id
if (!customerId) { result = { error: 'Customer ID required (positional arg or --id)' }; break }
const body = {}
if (args.email) body.email = args.email
if (args['first-name']) body.first_name = args['first-name']
if (args['last-name']) body.last_name = args['last-name']
if (args['created-at']) body.created_at = parseInt(args['created-at'])
if (args.plan) body.plan = args.plan
if (args.data) Object.assign(body, JSON.parse(args.data))
result = await trackApi('PUT', `/customers/${customerId}`, body)
break
}
case 'get': {
const customerId = rest[0] || args.id
if (!customerId) { result = { error: 'Customer ID required (positional arg or --id)' }; break }
result = await appApi('GET', `/customers/${customerId}/attributes`)
break
}
case 'delete': {
const customerId = rest[0] || args.id
if (!customerId) { result = { error: 'Customer ID required (positional arg or --id)' }; break }
result = await trackApi('DELETE', `/customers/${customerId}`)
break
}
case 'track-event': {
const customerId = rest[0] || args.id
if (!customerId) { result = { error: 'Customer ID required (positional arg or --id)' }; break }
if (!args.name) { result = { error: '--name required (event name)' }; break }
const body = { name: args.name }
if (args.data) body.data = JSON.parse(args.data)
result = await trackApi('POST', `/customers/${customerId}/events`, body)
break
}
default:
result = { error: 'Unknown customers subcommand. Use: identify, get, delete, track-event' }
}
break
case 'campaigns':
switch (sub) {
case 'list':
result = await appApi('GET', '/campaigns')
break
case 'get': {
const campaignId = rest[0] || args.id
if (!campaignId) { result = { error: 'Campaign ID required (positional arg or --id)' }; break }
result = await appApi('GET', `/campaigns/${campaignId}`)
break
}
case 'metrics': {
const campaignId = rest[0] || args.id
if (!campaignId) { result = { error: 'Campaign ID required (positional arg or --id)' }; break }
result = await appApi('GET', `/campaigns/${campaignId}/metrics`)
break
}
case 'trigger': {
const campaignId = rest[0] || args.id
if (!campaignId) { result = { error: 'Campaign ID required (positional arg or --id)' }; break }
const body = {}
if (args.emails) body.emails = args.emails.split(',')
if (args.ids) body.ids = args.ids.split(',')
if (args.data) body.data = JSON.parse(args.data)
result = await appApi('POST', `/campaigns/${campaignId}/triggers`, body)
break
}
default:
result = { error: 'Unknown campaigns subcommand. Use: list, get, metrics, trigger' }
}
break
case 'send':
switch (sub) {
case 'email': {
const body = {
transactional_message_id: args['message-id'],
to: args.to,
identifiers: {},
}
if (args['identifier-id']) body.identifiers.id = args['identifier-id']
if (args['identifier-email']) body.identifiers.email = args['identifier-email']
if (args.data) body.message_data = JSON.parse(args.data)
if (args.from) body.from = args.from
if (args.subject) body.subject = args.subject
if (args.body) body.body = args.body
result = await appApi('POST', '/send/email', body)
break
}
default:
result = { error: 'Unknown send subcommand. Use: email' }
}
break
default:
result = {
error: 'Unknown command',
usage: {
customers: 'customers [identify|get|delete|track-event] <customer_id> [--email <email>] [--first-name <name>] [--plan <plan>] [--data <json>] [--name <event>]',
campaigns: 'campaigns [list|get|metrics|trigger] [campaign_id] [--emails <e1,e2>] [--ids <id1,id2>] [--data <json>]',
send: 'send email --message-id <id> --to <email> [--identifier-id <id>] [--identifier-email <email>] [--data <json>]',
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})