Sha256: 9ebce7af280f626d5c35755f66857781d9a67e13b1e17a9610a084d39551cdd7
Contents?: true
Size: 1.47 KB
Versions: 7
Compression:
Stored size: 1.47 KB
Contents
class PostSlackMessage 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 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 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 end
Version data entries
7 entries across 7 versions & 1 rubygems