rubyでslackbotの作り方メモ
slackbotの作り方メモ
まず、botの登録。以下手順
- 左上にある自分のアイコンらへんををクリック
- 「Profile & account」をクリック
- 画面右に出る項目の中で「Account Settings」をクリック
- 左上の「Menu」をクリック
- 「Configure Apps」をクリック
- 左のManageカテゴリの「Custom Integrations」をクリック
- 「Bots」をクリック
- 「Add Configuration」をクリック
- あと、省略
上記の操作で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