Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send multiple files to Slack via API

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!

like image 928
Paulo Salgado Avatar asked Jan 27 '20 21:01

Paulo Salgado


People also ask

How do I send multiple files in API?

In order to make the API endpoint accept multiple files, we simply need to program the action method to take an input parameter with the type of List<IFormFile> . For example, the following action method accepts a list of files representing a student's certificates. In this simple example, we only deal with form files.

How do I send attachments in Slack API?

You must provide either a file or content parameter. The content of the file can either be posted using an enctype of multipart/form-data (with the file parameter named file ), in the usual way that files are uploaded via the browser, or the content of the file can be sent as a POST var called content .

Can you upload documents to Slack?

You can share just about any file type up to 1GB in size in Slack from your device, or add them from a file management app like Box, Dropbox, Google Drive, or OneDrive.


2 Answers

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
like image 151
Roman Kazakov Avatar answered Nov 12 '22 12:11

Roman Kazakov


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",
)
like image 43
xanatos Avatar answered Nov 12 '22 12:11

xanatos