我正在尝试简单的事情,使用python脚本将本地图片添加到我的松弛频道。我没有找到答案。我已经为自己的频道创建了松弛应用,并具有验证令牌和APP ID。
我尝试了以下操作,但没有结果:
import requests
files = {
'file': ('dog.jpg', open('dog.jpg', 'rb')),
'channels': (None, 'App ID,#channel'),
'token': (None, 'Verification Token'),
}
和:
import os
from slack import WebClient
from slack.errors import SlackApiError
client = WebClient(token=os.environ['SLACK_API_TOKEN'])
try:
filepath="./tmp.txt"
response = client.files_upload(
channels='#random',
file=filepath)
assert response["file"] # the uploaded file
except SlackApiError as e:
# You will get a SlackApiError if "ok" is False
assert e.response["ok"] is False
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
print(f"Got an error: {e.response['error']}")
response = requests.post('https://slack.com/api/files.upload', files=files)
这里,当我将Slack应用程序令牌插入SLACK_API_TOKEN时,它给了我令牌错误。任何人都知道将本地图像发布到松弛状态的快速简便的方法吗?
谢谢!
以下是使用官方Slack库将文件上传到Slack的示例代码段:
import os
import slack
from slack.errors import SlackApiError
# init slack client with access token
slack_token = os.environ['SLACK_TOKEN']
client = slack.WebClient(token=slack_token)
# upload file
try:
response = client.files_upload(
file='Stratios_down.jpg',
initial_comment='This space ship needs some repairs I think...',
channels='general'
)
except SlackApiError as e:
# You will get a SlackApiError if "ok" is False
assert e.response["ok"] is False
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
print(f"Got an error: {e.response['error']}")
如您所见,您不同时使用请求和松弛。后者将胜任。