Sha256: 03f6b98d51dfdc19a4ddd5d15d100352dd08a82a16e4ba7168dceba7fb53029c

Contents?: true

Size: 1017 Bytes

Versions: 1

Compression:

Stored size: 1017 Bytes

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
  text, *json = content.split("\n")
  if !json.empty?
    post_data = json.any? { |s| s.include? 'blocks' } ? JSON.parse(json.first) : {attachments: json.map { |j| JSON.parse(j) }}
    begin
      RestClient.post(
        URL,
        post_data.merge({text: text || ''}).to_json,
        {content_type: :json, accept: :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.3.0 exe/post-slack-message