LoginSignup
51
53

More than 5 years have passed since last update.

コード1行でBotがSlackへ通知してくれるGemを公開しました

Last updated at Posted at 2014-09-09

(2014/9/10追記)
v0.0.2をリリース、カスタマイズ時のInterfaceを変更しました。

概要

kidach1/slack_bot
https://github.com/kidach1/slack_bot

※ 本投稿は基本的にREADMEの和訳です。

何?

コード1行でSlackにエラーやらなにやら(任意)を飛ばしてくれます。
シンプルな分他の用途にも使える、かもしれません。

準備

APIトークンの生成

まずは準備として

よりAPIトークンの発行を行います。

Slack内にサインインし、かつ何らかのteamに属していれば、以下のようにCreate tokenを押下するだけで発行可能です。

Slack_API___Slack.png

設定ファイルを追加

Railsアプリの場合、

config/initializers/slack_bot.rbを作成、以下を追加します。

config/initializers/slack_bot.rb
SlackBot.setup do |config|
  config.token = <YOUR_API_TOKEN>
  config.channel = <YOUR_CHANNEL_OR_GROUPE_NAME>
  config.bot_name = <BOT_NAME>
  config.body = <DEFAULT_MESSAGE>
end

使う

任意の箇所で以下を呼び出します。

# Call me anywhere
SlackBot.notify

以上です!

例えばこんな設定だった場合

config/initializers/slack_bot.rb
SlackBot.setup do |config|
  config.token = xxxxxx
  config.channel = '#sample-channel'
  config.bot_name = 'SlackBot'
  config.body = "[#{Rails.env}] Hello, SlackBot!"
end
# Call me anywhere
SlackBot.notify

結果

Slack.png

こうなります。

カスタマイズ

文脈によってbotの名前や通知内容を変えたい場合は、呼び出し箇所で

SlackBot.notify(
  body: 'Custom body!',
  bot_name: 'CustomBot',
  channel: 'custom-channel'
)

としてやります。

カスタマイズ例

例えば

#Call me anywhere
SlackBot.notify(
  body: "[#{Rails.env}] Custom body!", 
  bot_name: "CustomBot"
)

とすると

結果

Slack.png

となります。

とにかく手軽がいい、という時にどうぞ。

51
53
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
51
53