# 【slack】上傳檔案

#### **找到 CHANNEL\_ID**

[![image-1741855326388.png](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/scaled-1680-/qAkFxsyhs02Aa1wG-image-1741855326388.png)](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/qAkFxsyhs02Aa1wG-image-1741855326388.png)

#### **找到 SLACK\_BOT\_TOKEN**

[![image-1741855202204.png](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/scaled-1680-/5y8eAFk89XYUS2QO-image-1741855202204.png)](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/5y8eAFk89XYUS2QO-image-1741855202204.png)

如果tocken權限不足，要往下拉確認一下 bot scopes 權限

[![image-1741855214090.png](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/scaled-1680-/TTtpV3geH2OsjcEW-image-1741855214090.png)](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/TTtpV3geH2OsjcEW-image-1741855214090.png)

#### 準備 python 環境

```shell
echo 'slack-sdk>=3.12,<4' > requirements.txt
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install -r requirements.txt
```

#### upload.py (注意參數是<span style="color: #e03e2d;">channel</span> 不是 <span style="text-decoration: line-through;">channel\_id</span> <span style="color: #e03e2d;">官方文件錯誤</span>)

```Python
import logging, os

logging.basicConfig(level=logging.DEBUG)

from slack_sdk import WebClient
SLACK_BOT_TOKEN = "xoxb-*******************"
CHANNEL_ID = "C01*******"

client = WebClient(SLACK_BOT_TOKEN)

upload_text_file = client.files_upload_v2(
    channel=CHANNEL_ID,
    # title="Test text data",
    file="./response.png",
    initial_comment="告警結果",
)
```

[![image-1741855246370.png](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/scaled-1680-/t9ta0IcLulSePPLX-image-1741855246370.png)](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/t9ta0IcLulSePPLX-image-1741855246370.png)

[![image-1741855268581.png](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/scaled-1680-/El8BrpybDNmLAbBO-image-1741855268581.png)](https://bookstack.treemanou.com/uploads/images/gallery/2025-03/El8BrpybDNmLAbBO-image-1741855268581.png)

參考 ：  
官方網站  
[https://tools.slack.dev/python-slack-sdk/tutorial/uploading-files](https://tools.slack.dev/python-slack-sdk/tutorial/uploading-files)

官方網站-文件錯誤  
[https://github.com/slackapi/python-slack-sdk/issues/1608](https://github.com/slackapi/python-slack-sdk/issues/1608)