LoginSignup
168
187

More than 5 years have passed since last update.

Slack APIでメッセージ送信

Posted at

定期的にSlackへメッセージを送ったり、
ある条件を満たした時にSlackへメッセージを送信するといったことを実現したい場合、
デフォルトで備わっているReminderやSlackbotを使って実現できなくはないですが、
少し凝ったことをしたいときには少し不便です。

そこで今回は、SlackのAPIを使ってメッセージを送信したいと思います。

APIを実行するためには「Token」が必要

  1. https://api.slack.com/web にアクセス
  2. 「Generate test tokens」をクリック
  3. 「Create token」をクリック
  4. 作成されたTokenをメモ(API実行には必ず必要です。)

使用するAPI:chat.postMessage

その名の通り特定のChannelやユーザーに向けメッセージを送信するAPIです。

使用方法

// Webブラウザで実行
https://slack.com/api/chat.postMessage?{Arguments}

or

// コマンドラインで実行(POST or GET)
$ curl -XPOST -d '{Arguments}' 'https://slack.com/api/chat.postMessage'
or
$ curl 'https://slack.com/api/chat.postMessage?{Arguments}' ★要urlエンコード

主に使用するパラメーター(Arguments)

パラメーター名 サンプル 必須/任意 詳細
token xxxx-xxxxxxxxx-xxxx 必須 上記で説明したToken
channel channel_name 必須 channel名 or @{user}
text Hello world 必須 送信したいメッセージ
as_user true 任意 trueの場合、差出人がToken発行ユーザーとなる。falseの場合Botが差出人となる。
username My Bot 任意 as_user=false時のBot名
icon_url http://lorempixel.com/48/48 任意 as_user=false時のBotアイコン
attachments [{"pretext": "pre-hello", "text": "text-world"}] 任意 添付ファイル

その他のパラメーターについては公式ページを参照してください。
https://api.slack.com/methods/chat.postMessage

実行例

■「test」チャンネルに「Hollo World」とメッセージを送信

パラメーター「username」を指定しない場合、Bot名はデフォルトの「Slack API Tester」となる。

実行コマンド

$ curl -XPOST -d 'token={token}' -d 'channel=test' -d 'text=Hello World' 'https://slack.com/api/chat.postMessage'

レスポンス

{
    "channel": "C2HDVFWM7", 
    "message": {
        "bot_id": "B2HDQ7VV3", 
        "subtype": "bot_message", 
        "text": "Hello World", 
        "ts": "1475138775.000006", 
        "type": "message", 
        "username": "Slack API Tester"
    }, 
    "ok": true, 
    "ts": "1475138775.000006"
}

Slackキャプチャ

image

■「test」チャンネルに「HelloWorldBot」という名前のBotで「Hello World!!」とメッセージを送信

Bot名が「HelloWorldBot」に変わる。

実行コマンド

$ curl -XPOST -d 'token={token}' -d 'channel=test' -d 'text=Hello World!!' -d 'username=HelloWorldBot' 'https://slack.com/api/chat.postMessage'

レスポンス

{
    "channel": "C2HDVFWM7", 
    "message": {
        "bot_id": "B2HDQ7VV3", 
        "subtype": "bot_message", 
        "text": "Hello World!!", 
        "ts": "1475140069.000007", 
        "type": "message", 
        "username": "HelloWorldBot"
    }, 
    "ok": true, 
    "ts": "1475140069.000007"
}

Slackキャプチャ

image

■「test」チャンネルに自分のユーザーで「@channel Hello World!!!!」とメッセージを送信

as_user=trueを付与すると差出人がBotではなく、Tokenを発行したユーザー(自分のユーザー)名でメッセージを送信できる。
また、チャンネル全体に通知する時につかう@channel を使いたい場合は、
Slack特有の記法に則らなければならない。

通知先 記法 備考
@channel <!channel> チャンネル全体
@here <!here> チャンネルのActiveユーザー全体
@user <@user> 特定のユーザー

※参考:http://qiita.com/Yinaura/items/539e59149b41b9d516c1

実行コマンド

$ curl -XPOST -d 'token={token}' -d 'channel=test' -d 'text=<!channel> Hello World!!!!' -d 'as_user=true' 'https://slack.com/api/chat.postMessage'

レスポンス

{
    "channel": "C2HDVFWM7", 
    "message": {
        "bot_id": "B2HDQ7VV3", 
        "text": "<!channel> Hello World!!!!", 
        "ts": "1475141074.000008", 
        "type": "message", 
        "user": "{user}"
    }, 
    "ok": true, 
    "ts": "1475141074.000008"
}

Slackキャプチャ

image

■「test」チャンネルに「猫Bot」という名前・猫アイコンのBotで「ニャ~ニャ~」とメッセージ送信

設定したいアイコンのURLを下記のように設定する。
icon_url=https://www.pakutaso.com/shared/img/thumb/HIRAorenobasyoosu_TP_V.jpg

実行コマンド

$ curl -XPOST -d 'token={token}' -d 'channel=test' -d 'text=ニャ~ニャ~' -d 'username=猫Bot' -d 'icon_url=https://www.pakutaso.com/shared/img/thumb/HIRAorenobasyoosu_TP_V.jpg' 'https://slack.com/api/chat.postMessage'

レスポンス

Slackの画像保存先ってAWS S3なんですね!知らなかった。

{
    "channel": "C2HDVFWM7", 
    "message": {
        "bot_id": "B2HDQ7VV3", 
        "icons": {
            "image_48": "https://s3-us-west-2.amazonaws.com/slack-files2/bot_icons/2016-09-29/85490051489_48.png"
        }, 
        "subtype": "bot_message", 
        "text": "ニャ~ニャ~", 
        "ts": "1475141746.000009", 
        "type": "message", 
        "username": "猫Bot"
    }, 
    "ok": true, 
    "ts": "1475141746.000009"
}

Slackキャプチャ

image

以上、Slack API(chat.postMessage)の使用方法でした。

168
187
1

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
168
187