User guide - Publish to Slack

this page is a WIP.

For making a workflow that pushes messages to slack, you'd first need to create a slack bot, and grant it these permissions

Once you do this, you will get a token starting with xoxb- - this is the bot token you'll need for further queries

Before we begin exploring that API and making a workflow of the same, you'll need to invite the bot to your desired channel as well. You can do this by /invite @BOTNAME in the channel.

The next thing we need is the channel ID, either you can fetch by going into the slack workspace and fetching the channel ID or you can get the channel ID by using THIS Slack API & docs to get it.

A sample message to your channel looks like this -

curl --location --request POST 'https://slack.com/api/chat.postMessage' 
--header 'Authorization: Bearer xoxb-<your bot token>' 
--form 'channel="<channel-id>"' 
--form 'text="<text message>"' 
--form 'attachments="[{
                    "fallback\": \"<a compulsory fallback message to show instead if the image doesnt go through>\",
          "image_url\": \"<the image url>"  
                                }]"'

Now, let's create a workflow for this. We can send just image, just text, or both through this.

So expected workflow input can be defined as:
The inputs: xoxb-token , channel-ID, text-message, image-url and fallback .

{               
        "text" : "<optional text in the message>",
        "creative_url":"<imahge-url>",
        "fallback":"<fallback-text> ",
        "channel_id":"<channel-id>",
        "bot_token":"xoxb-<bot-token>"

}

Workflow entities :
The URL - https://slack.com/api/chat.postMessage
The method - POST
The JSON body - Input from above
The header - Has the bot-token

{
  'name': 'send-slack-message',
  'scope': 'global',
  'flow': [{
                'concurrency': 1,
            'apispec': {
                        'url': 'https://slack.com/api/chat.postMessage',
                        'method': 'post',
                        'json_body':{'text': '{{INPUT.text}}','channel':'{{INPUT.channel_id}}','attachments':[{"fallback": "{{INPUT.fallback}}","image_url":"{{INPUT.creative_url}}"}]},
                        'headers': {'Authorization': 'Bearer {{INPUT.bot_token}}'}
                        },
                        'name': 'initiate slack message'
           }],
 'default_retry': {
             'total': 4,
             'connect': 3,
             'read': 3,
             'redirect': 3,
             'status': 3,
             'other': 0,
             'allowed_methods': ['GET', 'PUT', 'POST'],
             'status_forcelist': [413, 429, 503],
             'backoff_factor': 1,
             'raise_on_redirect': True,
             'respect_retry_after_header': True,
             'raise_on_status': True
             }
        }
 }

Did this page help you?