Search code examples
slackslack-api

Can i send custom properties/data in slack message attachments?


I want to send some custom properties in the attachment for interactive messages and retrieve them back in the action response. is there a way to do this?


Solution

  • Yes, that is possible. However, it only works well for small sets of data.

    Assuming we are talking about buttons the normal approach would be to use the value field of an action to transfer custom data based on which button the user clicked back to your app. The field is a normal string within a JSON message, which is send by POST request to your app. So it can in principle contain a whole data set, not only a single value. All you need to do is include it in the button attachment that is send to Slack and your app will receive the respective value field back. (depending on what data you want to send you might need to encode it, e.g. you want to encode binary data into base64, so that is can be transferred as JSON string)

    I have used it successfully in one of my apps to transfer serialized objects containing information about the user's application context.

    There is one caveat though, that caused me to later abandon this approach again. As I found out the field length is limited, so if your string is too long you might end up with truncated data. In my estimation the limit is about 2.000 chars, but I do not have a definitive number.

    Instead of transferring all data in the attachment, I now keep the user application context in a server session (PHP) and only transfer IDs through the value field of my buttons.

    Conclusion: If you have small sets of data you can transfer them through the value field. If you have larger sets of data I would not recommend it.