Search code examples
slackslack-api

How to neglect emojis when posting messages to slack using slack api?


I have a slack POST api call which I am doing from my application:-

slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=False, username="Slack bot")

The issue which I am encountering is that the response may contain error logs and can have text like

Error in /:hive:/SomeError....

When this gets posted to slack, it thinks of :hive: as an emoji and prints emoji of hive instead of :hive:, which I don't want.

How to do this? I have tried putting mrkdwn as false, but it didn't help. Can someone help me out here?


Solution

  • I think you have two options:

    • You can directly format your string as code. Then Slack will not parse for emojis.

    • You can send your message in a block, which gives you the option to explicitly turn off emojis.

    Example

    import slack
    import os
    import json
    
    client = slack.WebClient(token=os.environ['SLACK_TOKEN'])
    
    client.chat_postMessage(        
        channel='general',
        text='`Error in /:hive:/SomeError....`'
    )
    
    client.chat_postMessage(        
        channel='general',    
        blocks=json.loads("""
            [
                {
                    "type": "section",
                    "text": {
                        "type": "plain_text",
                        "text": "Error in /:hive:/SomeError....",
                        "emoji": false
                    }
                }
            ]
        """)
    )