Trying to post blocks to slack

Hi All, I am having trouble posting my blocks to slack. I am not sure whether to put the JSON in the text post field, or if I need to format the post within slack. I currently have a JSON blob that I’ve validated in the Slack block builder but I keep getting errors when I try to post with blocks, and if I use the text field it just posts the JSON to the slack channel without formatting it. Any ideas? I am adding the workflow here:

{
  "name": "Gdrive Playbook Notifier",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -240,
        570
      ]
    },
    {
      "parameters": {
        "channel": "cse-updates",
        "text": "=",
        "attachments": [],
        "otherOptions": {},
        "blocksUi": {
          "blocksValues": []
        }
      },
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        1910,
        770
      ],
      "credentials": {
        "slackApi": "n8n Bot"
      }
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyWeek",
              "hour": 13
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        -80,
        579
      ]
    },
    {
      "parameters": {
        "functionCode": "var curr = new Date;\nvar first = (curr.getDate() - curr.getDay()) +1;\nvar last = first + 4;\n\nvar firstday = new Date(curr.setDate(first));\nvar lastday = new Date(curr.setDate(last));\n\nbeginning_week = new Date(firstday.setHours(0,0,0,0));\nending_week = new Date(lastday.setHours(23,59,59,99));\n\nitems[0].json.from = beginning_week.toISOString();\nitems[0].json.to = ending_week.toISOString();\n\nreturn items;"
      },
      "name": "Start and end of week1",
      "type": "n8n-nodes-base.function",
      "position": [
        100,
        579
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "folder.scoped",
              "value": "1gaswxsAZsquyG_IA9JL0ChBPZtLNCGaT"
            },
            {
              "name": "folder.unscoped",
              "value": "1B46TbHO-MkCCV1_nGoeuDouFjWkXgzk3"
            }
          ]
        },
        "options": {}
      },
      "name": "Set PB Folder",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        280,
        579
      ]
    },
    {
      "parameters": {
        "operation": "list",
        "useQueryString": true,
        "queryString": "='{{$node[\"Set PB Folder\"].json[\"folder\"][\"unscoped\"]}}' in parents",
        "options": {
          "fields": [
            "*"
          ]
        }
      },
      "name": "Get Unscoped PBs",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        440,
        780
      ],
      "credentials": {
        "googleApi": "TC Service Account"
      }
    },
    {
      "parameters": {
        "operation": "list",
        "useQueryString": true,
        "queryString": "='{{$node[\"Set PB Folder\"].json[\"folder\"][\"scoped\"]}}' in parents",
        "options": {
          "fields": [
            "*"
          ]
        }
      },
      "name": "Get Scoped PBs",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        470,
        509
      ],
      "credentials": {
        "googleApi": "TC Service Account"
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "pb.creator",
              "value": "="
            },
            {
              "name": "pb.createddate",
              "value": "={{$json[\"createdTime\"]}}"
            },
            {
              "name": "pb.name",
              "value": "={{$node[\"Split Scopes\"].json[\"name\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set scoped pb dict",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        840,
        349
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "creator",
              "value": "={{$json[\"owners\"][0][\"displayName\"]}}"
            },
            {
              "name": "dateAdded",
              "value": "={{$json[\"pb\"][\"createddatepretty\"]}}"
            },
            {
              "name": "folder_name",
              "value": "={{$json[\"name\"]}}"
            },
            {
              "name": "link",
              "value": "={{$json[\"webViewLink\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set unscoped pb dict",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        980,
        960
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split Scopes",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        650,
        349
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches1",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        610,
        960
      ]
    },
    {
      "parameters": {
        "value": "={{$node[\"SplitInBatches1\"].json[\"createdTime\"]}}",
        "dataPropertyName": "pb.createddatepretty",
        "toFormat": "MMMM DD YYYY",
        "options": {}
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        800,
        960
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [],
          "boolean": [
            {
              "value1": "={{$node[\"SplitInBatches1\"].context[\"noItemsLeft\"]}}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF no more batches",
      "type": "n8n-nodes-base.if",
      "position": [
        1290,
        790
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "slackupdateformatted",
              "value": "={\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"`{{$json[\"dateAdded\"]}}` *{{$json[\"folder_name\"]}}* _ Added by {{$json[\"creator\"]}}_\"},\"accessory\":{\"type\":\"button\",\"url\":\"{{$json[\"link\"]}}\",\"text\":{\"type\":\"plain_text\",\"text\":\"Google Folder\",\"emoji\":true}}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Build Slack Post",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1170,
        960
      ]
    },
    {
      "parameters": {
        "functionCode": "const slackupdates = []\n\nlet counter = 0;\ndo {\n  try {\n    const items = $items(\"Build Slack Post\", 0, counter).map(item => item.json);\n    slackupdates.push.apply(slackupdates, items);\n  } catch (error) {\n    return [{json: {slackupdates}}];  \n  }\n\n  counter++;\n} while(true);\n"
      },
      "name": "Merge Results",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1440,
        770
      ]
    },
    {
      "parameters": {
        "functionCode": "let message = '';\n\nfor (const item of items[0].json.slackupdates) {\n  message += `${item.slackupdateformatted},`;\n}\n\nreturn [\n  {\n    json: {\n      message,\n    }\n  }\n];\n"
      },
      "name": "Create Message",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1590,
        770
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "slackmessage",
              "value": "={\"attachments\":[{\"color\":\"#f2c744\",\"blocks\":[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\" :loud_sound: *IN CASE YOU MISSED IT* :loud_sound:\"}},{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"Replay our screening of *Threat Level Midnight* and pick up a copy of the DVD to give to your customers at the front desk.\"},\"accessory\":{\"type\":\"button\",\"text\":{\"type\":\"plain_text\",\"text\":\"Watch Now\",\"emoji\":true}}},{\"type\":\"divider\"},{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\":calendar: |   *UPCOMING EVENTS*  | :calendar: \"}},{{$json[\"message\"]}}{\"type\":\"divider\"}]}]}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1750,
        770
      ]
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Start and end of week1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start and end of week1": {
      "main": [
        [
          {
            "node": "Set PB Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set PB Folder": {
      "main": [
        [
          {
            "node": "Get Scoped PBs",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Unscoped PBs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Scoped PBs": {
      "main": [
        [
          {
            "node": "Split Scopes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Unscoped PBs": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Scopes": {
      "main": [
        [
          {
            "node": "Get Scoped PBs",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set scoped pb dict",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches1": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set unscoped pb dict": {
      "main": [
        [
          {
            "node": "Build Slack Post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Set unscoped pb dict",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF no more batches": {
      "main": [
        [
          {
            "node": "Merge Results",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Slack Post": {
      "main": [
        [
          {
            "node": "IF no more batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Results": {
      "main": [
        [
          {
            "node": "Create Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Message": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "3"
}

Can you share the slack block?

I guess it depends. If the block is supported by the node’s UI you can use that. Else, you can turn JSON Parameter on and add the JSON you have in the Blocks parameter.

Saw your workflow and I will say that adding the expression in the blocks field instead of the text will fix the issue.

3 Likes

Did it work?

Hi all, I figured it out! The solution was to remove the first string from the JSON and follow your instructions regarding making sure to put it in the blocks section, and to make sure JSON was enabled. The last puzzle piece was to remove the block at the beginning and end from the slack validator. See screenshot for explanation.

4 Likes

@angel this was very helpful.
Thx!

1 Like