LoginSignup
4
2

More than 5 years have passed since last update.

RubyからSlack-API

Last updated at Posted at 2017-12-01

RubyからSlack-API

目次

  • はじめに
  • 準備
  • 実践
  • 応用

はじめに

AdventClendar"Slack"が空いていたので、この記事を書くことになりました。
Slackで嫌がらせメッセージを送りたいときに結構使います(笑)

準備

Slackにbotユーザーを作る

https://api.slack.com/apps
上にアクセスして、Create an Appをクリックします。
スクリーンショット 2017-12-01 9.49.28.png
そこで、botの名前とslackチームを選択します。
私は,"TestBot"にしました!
作ったら出てくるページの下の方のVerification Tokenのtokenをメモしておきましょう!
スクリーンショット 2017-12-01 9.50.59.png

gemInstall

gem install slack-apiを実行します
これで、準備が完了です!

実践

下のYOUTOKENをあなたの先ほどのTokenに置き換えて、実行して見ます。

require 'slack-api'
# 先ほどのTokenを下に入力
TOKEN = 'xxxxxx'

Slack.configure do |conf|
  conf.token = TOKEN
end

client = Slack::RealTime::Client.new

client.on :hello do
  puts 'connected!'
  client.message channel: 'your_channel_id', text: 'connected!'
end

上のようにすることで、slackにポストできちゃいます!

応用

Slack-APIを使うと、返事??もできるらしいです。
(例:"スケジュール"とダイレクトメッセージで送信すると、今日のGoogleカレンダーが表示されるなど..etc..)

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2