Slack Bot – Python slack send markdown messages

Question:

I’m trying to send markdown messages in slack using SlackBot but I’m unable to find the documentation, All I got is this:

response = client.chat_postMessage( 
    ...:     channel='#testing-bot', 
    ...:     text="Hello world! <@USerID> nn - a n-b" 
    ...:     
    ...:     )  

I want to send MArkdown MEssages, instead of the text one
I tried:

    ...:     channel='#testing-bot', 
    ...:     mkdwn="Hello world! <@UNVD64N02> nn - a n-b" 
    ...:     
    ...:     )  

but didn’t work. Help

Asked By: Charanjit Singh

||

Answers:

You need to send in the channel id (it will be alpha-numeric string) in instead of channel name (#testing-bot).

Update:
You can also use block kit which is a UI framework for slack apps. It comes with a block kit builder which can be used for real-time view of block code. Added the references below for both.

response = client.chat_postMessage(
    channel="", # channel ID
    text="",
    blocks=[
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Hello world! <@UNVD64N02> :tada: nn - a n-b"
            }
        }
    ]
)

Output:
Blockkit

References:

Answered By: stud3nt

I see you’re using python’s sdk, chat_postMessage() API. According to the docs, the key mrkdwn takes boolean value (which is True by default).

Example: Below code will send the message as: "Hello, World!"

response = client.chat_postMessage(
    channel="random",
    text="Hello, *World!*",
    mrkdwn=True,
)

For more message formatting, see Basic formatting with mrkdwn

Answered By: Saurabh Chopra
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.