scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 396 forks source link

Add a token header when downloading file #149

Closed kriw closed 7 years ago

kriw commented 7 years ago

It supports downloading file uploaded on slack. Related issue: https://github.com/lins05/slackbot/issues/99

jtatum commented 7 years ago

@kriw can you explain how to use this to download files? Where should the token value come from?

kriw commented 7 years ago

The token is your Slack API Token.
This is the example.

@respond_to("uploaded a file: \<?(.*)\>? .*")
def mention_download(message, url):
    if not 'file' in message.body:
        return

    url = message.body['file']['url_private']
    message.send("OK")
    if url.startswith('http'):
        with create_tmp_file() as tmpf:
            import secret
            download_file(url, tmpf, secret.API_TOKEN)
            message.channel.upload_file(url, tmpf,
                    'downloaded from {}'.format(url))

and

This is secret.py included in above file.

API_TOKEN = "xxxx-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx"