【问题标题】:Slack API: How to reply for an attachment action?Slack API:如何回复附件操作?
【发布时间】:2016-12-23 02:10:16
【问题描述】:

我创建了一个与 Slack 的集成作为 WebHook 应用程序。代码是使用chat.postMessage方法向slack频道发送消息,并带有一些附件动作,然后当用户单击动作按钮时,我向他发送一条成功消息。我正在尝试做这样的事情:

https://api.slack.com/img/api/message_guidelines/Example_6.gif

问题是当我尝试发送成功消息时。 Slack 只接收答案的文本部分。代码如下:

$message = 'Pre-text message';

$attachments = array(
  array(
    "title" => 'Title message',
    "author_name" => 'My name',
"author_link" => 'https://www.facebook.com/',
"author_icon" => 'https://graph.facebook.com/v2.6/picture',
"image_url" => 'https://i.scdn.co/image',
  ),
);

$answer = array(
  'text' => $message,
  'attachments' => json_encode($attachments)
)

如上图所示,如何让 Slack 使用附件部分显示答案?如果我在 $answer 上评论文本部分,Slack 会向用户显示一个错误('哦,不,出了点问题。请再试一次。')。非常感谢您的帮助。

【问题讨论】:

  • 您永远不会在部分响应中使用json_encode(),而是在整个响应中使用它,无论它是什么类型的消息。

标签: api chat attachment postmessage slack


【解决方案1】:

我找到了解决方案。在这里发帖以帮助有同样问题的人。当您发布消息时,应该对附件部分进行 json_encode,但在发布附件操作答案时,则不是必需的。这是解决方案:

$message = 'Pre-text message';

$attachments = array(
  array(
    "title" => 'Title message',
    "author_name" => 'My name',
    "author_link" => 'https://www.facebook.com/',
    "author_icon" => 'https://graph.facebook.com/v2.6/picture',
    "image_url" => 'https://i.scdn.co/image',
  ),
);

$answer = array(
    'text' => $message,
    'attachments' => $attachments
)

【讨论】:

  • 你能分享完整的代码,包括附件块吗?我正在尝试做同样的事情,但不知道如何在按钮的点击和期望的结果(浏览网页)之间建立联系
  • 嗨@hilaf,我现在更新了答案以包含完整的代码。抱歉耽搁了。
  • 如何在 def - def slack_msg(): data = { "token": "", "channel": "", "icon_emoji":"", "attachments": json 中实现.dumps([ { "text":msg, "fallback":"msg", "color":"", "attachment_type":"", } ]), "username": "", "mrkdwn": "true ", } response = requests.post("slack.com/api/chat.postMessage", data=data, verify=False) 如果我用您的 $attachments 值替换 json.dumps 之后的所有内容,它会起作用吗?
  • 我认为您实际上应该删除json.dumps。我会选择类似的东西:slack_msg.py
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 2019-12-09
  • 1970-01-01
相关资源
最近更新 更多