諸行無常

IT色々お勉強中のブログ

rails slackにtaskでメッセージを送る

まずslack側でwebhookの設定

https://creww.slack.com/apps/A0F7XDUAZ-incoming-webhooks

f:id:babababand:20161026205652p:plain

add configurationボタンを押して通知したいslack channel選択

gem 追加

gem 'slack-notifier'

slack側で取得したurlをhookに設定してtaskなどで以下を呼び出す

      hook = "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXX"
      if hook
        notifier = Slack::Notifier.new hook, http_options: { open_timeout: 2 }
        notifier.username = :dev
        begin
          notifier.ping "テストです。" 
        rescue
          # no need report problem with Slack
        end
      else
        Rails.logger.warn 'Slack hook is missing'
      end

以上