Search code examples
pythonmarkdownbotsslack

Slack Bot - Python slack send markdown messages


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> \n\n - a \n-b" 
    ...:     
    ...:     )  

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

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

but didn't work. Help


Solution

  • 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: \n\n - a \n-b"
                }
            }
        ]
    )
    

    Output:
    Blockkit

    References: