Patrick Lightbody
Patrick Lightbody

Reputation: 4534

How do you save the Slack modal view ID for future updates?

I'm using Slack's modals + their new BlockKit interactive actions. I have a modal dialog with several section's that include select menu accessories. When one of those accessories changes (ex: user preference goes from Yes -> No), I want to update the modal's view to reflect the change in some contextual text.

The docs outline two ways to update a model. Because I'm not using the traditional modal inputs, I have to update the modal via the API and not via response_action. Proof: I don't even receive a view_submission payload at all... I receive a blockkit_action payload.

The docs conflict a bit. In the above link they say that to update a view you must pass in the returned ID of the view when it was opened:

Remember the view.id that was included in the success response when you used views.open earlier? We hope you kept it, because you can now use it to update that view.

But when you look at the docs for the views.open and views.update, it appears there is a second option: external_id. The views.update docs say this:

A unique identifier of the view set by the developer. Must be unique for all views on a team. Max length of 255 characters. Either view_id or external_id is required.

Initially I got everything working really nicely by choosing an external ID of myapp-mymodal-[userID]. But as soon as I tried opening the modal simultaneously on my Desktop + Mobile client, I started getting internal_error responses and have been unable to open the modal since!

So I started looking into saving the view ID, per the first quote. My problem is: that ID changes every time the modal opens. I don't understand how I am expected to possibly keep track of the 0-2 "active" view IDs that might be taking place across a user's Desktop + Mobile clients (or more than 2 if they have an iPad, a second laptop, etc).

I tried to look for some sort of unique client ID that I could either use to include in the external_id or in my view_id persistence logic, but given that Slack's API appears to be sesssion-less I couldn't find any such thing.

How do others solve this problem?

Upvotes: 4

Views: 2209

Answers (1)

Zach Dunlap
Zach Dunlap

Reputation: 41

Try using the private_metadata field in the View

I initially did the same thing you did, which worked, but I wanted to avoid that edge case if possible. What I figured out, is that I could pass the external_id that I created when opening the view into the view's metadata, and then retrieve it when I receive an interaction.

When I set the external_id I set it to the userID + the current time (so it's always unique, even if the user opens the modal from different devices)

externalID := UserID + time.Now().String()

and pass that both as the external_id for the view, and as a string into the private_metadata.

PrivateMetadata string `json:"private_metadata"`

When the user interacts with one of my blocks and Slack sends me the interactive message, I retrieve the metadata like this:

Request.Payload.View.PrivateMetadata

I wrote my Slack app in Go, but I tried to make it clear for any language. Let me know if anything is unclear or needs expanding on - this is my first time giving back to the Stackoverflow community.

Upvotes: 4

Related Questions