環境
- Python3.10
準備
ライブラリの準備
slackが公式に slack-sdk というライブラリを公開しているので、こちらをインストールします
pip install slack-sdk
tokenの準備
実装
基本 公式ドキュメントを参考にします
テキストを送信する
client = WebClient(token=slack_token) try: response = client.chat_postMessage( channel="C0XXXXXX", text="Hello from your app! :tada:" ) except SlackApiError as e: # You will get a SlackApiError if "ok" is False assert e.response["error"]
ファイル(画像)を送信する
ファイルを送信するときは、以下をします。uploadがv2になっているので注意です
response = client.files_upload_v2( channel="C3UKJTQAC", file="files.pdf", title="Test upload", initial_comment="Here is the latest version of the file!", )