我已经用python写了一段代码来从DB获取大量数据(80行),现在我想通过webhook发布这些数据。我试着直接发布数据,但对我不起作用,所以我决定将数据保存在txt/.png文件中,然后将其发布到slack中
在将数据保存为report.txt文件后,我尝试了使用python编写以下代码,但它对我没有帮助
headers = {
'Content-type': 'application/json',
}
data = open('\\results\report.txt')
response = requests.post('https://jjjjjjj.slack.com/services/mywebhook', headers=headers, data=data)
请与我分享Curl命令,它将适合在python脚本中发布附件松弛,或请建议我任何更好的方法来张贴超过50行的数据松弛
发布于 2019-09-06 17:09:53
我建议将长文本作为文本文件上传。这样,Slack会自动将其格式化为预览,用户可以单击它来查看整个内容。
上传文件需要使用files_upload
接口方法。有了它,你还可以在上传中包含一条初始消息。
下面是一个使用标准Slack库的示例。这里我从一个文件中读取数据,但您当然会使用您的数据:
import slack
import os
# init slack client with access token
slack_token = os.environ['SLACK_TOKEN']
client = slack.WebClient(token=slack_token)
# fetch demo text from file
with open('long.txt', 'r', encoding='utf-8') as f:
text = f.read()
# upload data as file
response = client.files_upload(
content=text,
channels='general',
filename='long.txt',
title='Very long text',
initial_comment='Here is the very long text as requested:'
)
assert response['ok']
诀窍是使用content
而不是file
来传递数据。如果你使用file
,API会尝试从你的文件系统加载一个文件。
发布于 2019-09-10 15:23:00
下面的代码对我来说很好。:)
headers = { 'Authorization': 'Bearer xxxx-XXXXX-XXXXX', #Bot Token }
files = {
'file': ('LocationOfthefile\report.txt', open('LocationOfthefile\report.txt', 'rb')),
'initial_comment': (None, 'Some Text For Header'),
'channels': (None, 'Channel_Names'),
}
response = requests.post('slack.com/api/files.upload', headers=headers, files=files)
https://stackoverflow.com/questions/57824929
复制