【问题标题】:Slack API "attachments" not showingSlack API“附件”未显示
【发布时间】:2016-07-07 20:57:57
【问题描述】:

附件在以下代码中不起作用,response_type 也没有按应有的方式显示。我也尝试过使用 Python 的 Slack Client,但发生了完全相同的事情。

def send_message(channel_id, text):
    params = {
        "token" : token, 
        "username" : "NEW BOT",
        "channel" : channel_id,
        "text" : text,
        "response_type": "ephemeral",
        "attachments": [{ "text":"This is some text" }]
    }

    headers = {'content-type': 'application/json'}
    slack_api = 'https://slack.com/api/chat.postMessage'
    requests.get(slack_api, json=params, headers=headers)
    return

@app.route('/', methods=['GET', 'POST'])
def main():
    if sc.rtm_connect():
        sc.rtm_read()
        text = request.args.get("text")
        channel_id = request.args.get("channel_id")
        send_message(channel_id, text)
        return Response(), 200

【问题讨论】:

    标签: python slack-api


    【解决方案1】:

    response_type 字段只能在生成响应斜杠命令或消息按钮操作调用的消息时设置。它不能直接用chat.postMessage 设置,因为没有关于显示该临时消息的目标用户的上下文。

    chat.postMessage 的另一个怪癖是它目前不像传入的 webhook 那样接受 JSON。相反,您需要发送 application/x-www-form-urlencoded 种类的 POST 参数。更奇怪的是,attachments 字段实际上并没有作为 JSON 字符串发送,而是通过 URL 编码到参数中。

    还有一个提示,对于 chat.postMessage 和其他写入方法,您应该使用 HTTP POST 而不是 GET。

    【讨论】:

    • “更奇怪的是,附件字段实际上确实作为 JSON 字符串发送,但 URL 编码为参数。”。谢谢!发现这很痛苦。
    • 好帖子!对于所有寻找快速而肮脏的答案的人,请尝试将 "attachments": [{"title": "my title", "text": "my text", ... }] 替换为 "attachments": json.dumps([{"title": "my title", "text": "my text", ... }])
    • 神圣的 **** 。我一直在寻找年龄。谢谢!
    • 确实,这也发生在我身上。附件需要在 JSON 字符串中才能工作。 IE。 '[{"text": "hello"}]' 而不是 [{"text": "hello"}]
    【解决方案2】:
    attachments='[{"title": "Try these - ","text": " Text ", "mrkdwn_in":["text"]}]'
    

    将标题添加到附件。它在我的情况下有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多