1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Pi から Slack に投稿する

1
Last updated at Posted at 2019-10-13

はじめに

Raspberry Pi から Slack に投稿できるようにする。
ゆくゆくは、ボタンを押したら投稿するようにしたいのだが、とりあえず普通に投稿できるようにする。

プログラムは Ruby を利用する。

WebHook の準備

slackの左下にある「Add more apps」をクリックして、検索で「incomming webhook」と入れると出てくるので「インストール」をクリック
※)もうインストール済みなので「表示」と書かれている。

image.png

image.png

ブラウザが立ち上がって Incomming WebHookの画面が出るので「設定の追加」を押す

image.png

チャネルを選んで、「Incomming WebHookインテグレーションの追加」を押す
ここで選択するチャネルは何でもよいので、なにか選んでおく。(プログラムで、どのチャネルに投稿するか決められる)

image.png

Webhook URLが出てくるので、これをコピペしておく。

Rasberry Piの確認

Raspberry Pi 3B+ を Full でインストールすると、Rubyもインストールされているはずだが、とりあえず確認する。

# ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [arm-linux-gnueabihf]

よしよし、インストールされている。

slack-notifier のインストール

ruby のライブラリである slack-notifier を gem コマンドを利用してインストールする。
※)gem とは RubyGemsが公開しているライブラリのこと。

# gem install slack-notifier
Successfully installed slack-notifier-2.3.2
Parsing documentation for slack-notifier-2.3.2
Installing ri documentation for slack-notifier-2.3.2
Done installing documentation for slack-notifier after 1 seconds
1 gem installed

slack に送信するプログラムを作る

いたって簡単。
WEBHOOKURL に Incomming WEBHook で取得したURL、CHANNEL に送信したいチャネルを記載する。

require 'slack-notifier'

# slack Incomming Webhookの設定
WEBHOOKURL = "<Incomming Webhook で取得したURL>"
CHANNEL = "#<送信したいチャネル名>"
USERNAME = "<Slackに投稿するユーザ名(表示されるユーザ名)>"

# 通知内容
attachment = {
  color: "good",
  text: "送信完了!",
}

notifier = Slack::Notifier.new  WEBHOCKURL, channel: CHANNEL, username: USERNAME
notifier.ping '', attachments: [ attachment ]

ちなみに attachments の color にプリセットされているものは以下の3つ。

  • good
  • warning
  • danger
1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?