こんにちは。AWS CLIが好きな福島です。 今回は最近使っているSlack APIのコマンドをまとめたいと思います。
はじめに
参考
前提
SlackのAPIを実行するためには、トークンが必要になるため、トークンを定義した上で実行します。
slack_api_token="xoxb-xxxxxxxx-xxxxxxxxxxx-xxxxxxxxxxxxx"
また、ほとんどのコマンドにlimit=1を付与しているため、出力結果が1個になります。
GET系
メッセージの取得(conversations.history)
- 事前定義
channel_id="[チャンネルID]"
- コマンド
curl -s "https://slack.com/api/conversations.history?channel=${channel_id}&limit=1" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq
- 出力結果例
{ "ok": true, "messages": [ { "client_msg_id": "xxxxxxxxxx-xxxxx-xxxx-xxxxxxxxxxxx", "type": "message", "text": "こんにちは。AWS CLIが好きな福島です。", "user": "XXXXXXXXX", "ts": "1649450007.998529", "team": "XXXXXXXXX", "blocks": [ { "type": "rich_text", "block_id": "tXXtn", "elements": [ { "type": "rich_text_section", "elements": [ { "type": "text", "text": "こんにちは。AWS CLIが好きな福島です。" } ] } ] } ] } ], "has_more": true, "pin_count": 1, "channel_actions_ts": null, "channel_actions_count": 0, "response_metadata": { "next_cursor": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" } }
チャンネルの一覧取得(conversations.list)
- コマンド
curl -s "https://slack.com/api/conversations.list?limit=1" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq
- 実行結果
{ "ok": true, "channels": [ { "id": "XXXXXXXX", "name": "general", "is_channel": true, "is_group": false, "is_im": false, "is_mpim": false, "is_private": false, "created": XXXXXXXX, "is_archived": false, "is_general": true, "unlinked": 0, "name_normalized": "general", "is_shared": false, "is_org_shared": false, "is_pending_ext_shared": false, "pending_shared": [], "parent_conversation": null, "creator": "XXXXXXXX", "is_ext_shared": false, "shared_team_ids": [ "XXXXXXXXX" ], "pending_connected_team_ids": [], "is_member": false, "topic": { "value": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "creator": "XXXXXXXX", "last_set": 1552269191 }, "purpose": { "value": "ゆるいお知らせが流れたり、仕事の匂いがする話題を繰り広げる場所。通知専用チャンネルじゃないよ!", "creator": "XXXXXXXX", "last_set": 1567050902 }, "previous_names": [], "num_members": xxx } ], "response_metadata": { "next_cursor": "XXXXXXXXXXXXXX" } }
チャンネルの情報取得(conversations.info)
- 事前定義
channel_id="[チャンネルID]"
- コマンド
curl -s "https://slack.com/api/conversations.info?channel=${channel_id}&limit=1" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq
- 実行結果
{ "ok": true, "channel": { "id": "XXXXXXXX", "name": "notice-fk-ag-report-v2", "is_channel": true, "is_group": false, "is_im": false, "is_mpim": false, "is_private": true, "created": 1649209276, "is_archived": false, "is_general": false, "unlinked": 0, "name_normalized": "notice-fk-ag-report-v2", "is_shared": false, "is_org_shared": false, "is_pending_ext_shared": false, "pending_shared": [], "parent_conversation": null, "creator": "XXXXXXXXX", "is_ext_shared": false, "shared_team_ids": [ "XXXXXXXXX" ], "pending_connected_team_ids": [], "is_member": true, "last_read": "1649209495.192869", "is_open": true, "topic": { "value": "", "creator": "", "last_set": 0 }, "purpose": { "value": "", "creator": "", "last_set": 0 } } }
ピン留めしている情報の取得(pins.list)
- 事前定義
channel_id="[チャンネルID]"
- コマンド
curl -s https://slack.com/api/pins.list?channel=${channel_id} \ -H "Authorization: Bearer ${slack_api_token}" \ | jq
- 実行結果
{ "ok": true, "items": [ { "type": "message", "created": 1649450260, "created_by": "XXXXXXXXX", "channel": "XXXXXXXXX", "message": { "client_msg_id": "xxxxxxxxxx-xxxxx-xxxx-xxxxxxxxxxxx", "type": "message", "text": "こんにちは。AWS CLIが好きな福島です。", "user": "XXXXXXXXX", "ts": "1649450007.998529", "team": "XXXXXXXXX", "blocks": [ { "type": "rich_text", "block_id": "tXXtn", "elements": [ { "type": "rich_text_section", "elements": [ { "type": "text", "text": "こんにちは。AWS CLIが好きな福島です。" } ] } ] } ], "pinned_to": [ "XXXXXXXX" ], "permalink": "https://hogehoge.slack.com/archives/XXXXXXXX/p1649450007998529" } } ] }
ユーザー情報の取得(users.info)
- 事前定義
user_id=""
- コマンド
curl -s "https://slack.com/api/users.info?user=${user_id}" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq
- 実行結果
{ "ok": true, "user": { "id": "XXXXXXXXX", "team_id": "XXXXXXXXX", "name": "fukushima", "deleted": false, "color": "8f4a2b", "real_name": "fukushima", "tz": "Asia/Tokyo", "tz_label": "Japan Standard Time", "tz_offset": 32400, "profile": { "title": "", "phone": "xxxxxxxxx", "skype": "", "real_name": "fukushima", "real_name_normalized": "fukushima", "display_name": "fukushima", "display_name_normalized": "fukushima", "fields": null, "status_text": "", "status_emoji": "", "status_emoji_display_info": [], "status_expiration": 0, "avatar_hash": "XXXXXXXXX", "image_original": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_original.png", "is_custom_image": true, "email": "hogehoge@example.com", "huddle_state": "default_unset", "huddle_state_expiration_ts": 0, "first_name": "fukushima", "last_name": "", "image_24": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_24.png", "image_32": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_32.png", "image_48": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_48.png", "image_72": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_72.png", "image_192": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_192.png", "image_512": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_512.png", "image_1024": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_1024.png", "status_text_canonical": "", "team": "XXXXXXXXX" }, "is_admin": false, "is_owner": false, "is_primary_owner": false, "is_restricted": false, "is_ultra_restricted": false, "is_bot": false, "is_app_user": false, "updated": 1648693795, "is_email_confirmed": true, "who_can_share_contact_card": "EVERYONE" } }
Bot情報の取得(bots.info)
- 事前定義
bot_id=""
- 実行結果
curl -s "https://slack.com/api/bots.info?bot=${bot_id}" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' | jq
- 実行結果
{ "ok": true, "bot": { "id": "XXXXXXXXX", "deleted": false, "name": "Slackのメッセージ集約", "updated": 1646099607, "app_id": "XXXXXXXXX", "user_id": "XXXXXXXXX", "icons": { "image_36": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx36.png", "image_48": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx48.png", "image_72": "https://avatars.slack-edge.com/xxxx-xx-xx/xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx72.png" } } }
POST系
メッセージのPost(chat.postMessage)
[チャンネル名を指定]の箇所にチャンネルIDを指定します。
※チャンネルIDではない点に注意。
- コマンド
curl -X POST -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-type: application/json \ --data {"text" : "Hello World", "channel" : "チャンネル名を指定"} \ https://slack.com/api/chat.postMessage
変数を使いたい場合、以下のコマンドを使います。
- 事前定義
text="Hello World" post_channel_name=""
- コマンド
echo "{\"channel\":\"${post_channel_name}\", \ \"text\":\" ${text} \"}" \ | curl -X POST -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-type: application/json' \ --data @- \ https://slack.com/api/chat.postMessage
- 実行結果
メッセージの削除
- 事前定義
channel_id="チャンネルID" ts="timestamp"
上記情報は、メッセージのリンクから確認可能です。
Slackのメッセージのリンクは以下の通りとなり、yにチャンネルID、zにtimestampの情報が入ります。 ただし、timestampには後ろから6番目に.を入れる必要があります。 例えば、zが1649450775924549の場合、1649450775.924549となります。
https://xxxxxxx.slack.com/archives/yyyyyyyyyyy/pzzzzzzzzzzzzzzzzz
- コマンド
echo "{\"ts\":\"${ts}\",\"channel\":\"${channel_id}\"}" \ curl -XPOST -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ --data @- \ https://slack.com/api/chat.delete
ピン留め(pins.add)
- 事前定義
channel_id="チャンネルID" ts="timestamp"
- コマンド
echo "{\"channel\" : \"${channel_id}\",\"timestamp\":\"${ts}\"}" \ | curl -X POST \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8'\ --data @- \ https://slack.com/api/pins.add
ピン留めの解除(pins.remove)
- 事前定義
channel_id="チャンネルID" ts="timestamp"
- コマンド
echo "{\"channel\" : \"${channel_id}\",\"timestamp\":\"${ts}\"}" \ | curl -X POST \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8'\ --data @- \ https://slack.com/api/pins.remove
よく使うコマンド
メッセージのtextだけを取得
- 事前定義
channel_id="チャンネルID"
- コマンド
curl -s "https://slack.com/api/conversations.history?channel=${channel_id}&limit=1" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq .messages[].text
メッセージのtimestampのみ取得
- 事前定義
channel_id="チャンネルID"
- コマンド
curl -s "https://slack.com/api/conversations.history?channel=${channel_id}&limit=1" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq .messages[].ts
メッセージのリンクを取得(chat.getPermalink)
- 事前定義
channel_id="チャンネルID" ts="timestamp"
- コマンド
curl -s "https://slack.com/api/chat.getPermalink?channel=${channel_id}&message_ts=${ts}" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq -r .permalink
指定した時間以降のメッセージ取得
- 事前定義
channel_id="チャンネルID" oldest_ts="timestamp"
timestampはUNIXTIMEを指定する必要がありますが、以下のコマンドでUNIXTIMEに変換できます。
date -d "2022/04/09 10:00" +%s
ちなみにUNIXTIMEから元に戻す場合は以下のコマンドを使います。
date --date @1649466000 +'%Y/%m/%d %H:%M:%S'
- コマンド
curl -s "https://slack.com/api/conversations.history?channel=${channel_id}&oldest=${unix_oldset_time}" \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq -r
チャンネルIDからチャンネル名を取得
- 事前定義
channel_id="チャンネルID"
- コマンド
curl -s https://slack.com/api/conversations.info?channel=${channel_id} \ -H "Authorization: Bearer ${slack_api_token}" \ -H 'Content-Type: application/json; charset=utf-8' \ | jq .channel.name
終わりに
今回は、SlackのAPIをcurlを使って操作するコマンドをまとめてみました。 どなたかのお役に立てれば幸いです。