将消息发布到 Slack App Home 频道时缺少消息附件

Message Attachments missing when posting message to Slack App Home channel

我为 Slack 构建了一个 Workspace App,它现在带有一个频道(在 Apps header 下),用户可以在其中向应用程序本身发送消息。这会触发一个 message.app_home 事件,我的后端收到。我想使用 chat.postMessage 回复该消息,但只有消息文本出现在回复中。 None 我发送的附件出现在频道中。作为对 Slash 命令请求的 JSON 响应的一部分返回此结构可以正常工作。

这是我发送的附件结构:

(
    {
        "title": "Create a space, optionally setting its name and adding other users",
        "text": "`/luffa create [space name] [teammates]`\nExample: `/luffa create Marketing Campaign 2018 @john @kate`",
        "color": SLACK_COLOR,
    },
    {
        "title": "Start a new meeting, optionally strating it in the space matching the provided name",
        "text": "`/luffa start [space name]`\nExample: `/luffa start Marketing Campaign 2018`",
        "color": SLACK_COLOR,
    },
    {
        "title": "Search Luffa",
        "text": "`/luffa search [query]`\nExample: `/luffa search interviews before:Yesterday `",
        "color": SLACK_COLOR,
    },
    {
        "text": "There is more help available in our Help Center: https://help.okluffa.com/",
    },
)

我正在使用 slackclient Python 库来包装我对 Slack 的调用 API。

没有返回任何错误消息,并且基于 documentation 的结构似乎是正确的。是不是少了什么?

需要检查的小事 - 尝试删除 attachments 结构中每个最后一个值的多余逗号,因为插入 into the Slack message tester here:

时会导致验证错误

问题是我传递给 slackclient 1.2.1 的数据结构是一个 tuple 对象。该库将仅序列化为 JSON listdict 对象。 (参见 slackrequest.py,第 94 行,if isinstance(v, (list, dict)):)。传递元组或与此相关的任何其他可迭代对象将不会正确序列化,Slack API 将忽略附件。这不是 JSON 对请求的响应的问题,因为 Python 的 JSON 序列化程序将所有可迭代对象转换为 JSON 数组。

我通过将 list 传递给 slackclient 方法解决了这个问题。