当我尝试在邮件中添加附件时,我或者只获取文本,或者如果我遗漏文本,我会收到“错误”:“no_text”,有没有办法发送带有chat.postMessage的附件?< / p>
这是我用来发送消息的python代码:
r = requests.post('https://slack.com/api/chat.postMessage', params=json.loads("""
{
"token": "xoxp-mytokenhere",
"channel": "C4mychannelhere",
"attachments": [
{
"text": "Question?",
"fallback": "Question?",
"callback_id": "callback_id",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "question",
"text": "Yes",
"style": "good",
"type": "button",
"value": "yes"
},
{
"name": "question",
"text": "Nope",
"style": "good",
"type": "button",
"value": "no"
}
]
}
]
}
"""))
根据评论,我采用了以下解决方案:
r = requests.post('https://slack.com/api/chat.postMessage', params=json.loads({
"token": "xoxp-mytokenhere",
"channel": "C4mychannelhere",
"attachments": json.dumps([
{
"text": "Question?",
"fallback": "Question?",
"callback_id": "callback_id",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "question",
"text": "Yes",
"style": "good",
"type": "button",
"value": "yes"
},
{
"name": "question",
"text": "Nope",
"style": "good",
"type": "button",
"value": "no"
}
]
}
])
}))
答案 0 :(得分:3)
看起来您正在尝试将JSON字符串作为整套参数发送到chat.postMessage
。
chat.postMessage
和其他网络API方法仅支持网址编码查询或POST正文参数,因此您的token
和channel
以及attachments
等字段将作为应用/而是x-www-form-urlencoded键/值对。
为了使事情进一步复杂化,实际的attachments
参数获取一串URL编码的JSON数据。您的JSON数组需要进行URL编码并填充到该参数中。
根据您的目标,您可以跳过使用json.loads
并将该JSON字符串作为attachments
参数传递,requests
将为您处理URL编码 - 或者您可以在使用相同属性构建的本机Python数组上使用json.dump
之类的内容。