我正在编写一个 Python 脚本,使用 files.upload API 和 slack_sdk 库将文件上传到 Slack 通道。该脚本似乎工作正常,因为它返回带有文件 ID 的成功响应,但该文件在指定通道中不可见。
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
slack_token = 'YOUR_OAUTH_TOKEN'
channel_id = 'your_channel_id'
file_path = 'path/to/your/file.txt'
def upload_file_to_slack(token, channel, file_path):
client = WebClient(token=token)
try:
response = client.files_upload(
channels=channel,
file=file_path
)
print("File uploaded successfully. File ID:", response['file']['id'])
except SlackApiError as e:
print("Error uploading file:", e.response['error'])
if __name__ == "__main__":
upload_file_to_slack(slack_token, channel_id, file_path)
问题:
我收到包含文件 ID 的成功响应。 但是,该文件没有出现在 Slack 频道中。 我已经检查了权限和频道设置。
如果您能了解为什么尽管已成功响应但文件未在频道中共享,我将不胜感激。我应该以特定方式包含频道 ID,还是还有其他我可能遗漏的内容?
这通常是由于机器人不在通道上造成的,如“文件”标题下的 https://slack.dev/python-slack-sdk/web/index.html#files_upload 中所述。 您确定机器人已被邀请加入频道吗? 其次,您可以尝试使用 files_uplaod_v2,如下所述: https://github.com/slackapi/python-slack-sdk/releases/tag/v3.19.0