Last active
April 3, 2018 21:24
-
-
Save maecha/d3aca0d6a82fc1cf34fe45105cc12549 to your computer and use it in GitHub Desktop.
Post a message to 'alert' channel in Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'json' | |
gem 'faye-websocket' | |
gem 'eventmachine' | |
gem 'slack-ruby-client' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SlackModule | |
before_action :get_slack_client | |
require 'slack-ruby-client' | |
require 'json' | |
def post_to_alert_channel | |
index = 'Index' | |
description = 'Description' | |
json = JSON.generate([ | |
{ | |
color: '#ac3d54', | |
pretext: '', | |
title: 'Title', | |
text: '----------------------------', | |
fields: [ | |
{ | |
title: 'Description', | |
value: description, | |
short: false | |
}, | |
{ | |
title: 'Index', | |
value: index, | |
short: false | |
} | |
] | |
} | |
]) | |
@client.chat_postMessage( | |
channel: '#alert', # To 'alert' channel | |
text: '@channel Alert', # the notification type is '@channel' | |
as_user: false, | |
attachments: json, | |
username: 'Alert Bot chan', | |
icon_emoji: ':warning:', | |
link_names: '1' | |
) if Rails.env.production? || Rails.env.staging? | |
end | |
private | |
def get_slack_client | |
Slack.configure do |config| | |
config.token = '**********' | |
end | |
@client = Slack::Web::Client.new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment