Simple Ruby script that sends a slack message. Like Slackcat or others, but I needed more options. Uses slack-notifier gem.
YMMV.
| #!/usr/bin/env ruby | |
| require 'optparse' | |
| require 'uri' | |
| require 'net/https' | |
| require 'slack-notifier' | |
| SLACK_WEBHOOK_URL='' | |
| @slack_channel='#notifications' | |
| @slack_username= Socket.gethostname | |
| @slack_icon="" | |
| @msg = "" | |
| def parse_args(args = {}) | |
| @print = args[:print].nil? ? true : args[:print] | |
| @exit = args[:exit].nil? ? true : args[:exit] | |
| # now, handle the arguments... | |
| @opts = OptionParser.new | |
| @opts.banner = "Usage: #{File.basename($0)} -C '#{@slack_channel}' -U '#{@slack_username}' -I '#{@slack_icon}' 'This gets posted to Slack!'" | |
| @opts.on('-C', "--channel CHANNEL", "CHANNEL is required") do |channel| | |
| raise OptionParser::MissingArgument if channel.empty? | |
| @slack_channel = channel if channel.start_with?('#','@') | |
| end | |
| @opts.on("-U", "--username [USERNAME]", "Username to post as") do |user| | |
| @slack_username = user | |
| end | |
| @opts.on("-I", "--icon [ICON]", "Avatar to use") do |icon| | |
| @slack_icon = icon if (icon.start_with?(':') && icon.end_with?(':')) | |
| end | |
| @opts.on_tail("-h", "--help", "Show this message") do | |
| puts @opts | |
| exit | |
| end | |
| @opts.parse! | |
| @msg = ARGV.join(" ") | |
| end | |
| def send_slack_message(message) | |
| notifier = Slack::Notifier.new SLACK_WEBHOOK_URL, channel: @slack_channel, username: @slack_username | |
| notifier.ping "#{message}", icon_emoji: @slack_icon | |
| end | |
| parse_args() | |
| send_slack_message(@msg) |