Here is how I did the same thing using Go via the slack-go/slack library:
func uploadImagesToSlack(images []string, token string) ([]string, error) {
api := slack.New(token)
permalinkList := make([]string, 0, len(images))
for i, base64Image := range images {
imageData, err := base64.StdEncoding.DecodeString(base64Image)
if err != nil {
fmt.Printf("Error decoding image %d: %v\n", i, err)
continue
}
params := slack.FileUploadParameters{
Filename: fmt.Sprintf("image_%d.png", i+1),
Filetype: "auto",
Title: fmt.Sprintf("Image %d", i+1),
Content: string(imageData),
}
fileInfo, err := api.UploadFile(params)
if err != nil {
fmt.Printf("Error uploading file for image %d: %v\n", i+1, err)
continue
}
permalinkList = append(permalinkList, fileInfo.Permalink)
}
return permalinkList, nil
}
func postGalleryToSlack(permalinkList []string, channel, token string) error {
api := slack.New(token)
messageText := ""
for _, permalink := range permalinkList {
textSingleLink := fmt.Sprintf("<%s | >", permalink)
messageText = messageText + textSingleLink
}
channelID, timestamp, err := api.PostMessage(
channel,
slack.MsgOptionText(messageText, false),
)
if err != nil {
return err
}
fmt.Printf("Message successfully sent to channel %s at %s\n", channelID, timestamp)
return nil
}
The images were retrieved in a previous function and then encoded into base64 strings using base64.StdEncoding.EncodeToString()
, then this function is called which loops over the strings, decodes them, uploads to slack without posting, grabs the permalinks from the api.UploadFile() method, then posts them all in a single message which gives us the gallery format.