Create a gist now

Instantly share code, notes, and snippets.

@lmarkus /squirt
Last active Dec 6, 2017

What would you like to do?
SquirtJS: Upload a file to Slack from the CLI using NodeJS.
#!/usr/bin/env node
* Created by lmarkus on 5/23/15.
* Adapted from
'use strict';
var assert = require('assert'),
exec = require('child_process').execFileSync,
util = require('util'),
curl_json = function(command) {return JSON.parse(exec(command.shift(), command).toString());},
filename = process.argv[2],
channelName = process.argv[3],
comment = process.argv[4],
token = ''; //Insert token here.
assert.notStrictEqual(token, '', 'Get an API token at and enter it above');
assert.strictEqual(process.argv.length, 5, 'Usage: squirt <filename> <channelName> <comment>');
//Get channel list
var getChannels = util.format('curl -s -F token=%s', token).split(' ');
var channelData = curl_json(getChannels);
//Find channel ID for channel name
var channel = channelData.channels.filter(function (c) { return === channelName });
var channelId = channel && channel.length > 0 && channel[0].id;
assert.ok(channelId, 'Channel Id not found');
var upload = util.format('curl -s -F file=@%s -F channels=%s -F initial_comment=%s -F token=%s'.replace(/ /g, '\t'), filename, channelId, comment, token).split('\t');
var result = curl_json(upload);
assert.ok(result.ok, 'Upload Failed: ' + result.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment