カテゴリー
[Slack x 業務効率化] Slack APIを利用してCurlから画像をアップロードしてみた
※ 当ページには【広告/PR】を含む場合があります。
2020/09/17
2022/01/10

下準備 ~ Slack APIを使う前の設定手順
Slack APIの作成 ~ Tokenを取得

[Create an App]
App Name
Workspace


[Feature] > [OAuth & Permissions]
Bot Token Scopes
User Token Scopes

Bot Token Scopes
User Token Scopes
Bot Token Scopes
chat:write
files:write

[Settings] > [Install App]
[Install App to Workspace]



アプリをチャンネルに追加

シェルスクリプトの実装例
動作テスト
$ curl -XPOST \
-F "content=launch plan" \
-F channels="チャンネルの名前" \
-H "Authorization: Bearer xoxb-xxxxxxxxx-xxxx" \
https://slack.com/api/files.upload
$ curl -XPOST \
-d "token=xoxb-xxxxxxxxx-xxxx&content=launch_plan&channels=チャンネルの名前" \
https://slack.com/api/files.upload
"hoge=hogehoge&piyo=piyopiyo"
"-F 'hoge=hogehoge' -F 'piyo=piyopiyo'..."
&
画像の送信
#!/bin/sh
#APIのトークン
SLACK_API_TOKEN=xoxp-xxx-xx-xx-xxxx
#アップロードしたいファイルまでの絶対パス
filepath=/path/to/graph.png
#アプリを追加したチャンネルの名前
channels="チャンネルの名前"
curl -F "file=@${filepath}" -F channels="$channels" -H "Authorization: Bearer ${SLACK_API_TOKEN}" https://slack.com/api/files.upload

まとめ
記事を書いた人
ナンデモ系エンジニア
主にAngularでフロントエンド開発することが多いです。 開発環境はLinuxメインで進めているので、シェルコマンドも多用しております。 コツコツとプログラミングするのが好きな人間です。
カテゴリー