Repository: slack-go/slack
Pull request #1444 (2025-06-08 01:38:37)

Support Slack Files in ImageBlockElement
Supporting slack_file in ImageBlockElement: 1438
- Add support for slack_file to ImageBlockElement
- update tests involving ImageBlockElement where ImageURL is set as a function arg
- fix support for slack_file in ImageBlock
- update tests involving ImageBlock where ImageURL is set as a function arg
- Update examples for both ImageBlock and ImageBlockElement

This does change the function signature of both NewImageBlockElement and NewImageBlock. If you would instead prefer slack-file specific versions of each of these functions so that the existing function signature can remain the same, let me know.


nlopes: Thank you @superturkey650. Would you mind adjusting the following:
- add a title
- rebase and squash the commits
- keep the signature for now and add a slack file specific (but add a TODO with the words BREAKING CHANGE so that when I do a breaking change release, I can clean this up)

I think at this stage, I'm not going for another BREAKING CHANGE release unless I really need to.
superturkey650: Added a title, squashed the commits, and reverted the breaking change in favor of a new function just for slack files.

Patch:
diff --git a/block_element.go b/block_element.go
index c27dc6f..944d864 100644
--- a/block_element.go
+++ b/block_element.go
@@ -117,9 +117,10 @@ func (s UnknownBlockElement) ElementType() MessageElementType {
 //
 // More Information: https://api.slack.com/reference/messaging/block-elements#image
 type ImageBlockElement struct {
-	Type     MessageElementType `json:"type"`
-	ImageURL string             `json:"image_url"`
-	AltText  string             `json:"alt_text"`
+	Type      MessageElementType `json:"type"`
+	ImageURL  string             `json:"image_url"`
+	AltText   string             `json:"alt_text"`
+	SlackFile *SlackFileObject   `json:"slack_file,omitempty"`
 }
 
 // ElementType returns the type of the Element
@@ -140,6 +141,16 @@ func NewImageBlockElement(imageURL, altText string) *ImageBlockElement {
 	}
 }
 
+// NewImageBlockElementSlackFile returns a new instance of an image block element
+// TODO: BREAKING CHANGE - This should be combined with the function above
+func NewImageBlockElementSlackFile(slackFile *SlackFileObject, altText string) *ImageBlockElement {
+	return &ImageBlockElement{
+		Type:      METImage,
+		SlackFile: slackFile,
+		AltText:   altText,
+	}
+}
+
 // Style is a style of Button element
 // https://api.slack.com/reference/block-kit/block-elements#button__fields
 type Style string
diff --git a/block_image.go b/block_image.go
index cdee5b4..2a914e5 100644
--- a/block_image.go
+++ b/block_image.go
@@ -41,3 +41,15 @@ func NewImageBlock(imageURL, altText, blockID string, title *TextBlockObject) *I
 		Title:    title,
 	}
 }
+
+// NewImageBlockSlackFile returns an instance of a new Image Block type
+// TODO: BREAKING CHANGE - This should be combined with the function above
+func NewImageBlockSlackFile(slackFile *SlackFileObject, altText string, blockID string, title *TextBlockObject) *ImageBlock {
+	return &ImageBlock{
+		Type:      MBTImage,
+		SlackFile: slackFile,
+		AltText:   altText,
+		BlockID:   blockID,
+		Title:     title,
+	}
+}


Test patch:
diff --git a/block_element_test.go b/block_element_test.go
index fead400..ed29868 100644
--- a/block_element_test.go
+++ b/block_element_test.go
@@ -14,6 +14,15 @@ func TestNewImageBlockElement(t *testing.T) {
 	assert.Equal(t, imageElement.AltText, "Location Pin Icon")
 }
 
+func TestNewImageBlockElementSlackFile(t *testing.T) {
+	slackFile := &SlackFileObject{URL: "https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png"}
+	imageElement := NewImageBlockElementSlackFile(slackFile, "Location Pin Icon")
+
+	assert.Equal(t, string(imageElement.Type), "image")
+	assert.Contains(t, imageElement.SlackFile.URL, "tripAgentLocationMarker")
+	assert.Equal(t, imageElement.AltText, "Location Pin Icon")
+}
+
 func TestNewButtonBlockElement(t *testing.T) {
 	btnTxt := NewTextBlockObject("plain_text", "Next 2 Results", false, false)
 	btnElement := NewButtonBlockElement("test", "click_me_123", btnTxt)
diff --git a/block_image_test.go b/block_image_test.go
index 801a142..0670bb6 100644
--- a/block_image_test.go
+++ b/block_image_test.go
@@ -6,7 +6,7 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
-func TestNewImageBlock(t *testing.T) {
+func TestImageURLForNewImageBlock(t *testing.T) {
 	imageText := NewTextBlockObject("plain_text", "Location", false, false)
 	imageBlock := NewImageBlock("https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png", "Marker", "test", imageText)
 
@@ -18,3 +18,17 @@ func TestNewImageBlock(t *testing.T) {
 	assert.Contains(t, imageBlock.Title.Text, "Location")
 	assert.Contains(t, imageBlock.ImageURL, "tripAgentLocationMarker.png")
 }
+
+func TestSlackFileForNewImageBlock(t *testing.T) {
+	imageText := NewTextBlockObject("plain_text", "Location", false, false)
+	slackFile := &SlackFileObject{URL: "https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png"}
+	imageBlock := NewImageBlockSlackFile(slackFile, "Marker", "test", imageText)
+
+	assert.Equal(t, imageBlock.BlockType(), MBTImage)
+	assert.Equal(t, string(imageBlock.Type), "image")
+	assert.Equal(t, imageBlock.Title.Type, "plain_text")
+	assert.Equal(t, imageBlock.ID(), "test")
+	assert.Equal(t, imageBlock.BlockID, "test")
+	assert.Contains(t, imageBlock.Title.Text, "Location")
+	assert.Contains(t, imageBlock.SlackFile.URL, "tripAgentLocationMarker.png")
+}