Skip to content

Instantly share code, notes, and snippets.

@spasiu
Last active November 19, 2019 20:56
Show Gist options
  • Select an option

  • Save spasiu/12dd76084932e85e026d2b0ba1db802b to your computer and use it in GitHub Desktop.

Select an option

Save spasiu/12dd76084932e85e026d2b0ba1db802b to your computer and use it in GitHub Desktop.
Script to rename a CSV full of Smooch apps
/*
How to:
Run this script with node and pass filepath and username + password as command line args:
node script.js "./YOUR_CSV_FILE" ACCOUNT_KEY ACCOUNT_SECRET
e.g.,
node script.js "./apps.csv" act_5964f00fc26cb5516693ebf5 cUQIis8kGtVZNg6eWoZrvk...
CSV file files should have two unlabeled columns, appId and name, e.g.,
5d76820de424c8001008f2ae, sweet potato
5ca624db00a5190010da755b, russet potato
...
Responses are logged as appId and status code or OK, e.g.,
5ca624db00a5190010da755b OK
5d76820de424c8001008f2ae OK
...
*/
const fs = require('fs');
const https = require('https');
const filepath = process.argv[2];
const username = process.argv[3];
const password = process.argv[4];
const token = Buffer.from(`${username}:${password}`).toString('base64');
fs.readFileSync(filepath)
.toString()
.split('\n')
.map(line => line.split(','))
.map(values => {
const appId = values[0] && values[0].trim();
const name = values[1] && values[1].trim();
if (appId && name) {
const data = JSON.stringify({ name });
const req = https.request({
hostname: 'api.smooch.io',
path: `/v1.1/apps/${appId}`,
method: 'PUT',
headers: {
'Authorization': `Basic ${token}`,
'Content-Type': 'application/json',
'Content-Length': data.length
}
}, res => console.log(`${appId} ${res.statusCode == 200 ? 'OK' : res.statusCode}`));
req.on('error', error => console.error(`${appId} ${error}`));
req.write(data);
req.end();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment