7
votes

According to Slack's documentation is only possible to send one file per time via API. The method is this: https://api.slack.com/methods/files.upload.

Using Slack's desktop and web applications we can send multiple files at once, which is useful because the files are grouped, helping in the visualization when we have more than one image with the same context. See the example below:

enter image description here

Do you guys know if it's possible, via API, to send multiple files at once or somehow achieve the same results as the image above?

Thanks in advance!

2

2 Answers

7
votes

I've faced with the same problem. But I've tried to compose one message with several pdf files.

How I solved this task

  1. Upload files without setting channel parameter(this prevents publishing) and collect permalinks from response. Please, check file object ref. https://api.slack.com/types/file. Via "files.upload" method you can upload only one file. So, you will need to invoke this method as many times as you have files to upload.
  2. Compose message using Slack markdown <{permalink1_from_first_step}| ><{permalink2_from_first_step}| > - Slack parse links and automatically reformat message
3
votes

Here is an implementation of the procedure recommended in the other answer in python

def postMessageWithFiles(message,fileList,channel):
    import slack_sdk
    SLACK_TOKEN = "slackTokenHere"
    client = slack_sdk.WebClient(token=SLACK_TOKEN)
    for file in fileList:
        upload=client.files_upload(file=file,filename=file)
        message=message+"<"+upload['file']['permalink']+"| >"
    outP = client.chat_postMessage(
        channel=channel,
        text=message
    )
postMessageWithFiles(
    message="Here is my message",
    fileList=["1.jpg", "1-Copy1.jpg"],
    channel="myFavoriteChannel",
)