Slack API"附件"未显示

Sur*_*oor 1 python slack-api

附件在以下代码中不起作用,也不是response_type应该显示的.我也尝试过使用Python的Slack客户端,但完全相同的事情正在发生.

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
Run Code Online (Sandbox Code Playgroud)

Tay*_*ary 8

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

另一个怪癖chat.postMessage是,它目前不接受像传入webhooks那样的JSON.相反,您需要发送application/x-www-form-urlencoded各种POST参数.即使是奇怪的,该attachments字段实际上并不是作为一串JSON发送的,而是URL编码成一个参数.

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

  • 好帖子!对于每个寻找快速和肮脏答案的人,请尝试替换"附件":[{"title":"my title","text":"my text",...}]`with""attachments" :json.dumps([{"title":"我的标题","文字":"我的文字",......}]) (5认同)