なるべくシンプルに Ruby のコードから Slack へ投稿する方法
Ruby には Slack を扱うための gem がありますが、
Slack 用の gem を利用しなくても Slack に投稿することは出来ます。 incoming webhook の url は既に Slack apps で生成済みとします。
- blocks を編集すればメッセージ内容を変えることができます。
require 'net/http' require 'uri' require 'json' def post_slack(payload, incoming_webhook_url) uri = URI.parse(incoming_webhook_url) begin res = Net::HTTP.post_form(uri, {payload: payload.to_json}) puts "#{res.code}: #{res.message}" rescue => exception puts exception.message end end incoming_webhook_url = "incoming webhook url" payload = { blocks: [ { type: "header", text: { type: "plain_text", text: "Hello, This is Sales Report" } }, { type: "section", text: { type: "mrkdwn", text: "Today's *product* sales <https://ranking.rakuten.co.jp|rank>" }, }, ] } post_slack(payload, incoming_webhook_url)