Slack API 中的某些部分不起作用。

huangapple go评论70阅读模式
英文:

Certain sections in slack-api blocks don't work

问题

尝试将此示例负载在块工具生成器中使用时,使用它在Python中时不起作用。原因是它不识别类型为tags的一些值,可能是emoji=True。但将负载格式化为JSON字符串并发送也不起作用。我必须删除在块中创建问题的某些部分。

根据此示例,Slack API将块作为字典处理:

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&#39;t work when using it in python.
Reason being it doesn&#39;t recognise some values in type `tags`, possibly emoji=True. But sending the payload formatting it to json string also doesn&#39;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&#39;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&amp;%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 = [
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;Hello, &lt;lob&gt; Team, Varanus wants you to know the Seo Anomalies Found.\n\n *Please review the Notifications below:*&quot;
            }
        },
        {
            &quot;type&quot;: &quot;divider&quot;
        },
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;*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&quot;
            },
            &quot;accessory&quot;: {
                &quot;type&quot;: &quot;image&quot;,
                &quot;image_url&quot;: &quot;https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg&quot;,
                &quot;alt_text&quot;: &quot;alt text for image&quot;
            }
        },
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;*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.&quot;
            },
            &quot;accessory&quot;: {
                &quot;type&quot;: &quot;image&quot;,
                &quot;image_url&quot;: &quot;https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg&quot;,
                &quot;alt_text&quot;: &quot;alt text for image&quot;
            }
        },
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;*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 &amp; rice powder.&quot;
            },
            &quot;accessory&quot;: {
                &quot;type&quot;: &quot;image&quot;,
                &quot;image_url&quot;: &quot;https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg&quot;,
                &quot;alt_text&quot;: &quot;alt text for image&quot;
            }
        },
        {
            &quot;type&quot;: &quot;divider&quot;
        },
        {
            &quot;type&quot;: &quot;tags&quot;,
            &quot;elements&quot;: [
                {
                    &quot;type&quot;: &quot;button&quot;,
                    &quot;text&quot;: {
                        &quot;type&quot;: &quot;plain_text&quot;,
                        &quot;text&quot;: &quot;Farmhouse&quot;
                    }
                },
                {
                    &quot;type&quot;: &quot;button&quot;,
                    &quot;text&quot;: {
                        &quot;type&quot;: &quot;plain_text&quot;,
                        &quot;text&quot;: &quot;Kin Khao&quot;
                    }
                },
                {
                    &quot;type&quot;: &quot;button&quot;,
                    &quot;text&quot;: {
                        &quot;type&quot;: &quot;plain_text&quot;,
                        &quot;text&quot;: &quot;Ler Ros&quot;
                    }
                }
            ]
        }
    ]
    
    response = client.chat_postMessage(blocks=block, text=&quot;A New Notification&quot;, channel=&quot;Channel-ID&quot;)

**Without the tags section i.e (&quot;type&quot; : &quot;tags&quot;), it works** 

    block = [
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;Hello, &lt;lob&gt; Team, Varanus wants you to know the Seo Anomalies Found.\n\n *Please review the Notifications below:*&quot;
            }
        },
        {
            &quot;type&quot;: &quot;divider&quot;
        },
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;*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&quot;
            },
            &quot;accessory&quot;: {
                &quot;type&quot;: &quot;image&quot;,
                &quot;image_url&quot;: &quot;https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg&quot;,
                &quot;alt_text&quot;: &quot;alt text for image&quot;
            }
        },
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;*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.&quot;
            },
            &quot;accessory&quot;: {
                &quot;type&quot;: &quot;image&quot;,
                &quot;image_url&quot;: &quot;https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg&quot;,
                &quot;alt_text&quot;: &quot;alt text for image&quot;
            }
        },
        {
            &quot;type&quot;: &quot;section&quot;,
            &quot;text&quot;: {
                &quot;type&quot;: &quot;mrkdwn&quot;,
                &quot;text&quot;: &quot;*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 &amp; rice powder.&quot;
            },
            &quot;accessory&quot;: {
                &quot;type&quot;: &quot;image&quot;,
                &quot;image_url&quot;: &quot;https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg&quot;,
                &quot;alt_text&quot;: &quot;alt text for image&quot;
            }
        },
        {
            &quot;type&quot;: &quot;divider&quot;
        }
    ]
    
    response = client.chat_postMessage(blocks=block, text=&quot;A New Notification&quot;, channel=&quot;Channel-ID&quot;)


If I use json.dumps(block) or str(block), it gives and error `invalid_block`

Using the suggestions to use json.dumps(blocks) doesn&#39;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作为消息预览布局验证器会在 `&quot;type&quot;: &quot;tags&quot;` 附近失败显示以下信息

无效数值: "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”表面的此类区块。总之,这根本不是现有的类型。
在原始示例中,这是一个 [`&quot;type&quot;: &quot;actions&quot;` 区块](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 &quot;Message Preview&quot; layout, the validator will fail near the `&quot;type&quot;: &quot;tags&quot;` 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 &quot;Message&quot; 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 [`&quot;type&quot;: &quot;actions&quot;` 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 &#39;as-is&#39;, i.e. you don&#39;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>

huangapple
  • 本文由 发表于 2023年7月3日 12:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定