Search code examples
slackslack-api

SLACK - read the last image in a channel - get it's URL and send it to an API via a Slack APP


I want to upload a file to a channel, and then type /mycommand which would then read the slack url of the image that was uploaded into the channel and then send that as a argument to /mycommand [url]

I have managed to get my APP working so that I can invoke /mycommand [url] but I need help to figure out how to automate the reading of the last uploaded file vs. having to add the argument url to /mycommand.

Thanks in advance.


Solution

  • To get the URL of the latest shared file you can call the API method conversations.history for the current channel. It will return the list of all message incl. uploaded filed. From that you can filter out file uploads and sort by date to get the URL.

    Note that slash commands have a 3 seconds time limit, so you probably want to implement the processing of the channel history asynchronously.