0

I need some help with VBA post to SLACK using markdowns. I have made a macro that sends text to my Slack channel, but I was not able to find the right way to makdown my text so that output to slack would be posted in a formatted manner showing hyperlinked text but not the whole text string, or would post an image but not image url.

Here is my working code below.

Sub BOT_SLACK_POST()
Dim HTTP As Object, Htmldoc As New HTMLDocument
Set HTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim send_text As String

send_text = "<https://stackoverflow.com/questions/ask|Ask a public question> \n :star: Text in a new line \n https://cormullion.github.io/assets/images/slackmojif/slackanimation.gif"

    Dim URL$, body$
    JBody = "{""Contents"":""" & send_text & """}"
    URL = "My_Slack_Channel_Webhook_URL"
    HTTP.Open "POST", URL
    HTTP.setRequestHeader "Content-Type", "application/json"
    HTTP.send JBody
    
End Sub

This is how the output to Slack channel looks.

Output To Slack

Any advise is much appreciated.

1 Answers1

1

Checkout Slack Block Kit (https://api.slack.com/block-kit) for formatting messages.

Regarding friendly URLs. Use the following format:

    <http://www.google.com|This message *is* a link to Google>

Regarding images - You can use the Block Kit Builder to build sample JSON that has an image. Unfortunately, to work, the URL to the image has to be on the public Internet in order for Slack to retrieve it and include it in the message. If you're images are private, here's a discussion with two suggestions (I've tested both and they both work): How to create a Slack message containing an uploaded image?

Screenshot of example block kit formatting

Here's the JSON included in the sample I posted:

{
"blocks": [
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Here's a message that includes some *bold text*.  The next section has an image."
        }
    },
    {
        "type": "image",
        "title": {
            "type": "plain_text",
            "text": "Here's an image",
            "emoji": true
        },
        "image_url": "https://assets3.thrillist.com/v1/image/1682388/size/tl-horizontal_main.jpg",
        "alt_text": "marg"
    },
    {
        "type": "divider"
    },
    {
        "type": "actions",
        "elements": [
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "This button is a link",
                    "emoji": true
                },
                "value": "click_me_123",
                "url": "https://stackoverflow.com"
            }
        ]
    }
]
}