Script in server saving locally

2019-08-16 02:07发布

问题:

I wrote a script that is using slack API to parse AWS S3 files looking for strings or samples. As this is in testing, I'm using my local machine and ngrok to forward localhost traffic.

The thing is that the generated files are getting stores in my machine and will be stored in server once the script is ready for production.

Ideally, I'd like to avoid users needing to grab files from server. Do you think it's possible to store directly in user local machine?

回答1:

No. Slack does not allow you to access the local machine of their users through a Slack app / API.

Solution 1: Download via browser

The easiest solution would be to offer a direct download link in a Slack message, e.g. via a Link button. Once the user clicks it he is prompted to download the file to his local machine.

Here is an example from one of my apps:

And once you click it you get this window:

To enable downloading via browser you need to set appropriate headers and send the file contents to the browser.

One approach is to have a helper script for the actual download and include a link to the helper script in the link button (you may also want to include some parameters in the link that defines what which file is downloaded).

The helper script then does the following:

  1. Fetch the file to be downloaded (e.g. an PNG image)
  2. Set headers to enable downloading via browser
  3. Send the file to the browser

Here is an example in PHP:

<?php
$filename = "demo.png";
$file = file_get_contents($filename);
header('Content-Disposition: attachment;filename=' . $filename);
header('Content-Type: image/png');
echo $file;
die();  

For more infos on download headers see also this answer on SO.

Solution 2: Upload to Slack

Alternatively you could upload the file to the user's Slack workspace via file.upload API method. That way the user does not need to download anything and and you can remove the file from your server after your app has finished processing.