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

时间:2018-07-17 17:37:56

标签: python slack slack-api

我为Slack构建了一个Workspace App,它现在带有一个通道(在Apps标题下),用户可以在其中向应用程序本身发送消息。这触发了 我的后端收到的 message.app_home 事件。我想使用chat.postMessage响应该消息,但是响应中仅显示消息文本。我发送的附件都没有出现在频道中。将这种结构作为对Slash Command请求的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的结构似乎是正确的。那里缺少什么吗?

2 个答案:

答案 0 :(得分:1)

需要检查的地方很小-尝试删除keptLetters = chosen.remove (removeLetter) 结构中每个最后一个值上的多余逗号,因为插入into the Slack message tester here时会引起验证错误:

Slack validation error

答案 1 :(得分:1)

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

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