Slack API “attachments” not showing

帅比萌擦擦* 提交于 2020-01-06 05:07:27

问题


Attachments aren't working in the following code and neither is the response_type showing as it should be. I've also tried using Python's Slack Client, but the exact same thing is happening.

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

回答1:


The response_type field can only be set when producing a message in response to a slash command or message button action invocation. It can't be set directly with chat.postMessage, as there's no context on the target user for whom to display that ephemeral message.

Another quirk about chat.postMessage is that it doesn't currently accept JSON like incoming webhooks does. Instead, you need to send POST parameters of the application/x-www-form-urlencoded variety. Even quirkier, that attachments field actually doesn't get sent as a string of JSON, but URL-encoded into a parameter.

One more tip, with chat.postMessage and other write methods, you should use an HTTP POST instead of a GET.




回答2:


attachments='[{"title": "Try these - ","text": " Text ", "mrkdwn_in":["text"]}]'

Add the title to the attachment. It worked in my case.



来源:https://stackoverflow.com/questions/38255143/slack-api-attachments-not-showing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!