client.files_upload_v2 命令运行时不会抛出错误,但文件没有出现在通道中。
如果我检查响应['ok'],我就明白了。
如果我检查:response['files'][0]['url_private'] 我得到一个可以下载该文件的链接,所以它似乎正在上传到slack,但不幸的是,该文件没有出现在频道里。
from slack_sdk import WebClient
def upload_file_to_slack2(ch, a_file, comment):
token = "xoxp-3599... "
ch = "XXXXXXXXXX"
client = WebClient(token)
response = client.chat_postMessage(channel=ch, text='testing postMessage from python!') # this runs fine - text appears in channel
response = client.files_upload_v2( channel=ch,title=a_file,file=a_file) # runs without throwing error but file doesn't appear in the channel
print("Channels:",response['files'][0]['channels'])
产生:频道:[]
我怀疑这与缺少范围或 OAuth 问题有关。在文档中,它说 OAuth 更改为机器人令牌,但没有说明何时更改。
我一直在使用用户令牌,但如果我是唯一的用户,我应该使用机器人令牌吗?
我有: chat:write、files:read 和 files:write 作为我的机器人和用户令牌的范围。
我找到了一个解决方案:上传文件,从响应中获取永久链接,然后将永久链接发布到同一频道,然后文件就会显示出来。
response = client.files_upload_v2( channel=ch,title=a_file,file=a_file)
permalink = response['files'][0]['permalink']
response = client.chat_postMessage(channel=ch, text=permalink)
我认为这是由 files_upload_V2 修复的行为。