我为Slack构建了一个Workspace App,它现在带有一个通道(在Apps Headers 下),用户可以在其中向应用程序发送消息 . 这会触发我的后端收到的message.app_home事件 . 我想使用chat.postMessage回复该消息,但只有消息文本出现在响应中 . 我发送的所有附件都不会出现在 Channels 中 . 将此结构作为对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,结构似乎是正确的 . 那里缺少什么东西吗?
2 Answers
要检查的小东西 - 尝试删除
attachments
结构中每个最后一个值的额外逗号,因为这些在插入时会导致验证错误into the Slack message tester here:问题结果是我传递给slackclient 1.2.1的数据结构是
tuple
个对象 . 该库将仅序列化为JSONlist
或dict
对象 . (见slackrequest.py
,第94行,if isinstance(v, (list, dict)):
) . 传递元组或任何其他可迭代的元素将无法正确序列化,Slack API将忽略附件 . 这不是对请求的JSON响应的问题,因为Python的JSON序列化器将所有迭代转换为JSON数组 .我通过将
list
传递给slackclient方法解决了这个问题 .