slack rails連携

Slack管理画面

  1. manage/custom-integrations に遷移
  2. Incoming WebHooks を新規登録。
  3. Botアカウント及びチャンネルを作成
  4. Webhook URLを控える

Rails

// slack_url, slack_pathは、上記で控えたもの
class SlackClient
  def self.send(message_text)
    client = Faraday.new(:url => ENV["SLACK_URL"]) do |conn|
      conn.adapter Faraday.default_adapter
    end

    res = client.post do |request|
    request.url ENV["SLACK_PATH"]
    request.headers = {
      w'Content-type' => 'application/json',
    }
    request.body = {"text" => message_text}.to_json
    end
  end
end