LoginSignup
0
1

More than 3 years have passed since last update.

SlackのIncoming Webhooksを使って外部から通知するためのシェルスクリプトのサンプル

Last updated at Posted at 2017-09-25

Incoming Webhooks の Slack アプリケーションを使って Slack に通知を送るためのサンプルシェルスクリプト。

Require

Sample Code

slack-notify.sh
#!/bin/bash

URL="https://hooks.slack.com/services/*********/*********/************************"

CHANNEL='#test_channel'
USERNAME='test-bot'
ICON=':sushi:'
MESSAGE='```'$@'```'

PAYLOAD="payload={
    \"channel\": \"${CHANNEL}\",
    \"username\": \"${USERNAME}\",
    \"icon_emoji\": \"${ICON}\",
    \"text\": \"${HEAD}${MESSAGE}\"
}"

curl -s -S -X POST --data-urlencode "${PAYLOAD}" ${URL} > /dev/null

Incoming Webhooks の設定で、投稿する対象のチャンネルを設定し、Slack APIを利用するための URL の取得します。シェルスクリプトでその URL とチャンネルをセットします。
合わせて、投稿するユーザ名とアイコン、このシェルスクリプトの引数をメッセージとして変数に格納するようにしています。

Usage

$ ./slack-notify.sh test

引数の内容を、指定したチャンネルへ、指定したユーザ名とアイコンで、投稿します。

0
1
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
0
1