class PostSlackMessage

Public Instance Methods

post_basic_message(url, channel, content) click to toggle source
# File lib/pivotoolz/post_slack_message.rb, line 36
def post_basic_message(url, channel, content)
  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
end
post_message(url, channel, content) click to toggle source
# File lib/pivotoolz/post_slack_message.rb, line 16
def post_message(url, channel, content)
  begin
    text, *json = content.split("\n")
    return post_basic_message(url, channel, content) 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
      return "Error posting to slack #{e.message}:\n#{e.backtrace}"
    end
  rescue JSON::ParserError => e
    "Error parsing json: #{e}"
  end
end
select_webhook_url(url, channel_map, channel) click to toggle source
# File lib/pivotoolz/post_slack_message.rb, line 2
def select_webhook_url(url, channel_map, channel)
  return url if channel_map.empty?

  channel_webhook_map = channel_map.split(',').reduce({}) do |reduced, channel_map_string|
    c, url = channel_map_string.split('->')
    reduced[c] = url
    reduced["@#{c}"] = url if !c.start_with?('@')
    reduced["##{c}"] = url if !c.start_with?('#')
    reduced
  end

  channel_webhook_map[channel]
end