Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
Anshul Dubey Avatar asked Nov 05 '25 05:11

Anshul Dubey


1 Answers

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
                }
            }
        ]
    """)
)
like image 119
Erik Kalkoken Avatar answered Nov 07 '25 12:11

Erik Kalkoken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!