Skip to content

Instantly share code, notes, and snippets.

@arches
Forked from foozmeat/squirt
Last active October 13, 2016 15:22
Show Gist options
  • Save arches/83799b98d2fff056d5c8 to your computer and use it in GitHub Desktop.
Save arches/83799b98d2fff056d5c8 to your computer and use it in GitHub Desktop.
squirt: upload a file to a slack channel from the cli
#!/usr/bin/env ruby
require 'json'
require 'shellwords'
if ARGV.length < 3 then
puts "Usage: squirt <filename> <channel> <comment>"
exit
end
token = ""
if token == "" then
puts "Get an API token at https://api.slack.com/web#basics and enter it above"
exit
end
filename = ARGV[0]
channel = ARGV[1]
comment = ARGV[2]
# get channel list
channel_raw_data = `curl -s -F token=#{token} https://slack.com/api/channels.list`
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
end
output = `curl -s -F file=@#{Shellwords.escape filename} -F channels=#{channel_id} -F initial_comment=\"#{comment}\" -F token=#{token} https://slack.com/api/files.upload`
result = JSON.parse(output)
if not result['ok'] then
puts "Upload failed: #{result['error']}"
exit 1
end
@sexygeoff
Copy link

sexygeoff commented Oct 13, 2016

This is supper useful, thanks. How does one @mention a user? When I use the @ symbol on the comment argument, I get this error. A JSON text must at least contain two octets!. Escaping @ just sends a literal "@" and nobody is mentioned. Thanks!

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