Sha256: 79d35c945fec268eec3162665c21f606f9b3af7e81c24413a166ff2fb420f510

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

#!/usr/bin/env ruby

require 'rest-client'
require 'json'

URL = ENV['SLACK_WEBHOOK_URL']

channel = ARGV.shift
content = ARGV.empty? ? ARGF.read : StringIO.new(ARGV.join("\n")).read
exit 0 if content.empty?

begin
  if attachments = JSON.parse(
    "[#{content.split("\n").slice(1..-1).join(",")}]"
  )
    begin
      RestClient.post(
        URL,
        payload: {
          username: ENV['SLACKBOT_USERNAME'] || 'slackbot',
          channel: channel,
          text: content.split("\n")&.first || '',
          attachments: attachments,
          icon_emoji: ":ghost:"
        }.to_json
      )
    rescue RestClient::Exceptions => e
      puts "Error posting to slack #{e.message}:\n#{e.backtrace}"
    end

    exit 0
  end
rescue JSON::ParserError => e
end

begin
  RestClient.post(
    URL,
    payload: {
      username: ENV['SLACKBOT_USERNAME'] || 'slackbot',
      channel: channel,
      text: content,
      icon_emoji: ":ghost:"
    }.to_json
  )
rescue RestClient::Exceptions => e
  puts "Error posting to slack #{e.message}:\n#{e.backtrace}"
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pivotoolz-1.2.0 exe/post-slack-message