-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (56 loc) · 1.52 KB
/
app.js
File metadata and controls
63 lines (56 loc) · 1.52 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
#!/usr/bin/env node
require('dotenv').config()
const Intercom = require('intercom-client')
const { promisify } = require('util')
const fs = require('fs')
const parse = require('csv-parse/lib/sync')
const readFile = promisify(fs.readFile)
const yargs = require('yargs')
// .usage('$0 <cmd> [args]')
.env('INTERCOM')
.options({
file: {
alias: 'f',
description: 'CSV file of email addresses',
demandOption: true
},
tags: {
alias: ['t', 'tag'],
description: 'Tags to apply',
demandOption: true,
array: true
},
token: {
description: 'Intercom access token (can also set env INTERCOM_TOKEN)',
demandOption: true
}
})
.help()
const argv = yargs.argv
process.on('unhandledRejection', (reason) => {
console.error(reason)
console.error('\n\n')
yargs.showHelp()
process.exit(1)
})
async function main () {
const client = new Intercom.Client({ token: argv.token })
const emailsToTag = parse(await readFile(argv.file)).map(([email]) => email)
const users = [] // <Array>{ id: <String> }
process.stdout.write('Paging... ')
let i = 0
await client.users.scroll.each({}, async (res) => {
i++; process.stdout.write(`${i}... `)
for (const user of res.body.users) {
if (emailsToTag.includes(user.email)) {
users.push({ id: user.id })
}
}
})
process.stdout.write('\n\n')
for (const name of argv.tags) {
await client.tags.tag({ name, users })
console.log(`tagged ${users.length} users with ${name}`)
}
}
main()