简体   繁体   中英

Slack API: Can't send attachments

I'm trying to send an attachment using Slack's Python Client but whenever I do I fail. I tried sending it with the Tester too but it still didn't work. Either I get {"ok": false,"error": "no_text"} or if I have the text property only the text is going to be sent. This is how I do it. I searched too but didn't found anything.

       attachment = json.dumps([{"attachments": [{"fallback": "Reddit Message","color": "#448aff","pretext":"You've got a new Message!","author_name": "Reddit","author_link": "https://reddit.com","author_icon": "imageurl","title": "Reddit Message","title_link": "https://reddit.com/message/inbox","text": "This is what I know about it.","fields": [{"title": "Author:","value": str(item.author),"short": "true"},{"title": "Subject: ","value": str(item.subject),"short": "true"},{"title": "Message:","value": str(item.body),"short": "false"}],"footer": "Reddit API","footer_icon": "anotherimageurl"}]})

sc.api_call("chat.postMessage",channel="U64KWRJAU",attachments=attachment,as_user=True)

Help would be appreciated. This should make sense but I don't get it why it doesn't work

From your reference, you need to pass attachment as a list. You won't need to have the attachments key in a dict containing the list.

attachment = json.dumps([
    {
        "fallback": "Reddit Message",
        "color": "#448aff",
        "pretext":"You've got a new Message!",
        "author_name": "Reddit",
        "author_link": "https://reddit.com",
        ....
    }
])

sc.api_call(
    "chat.postMessage", channel="U64KWRJAU",
    attachments=attachment, as_user=True)

I met the same issue and found the solution.
The problem is that if only attachments field is added into the payload, it will report no_text error. But if text field is added, slack message will only show the text content.

The solution:
When we want to display attachments , we need to add a basic blocks field instead of text field. Something like

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "bar"
            }
        }
    ],
    "attachments": [
        {
            "color": "#FF0000",
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "foo"
                    }
                }
            ]
        }
    ]
}

If putting the above payload into the Slack build kit , it will be a misleading. That's also why I stuck with the issue.

I would recommend to use chat.postMessage test to debug the payload. It will work like a charm.

https://stackoverflow.com/a/72036841/3409400

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM