雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

rubyでslackbotの作り方メモ

slackbotの作り方メモ

まず、botの登録。以下手順

  1. 左上にある自分のアイコンらへんををクリック
  2. 「Profile & account」をクリック
  3. 画面右に出る項目の中で「Account Settings」をクリック
  4. 左上の「Menu」をクリック
  5. 「Configure Apps」をクリック
  6. 左のManageカテゴリの「Custom Integrations」をクリック
  7. 「Bots」をクリック
  8. 「Add Configuration」をクリック
  9. あと、省略

上記の操作でTOKENを得る

以下はターミナルで

gem install slack-api

以下はプログラム

require "slack"

TOKEN = "自分のbotのTOKEN"

Slack.configure {|config| config.token = TOKEN }
client = Slack.realtime

client.on :hello do
  puts 'Successfully connected.'
end

client.on :message do |data|
  # 得られたmessageにtestが含まれていて、botからのメッセージでなければ
  if data['text'].include?("test") && data["subtype"] != 'bot_message'
    # test_channelにtestをいうメッセージをいれる
    Slack.chat_postMessage(text: "test", channel: "test_channel")
  end
end

client.start