Skip to content

Instantly share code, notes, and snippets.

@maecha
Last active April 3, 2018 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maecha/d3aca0d6a82fc1cf34fe45105cc12549 to your computer and use it in GitHub Desktop.
Save maecha/d3aca0d6a82fc1cf34fe45105cc12549 to your computer and use it in GitHub Desktop.
Post a message to 'alert' channel in Slack
gem 'json'
gem 'faye-websocket'
gem 'eventmachine'
gem 'slack-ruby-client'
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