Slack POST message to multiple channels at once

Hello,

It would be nice if it would be possible to automatically send a message to multiple channels using the slack node.
I end up with several slack nodes with exactly the same configuration just to send the message on several channels at the same time :

I know that the method chat.postMessage from the slack API doesn’t natively allow you to do that.
I think it would be very convenient to have a loop on sending the message if you pass a channel dictionary.

What do you think ?

Thanks for this great tool.

Jules

I guess I can also do an upstream function that returns my two channels :

return [
  {
    json: {
      chan: "#chan1"
    }
  },
   {
    json: {
       chan: "#chan2"
    }
  }
]

And then call the Slack node to make some kind of loop. It might be a little cleaner, but it doesn’t really solve my problem.

Hello @Jules_DUVIVIER welcome to the community!

Yes, makes sense. The thing would just be, that if one item would enter the Slack-Node, the output would have to consist of 3, as there would be 3 responses from Slack. Not sure if that could be confusing for people (honestly no idea). Definitely worth thinking about!

Yes, something similar like you described would be also this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json.message = 'this is the message';\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        450
      ]
    },
    {
      "parameters": {
        "functionCode": "const channels = ['channel1', 'channel2', 'channel3'];\n\nreturn channels.map(channelName => {\n  return {\n    json: {\n      channelName,\n      message: items[0].json.message,\n    }\n  }\n});\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        450
      ]
    },
    {
      "parameters": {
        "channel": "={{$node[\"Function1\"].json[\"channelName\"]}}",
        "text": "={{$node[\"Function1\"].json[\"message\"]}}",
        "attachments": [],
        "otherOptions": {}
      },
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        1000,
        450
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hello @jan, thanks for your quick answer !

Yes sure, thank you. It’ll work and it’s cleaner. But at several points in my workflow I need to send a slack message to my 3 (or more) channels. So I have to have this function before each slack node.

Ideally I wish I could use my “Set” node to be able to define an array of channels and use this variable every time I call my slack node. I don’t know if my use-case is clear.

At the moment my workflow (my first one!) looks like this:

In my “Set vars” Node, I have defined two variables “Chan1” and “Chan2” that I use when I send a message but I have to duplicate my slack nodes.

(In my current workflow, I’m sending the message to 2 channels not 3)

For now you could “simply” create a separate workflow to send Slack-Messages with this workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return items[0].json.channels.split(',').map(channelName => {\n  return {\n    json: {\n      channelName,\n      message: items[0].json.message,\n    }\n  }\n});\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        450
      ]
    },
    {
      "parameters": {
        "channel": "={{$node[\"input-data1\"].json[\"channelName\"]}}",
        "text": "={{$node[\"input-data1\"].json[\"message\"]}}",
        "attachments": [],
        "otherOptions": {}
      },
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        1000,
        450
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "channels",
              "value": "channel1,channel2,channel3"
            },
            {
              "name": "message",
              "value": "this is the message"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        450
      ]
    }
  ],
  "connections": {
    "Function1": {
      "main": [
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The Set-Node is only an Example and would be in the existing workflow. You would just have to send the data to the new Slack-Workflow the same way. Then instead of having a Slack-Node in exising workflow, you would have the Set-Node and the Execute Workflow-Node there.

Thanks, that sounds like a good workaround ! I’ll dig that, thank you!

You are welcome. Happy to help! But fixing that properly in the node in the short-term will be a good idea.