#!/usr/bin/env ruby require 'optparse' require 'slackit' # -------------------------------------------------------------------------------- # # Send Mssage to Slack # # -------------------------------------------------------------------------------- # # This function will take the input arguments and then send the message. # # -------------------------------------------------------------------------------- # def send_message_to_slack(options) s = Slackit.new(options[:webhook], options[:channel], options[:username], options[:emoji]) s.send(options[:message]) end # -------------------------------------------------------------------------------- # # Process Arguments # # -------------------------------------------------------------------------------- # # This function will process the input from the command line and work out what it # # is that the user wants to see. # # # # This is the main processing function where all the processing logic is handled. # # -------------------------------------------------------------------------------- # def process_arguments options = {} # Enforce the presence of mandatory = %I[webhook message] optparse = OptionParser.new do |opts| opts.banner = "Usage: #{$PROGRAM_NAME}" opts.on('-h', '--help', 'Display this screen') do puts opts exit(1) end opts.on('-c', '--channel string', 'The channel to send the message to') do |channel| options[:channel] = channel end opts.on('-e', '--emoji string', 'The emoji to use [default: :wolf:]') do |emoji| options[:emoji] = emoji end opts.on('-m', '--message string', 'The message to send') do |message| options[:message] = message end opts.on('-w', '--webhook string', 'The slack incoming webhook to use') do |webhook| options[:webhook] = webhook end opts.on('-u', '--username string', 'The username to send as [default: slackit]') do |username| options[:username] = username end end begin optparse.parse! missing = mandatory.select { |param| options[param].nil? } raise OptionParser::MissingArgument.new(missing.join(', ')) unless missing.empty? rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e puts e.to_s puts optparse exit end exit 0 if send_message_to_slack(options) exit 1 end # -------------------------------------------------------------------------------- # # Main() # # -------------------------------------------------------------------------------- # # The main function where all of the heavy lifting and script config is done. # # -------------------------------------------------------------------------------- # def main process_arguments end main # -------------------------------------------------------------------------------- # # End of Script # # -------------------------------------------------------------------------------- # # This is the end - nothing more to see here. # # -------------------------------------------------------------------------------- #