Search code examples
slackslack-api

How might I share a message URL that renders a preview the same way Slack does?


I'd like to include Slack message URLs in a post message (using blocks, if at all possible) in such a way that it's rendered the same way Slack would render the message URL; "Posted in #channel | Dec 11th | View message", etc.

I've tried using the Block Builder Kit to test this out. Slack will render this URL as plain text (which I guess is obvious), but verbatim: false isn't an option for plain_text. My options seem limited and I'm wondering if maybe I'm overlooking an alternative way to do this?

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "https://mycompany.com/archives/C0H0DMAEB/p1576068001171300"
            }
        }
    ]
}

Example of what I mean showing Block Builder vs. URL paste: https://i.sstatic.net/Wy9YC.jpg

Any guidance would be greatly appreciated.


Solution

  • What you need then is to avoid using blocks since text based links don't unfurl automatically. You need to pass the "unfurl_links": true in the chat.postMessage so if you are using Python's slackclient as in my case it would be as follows

    response = client.chat_postMessage(
      channel=channel_id,
      text='<https://mycompany.com/archives/C0H0DMAEB/p1576068001171300>',
      unfurl_links = True,
    
    )
    assert response["ok"]
    

    You can also dictate to slack how to behave when it encounters links in messages find more details here