Slack chat.postMessage 附件提供 no_text
Slack chat.postMessage attachment gives no_text
当我尝试向我的消息添加附件时,我要么只收到文本,要么如果我遗漏了文本,我会收到 "error": "no_text",有什么方法可以发送附件chat.postMessage?
这是我用来发送消息的 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"
}
]
}
])
}))
您似乎正在尝试将 JSON 字符串作为整组参数发送至 chat.postMessage
。
chat.postMessage
和其他网络 API 方法仅支持 URL 编码查询或 POST 正文参数,因此您的字段 token
和 channel
和 attachments
作为 application/x-www-form-urlencoded key/value 对发送。
更复杂的是,attachments
参数实际上 确实 接受一串 URL 编码的 JSON 数据。您的 JSON 数组需要进行 URL 编码并填充到该参数中。
根据您的目标,您可以跳过使用 json.loads
,只需将 JSON 字符串作为您的 attachments
参数传递,requests
将处理 URL-为您编码——或者您可以在使用相同属性构建的本机 Python 数组上使用类似 json.dump
的内容。
当我尝试向我的消息添加附件时,我要么只收到文本,要么如果我遗漏了文本,我会收到 "error": "no_text",有什么方法可以发送附件chat.postMessage?
这是我用来发送消息的 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"
}
]
}
])
}))
您似乎正在尝试将 JSON 字符串作为整组参数发送至 chat.postMessage
。
chat.postMessage
和其他网络 API 方法仅支持 URL 编码查询或 POST 正文参数,因此您的字段 token
和 channel
和 attachments
作为 application/x-www-form-urlencoded key/value 对发送。
更复杂的是,attachments
参数实际上 确实 接受一串 URL 编码的 JSON 数据。您的 JSON 数组需要进行 URL 编码并填充到该参数中。
根据您的目标,您可以跳过使用 json.loads
,只需将 JSON 字符串作为您的 attachments
参数传递,requests
将处理 URL-为您编码——或者您可以在使用相同属性构建的本机 Python 数组上使用类似 json.dump
的内容。