Sha256: 374df8bb72128631e0dd739b79ded7001f52920e3e9811592e144df2a66859df

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.strip.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.2 exe/post-slack-message