Skip to content

Instantly share code, notes, and snippets.

Last active September 13, 2018 14:07
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
squirt: upload a file to a slack channel from the cli
#!/usr/bin/env ruby
require 'json'
if ARGV.length < 3 then
puts "Usage: squirt <filename> <channel> <comment>"
token = ""
if token == "" then
puts "Get an API token at and enter it above"
filename = ARGV[0]
channel = ARGV[1]
comment = ARGV[2]
# get channel list
channel_raw_data = `curl -s -F token=#{token}`
channel_data = JSON.parse(channel_raw_data)
#find channel ID for channel name
channel_id = channel_data["channels"].map {|c| c['id'] if c['name']==channel}.compact.first
if not channel_id then
puts "Channel ID not found"
exit 1
output = `curl -s -F file=@#{filename} -F channels=#{channel_id} -F initial_comment=\"#{comment}\" -F token=#{token}`
result = JSON.parse(output)
if not result['ok'] then
puts "Upload failed: #{result['error']}"
exit 1
Copy link

lmarkus commented May 24, 2015

Copy link

arches commented Oct 14, 2015

wish I could submit a PR for a gist :) some filenames (eg anything with spaces) were being slightly munged as they pass through, so I added shell escaping:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment