英文:
Certain sections in slack-api blocks don't work
问题
尝试将此示例负载在块工具生成器中使用时,使用它在Python中时不起作用。原因是它不识别类型为tags
的一些值,可能是emoji=True。但将负载格式化为JSON字符串并发送也不起作用。我必须删除在块中创建问题的某些部分。
根据此示例,Slack API将块作为字典处理:
当在Python代码中使用时,引发错误的块工具如下:
[块工具链接](https://app.slack.com/block-kit-builder/T09D77D4P#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Hello,%20Assistant%20to%20the%20Regional%20Manager%20Dwight!%20Michael%20Scott%20wants%20to%20know%20where%20you'd%20like%20to%20take%20the%20Paper%20Company%20investors%20to%20dinner%20tonight.%5Cn%5Cn%20Please%20select%20a%20restaurant:%22%7D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Farmhouse%20Thai%20Cuisine%5Cn:star::star::star::star:%201528%20reviews%5Cn%20They%20do%20have%20some%20vegan%20options,%20like%20the%20roti%20and%20curry,%20plus%20they%20have%20a%20ton%20of%20salad%20stuff%20and%20noodles%20can%20be%20ordered%20without%20meat!!%20They%20have%20something%20for%20everyone%20here%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Kin%20Khao%5Cn:star::star::star::star:%201638%20reviews%5Cn%20The%20sticky%20rice%20also%20goes%20wonderfully%20with%20the%20caramelized%20pork%20belly,%20which%20is%20absolutely%20melt-in-your-mouth%20and%20so%20soft.%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Ler%20Ros%5Cn:star::star::star::star:%202082%20reviews%5Cn%20I%20would%20really%20recommend%20the%20%20Yum%20Koh%20Moo%20Yang%20-%20Spicy%20lime%20dressing%20and%20roasted%20quick%20marinated%20pork%20shoulder,%20basil%20leaves,%20chili%20&%20rice%20powder.%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22actions%22,%22elements%22:%5B%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Farmhouse%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22%7D,%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Kin%20Khao%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22,%22url%22:%22https://google.com%22%7D,%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Ler%20Ros%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22,%22url%22:%22https://google.com%22%7D%5D%7D%5D%7D
引发问题的负载如下:
block = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello, <lob> Team, Varanus wants you to know the Seo Anomalies Found.\n\n *Please review the Notifications below:*"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
<details>
<summary>英文:</summary>
Trying this example payload in block-kit builder doesn't work when using it in python.
Reason being it doesn't recognise some values in type `tags`, possibly emoji=True. But sending the payload formatting it to json string also doesn't work. I have to remove certain sections in the blocks which create the problem.
As per this example Slack API takes blocks as a Dictionary
https://github.com/slackapi/python-slack-sdk/blob/c9dc6aa0907a72c16cf36aa15e7e80031a9fdce2/integration_tests/samples/basic_usage/sending_a_message.py
The block-kit which gives an error when used in python code
https://app.slack.com/block-kit-builder/T09D77D4P#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Hello,%20Assistant%20to%20the%20Regional%20Manager%20Dwight!%20*Michael%20Scott*%20wants%20to%20know%20where%20you'd%20like%20to%20take%20the%20Paper%20Company%20investors%20to%20dinner%20tonight.%5Cn%5Cn%20*Please%20select%20a%20restaurant:*%22%7D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Farmhouse%20Thai%20Cuisine*%5Cn:star::star::star::star:%201528%20reviews%5Cn%20They%20do%20have%20some%20vegan%20options,%20like%20the%20roti%20and%20curry,%20plus%20they%20have%20a%20ton%20of%20salad%20stuff%20and%20noodles%20can%20be%20ordered%20without%20meat!!%20They%20have%20something%20for%20everyone%20here%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Kin%20Khao*%5Cn:star::star::star::star:%201638%20reviews%5Cn%20The%20sticky%20rice%20also%20goes%20wonderfully%20with%20the%20caramelized%20pork%20belly,%20which%20is%20absolutely%20melt-in-your-mouth%20and%20so%20soft.%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Ler%20Ros*%5Cn:star::star::star::star:%202082%20reviews%5Cn%20I%20would%20really%20recommend%20the%20%20Yum%20Koh%20Moo%20Yang%20-%20Spicy%20lime%20dressing%20and%20roasted%20quick%20marinated%20pork%20shoulder,%20basil%20leaves,%20chili%20&%20rice%20powder.%22%7D,%22accessory%22:%7B%22type%22:%22image%22,%22image_url%22:%22https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg%22,%22alt_text%22:%22alt%20text%20for%20image%22%7D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22actions%22,%22elements%22:%5B%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Farmhouse%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22%7D,%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Kin%20Khao%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22,%22url%22:%22https://google.com%22%7D,%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Ler%20Ros%22,%22emoji%22:true%7D,%22value%22:%22click_me_123%22,%22url%22:%22https://google.com%22%7D%5D%7D%5D%7D
Payload creating an issue
block = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello, <lob> Team, Varanus wants you to know the Seo Anomalies Found.\n\n *Please review the Notifications below:*"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
},
"accessory": {
"type": "image",
"image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."
},
"accessory": {
"type": "image",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."
},
"accessory": {
"type": "image",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "divider"
},
{
"type": "tags",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Farmhouse"
}
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Kin Khao"
}
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Ler Ros"
}
}
]
}
]
response = client.chat_postMessage(blocks=block, text="A New Notification", channel="Channel-ID")
**Without the tags section i.e ("type" : "tags"), it works**
block = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello, <lob> Team, Varanus wants you to know the Seo Anomalies Found.\n\n *Please review the Notifications below:*"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
},
"accessory": {
"type": "image",
"image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."
},
"accessory": {
"type": "image",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."
},
"accessory": {
"type": "image",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",
"alt_text": "alt text for image"
}
},
{
"type": "divider"
}
]
response = client.chat_postMessage(blocks=block, text="A New Notification", channel="Channel-ID")
If I use json.dumps(block) or str(block), it gives and error `invalid_block`
Using the suggestions to use json.dumps(blocks) doesn't work.
https://stackoverflow.com/questions/60344831/slack-api-invalid-block/76601814#76601814
Any idea what is the right way to send blocks in python?
Any help is appreciated.
</details>
# 答案1
**得分**: 2
如果你将你的区块部分复制粘贴到一个“Block Kit Builder”中,作为“消息预览”布局,验证器会在 `"type": "tags"` 附近失败,显示以下信息:
无效数值: "tags",
必须是以下之一: "actions", "condition", "context", "divider", "image" "section", "header", "sticker", "contact_card", "share_shortcut" "share_workflow", "video", "channel_set".
在参考链接中(https://api.slack.com/reference/block-kit/blocks),没有名为“Message”表面的此类区块。总之,这根本不是现有的类型。
在原始示例中,这是一个 [`"type": "actions"` 区块](https://api.slack.com/reference/block-kit/blocks#actions),这意味着它包含了在[参考:区块元素](https://api.slack.com/reference/block-kit/block-elements)中描述的一系列交互式 `elements`。
因此,你需要使用其中一些区块元素,而不是 `tags`。
`emoji` 布尔值通常是 Text 对象的字段,其 `type` 为 `plain_text` 或 `mrkdwn`。
发送像 `client.chat_postMessage(blocks=block,...` 这样的负载是正确的,`block` 可以是一个标准的 Python 对象 'as-is',也就是说,在从 Block Kit Builder 复制+粘贴时,你不需要手动序列化它使用 `json.dumps()`,只需确保在复制+粘贴时填写正确的原始值,例如使用 Python 的 `True`/`False` 而不是 JS 的 `true`/`false` 等。
<details>
<summary>英文:</summary>
If you copy-paste your blocks section into a Block Kit Builder as "Message Preview" layout, the validator will fail near the `"type": "tags"` with
Invalid value: "tags",
Must be one of: "actions", "condition", "context", "divider", "image"
"section", "header", "sticker", "contact_card", "share_shortcut"
"share_workflow", "video", "channel_set".
There is no such type of block with "Message" surface, in their reference: https://api.slack.com/reference/block-kit/blocks, anyway, this is not existing type at all.
In original example this is a [`"type": "actions"` block](https://api.slack.com/reference/block-kit/blocks#actions), that means it holds an array of interactive `elements` described in the [Reference: Block elements](https://api.slack.com/reference/block-kit/block-elements).
So you need to use some of these block elements, not `tags`.
`emoji` Boolean is field of Text object usually with `type` = `plain_text` or `mrkdwn`.
Sending payloads like `client.chat_postMessage(blocks=block,...` is correct, `block` could be a standard Python object 'as-is', i.e. you don't need to manually serialize it with `json.dumps()`, just ensure you fill right primitives when copying+pasting from Block Kit Builder (i.e. use Python `True`/`False` instead of JS `true`/`false` etc.).
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论