Note: This answer is based on my experience building and running the Simple Poll Slack app. I'm not completely certain what the Slack-approved "most correct" approach is, but I have a pretty good sense of what works in practice.
The short answer here is that block_ids don't need to be unique in most cases. The majority of our block_ids are not unique. For example, we a have a modal/view in which users can set up a recurring poll. The block_id for this block is set_up_recurring_poll
. There is no issue with this block_id not being unique.
So that's our default approach: Choose non-unique, human-readable names for block_ids. As you mentioned, these are then also the easiest to parse when Slack sends over the block_actions
/view_submission
payloads.
In practice, Slack uses the block_id as a way to maintain state within interactive/input blocks. Specifically the block_id uniqueness is used to decide whether to update/replace the block you previously specified with a new block or whether to keep the existing block. Essentially a form of caching.
That's a bit abstract, so I'm going to give two examples:
- Updating a surface that has a
plain_text_input
block
- Updating a surface that has a
static_select
menu
Updating a surface that has a plain_text_input
block
- Imagine your app opened a view that has a
plain_text_input
block.
- A user enters something into this block, for example "Hello World"
- A user then clicks on a button in the view and your app decides to update the view with
views.update
but keeping that same plain_text_input
block in the view
If in this call to views.update
, your app kept the block_id the same as the block_id your app used in step 1, then the "Hello World" the user entered will still be there.
If in this call to views.update
, your app used a different block_id to the one your app used in step 1, then the "Hello World" the user entered is gone and the user will see an empty input field.
So in this case, if your intention is to clear the plain_text_input
, then use a different block_id. If your intention is to maintain the user input, then use the same block_id.
Updating a surface that has a static_select
menu
- Imagine your app posted a message that has a
static_select
menu
- A user selects one of the options in the
static_select
menu
- Your app then updates the underlying message (using
chat.update
). The only change between the previous and updated message is that your app added another option to the static_select
menu
In this case, if the block containing the static_select
menu had the same block_id in the updated message, then the new option would not show up in the static_select
menu. Slack sees that the block_id is the same as before, and decided not to update the actual content of the block.
So in this case, if your desire is to add another option to the static_select
menu, then you need to use a new block id.
When we run into a case like this, we tend to append a randomly generated suffix to our existing, human-readable block id. So instead of my-static-select-menu
, we'd instead use my-static-select-menu&5jRMA
as our block_id. Then later when we parse the block_id we look only at the value before the &
The overall approach that I would recommend and that has worked for us:
Default to using non-unique, human-readable block_ids. And if you happen to run into a case, where Slack is caching the block content based on the id and this is preventing you from doing something, then add an at-runtime-generated, random suffix to the block id, which will bust Slack's cache.
what exactly is the scope of uniqueness for block_ids?
In my mental model, block_ids need to be unique only in the context of their directly adjacent blocks (ie the array of blocks: []
) that any given block_id exists within. Or said another way, they need to be unique for the instance of the surface they are a part of (message, view, app home)
So you can't have the same block_id twice in the exact same message, view, or app_home page – but it's totally fine to use the same, non-unique block id to for example power the "Upgrade" button in a message. Even if that same message is posted several times
One final thought: For questions like these and similar, you might also find it useful to send a note to developers@slack.com – there are several folks on their Customer Success team which are dedicated to answering technical questions like this one. They've helped me in the past!